CSS Anchoring Text to Image Corner - html

I'm trying to make a responsive design were images scales according to the viewport, depending on whichever width or height of the viewport restricts the image, it never exceeds the viewport.
This was quite easy to achieve, but now I have the issue that the left-aligned title over the images doesn't of course follow the center'd image, but instead sticks by the page margin. I tried experimenting with all sorts of different calculated padding and other junk, but so far I can't figure it out.
What I'd ideally would like to do is to scale a box according to the viewport and center-align that box, and then have a full width image inside the box, and the left aligned title above it.
Here's what I've got so far, excuse the crude CSS.
Code:
/* Create 1 column that floats */
.column {
float: center;
width: 100%;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Add a card effect for articles */
.card {
background-color: var(--bg-color);
padding: 1vw;
margin-top: 1vw;
cursor: pointer;
width: 100%;
border-style: none;
}
.card h3 {
color: var(--text-color);
font-family: 'Lato', sans-serif;
font-size: 3vw;
text-align: left;
margin-left: 1.5vw;
margin-bottom: 0vw;
margin-top: 0vw;
}
.card img {
display: block;
width: auto;
height: auto;
max-width: 95vw;
max-height: 75vh;
margin-left: auto;
margin-right: auto;
padding: 1vw;
}
<div class="content">
<a href="page.html"><button type="button" class="card">
<h3>Image Title</h3>
<img src="https://via.placeholder.com/1920x1080.jpg"
alt="Image Title">
</button></a>
</div>

Supposing you can change your HTML structure and you're open to use a more semantic HTML structure you should go with the following.
.card {
display: flex;
justify-content: center;
}
<div class="card" onclick="myFunction()">
<figure>
<figcaption>Caption</figcaption>
<img src="https://dummyimage.com/100/eee">
</figure>
</div>
I've also added the onclick="myFunction()" attribute so you can mimic a button behavour aswell.
You could also use <a> tag instead of the <div> tag, just set it to display:block.
Now a little weird hack
I do not recommend this approach as it is not semantic (you will have to style it on your own).
.figure {
display: flex;
flex-flow: column nowrap;
margin: auto;
}
h3 {
text-align: left;
margin: 0;
}
<button class="figure">
<h3>Caption</h3>
<img src="https://dummyimage.com/100/eee">
</button>
You can use this if you absolutely need to have a <button> tag and you can't change your HTML structure. If none of those apply, the only advantage of this method is that you don't need another wrapper.
Notice that if you change <button> to <div> in the above example it will not work as it relies on the fact that <button> is a replaced element, and so its size is defined by content width, just like inline elements, but its margin are calculated like block elements, so you can use margin:auto.
CSS Spec on 10.3.4:
The used value of 'width' is determined as for inline replaced elements. Then the rules for non-replaced block-level elements are applied to determine the margins.
Anyway, I was just sharing what I've found while looking for how to solve this without changing HTML structure. Please go with the first solution.

You could wrap the h3 and the img in a div and center that. By the way, float:center doesn't exist.

Related

How to vertically align content within a div

I am struggling centering content vertically. Here is a screenshot:
I need a float left as there will be more content on the side which you can't see but yeah how can I get this text to vertically be in the center? Also I am not sure if I do need a tag in the tag
.newsletter_text_section {
width: 40%;
float: left;
padding: 15px;
font-size:24px;
padding-right: 0 !important;
display: inline-block;
height: 100%;
vertical-align: middle;
}
.newsletter_text_section p {
font-size:24px !important;
display: inline-block;
vertical-align: middle;
}
<!-- newsletter section -->
<div class="newsletter_section">
<div class="newsletter_text_section">
<p>Join Balance and get 20% off your first order</p>
</div>
<div class="newsletter_gif_section">
...
</div>
<div class="newsletter_input_section">
...
</div>
</div>
To solve this, the faster way is to set the same pixel for height and line-height of the element. Like this:
.box{
height : 10vh;
line-height: 10vh
}
Otherwise, you can also display: flex to layout your page, in flex scope, you can use align-item to align element vertically like this :
.box {
display: flex;
align-items: center;
justify-content: center;
}
For more detailed information, you can refer to here.
The next method is to adjust padding to your parent element since you are using the percent unit, but I don't recommend this way due to it exist side-effect sometimes.
The above content is what I think so now, hope it can help you.
Add This To The Style Of The Element:
position: absolute;
top: 50%;
Thanks,
Kamesta

Center an inline element that is a button [duplicate]

Is the property text-align: center; a good way to center an image using CSS?
img {
text-align: center;
}
That will not work as the text-align property applies to block containers, not inline elements, and img is an inline element. See the W3C specification.
Use this instead:
img.center {
display: block;
margin: 0 auto;
}
<div style="border: 1px solid black;">
<img class="center" src ="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a">
</div>
That doesn't always work... if it doesn't, try:
img {
display: block;
margin: 0 auto;
}
I came across this post, and it worked for me:
img {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
<div style="border: 1px solid black; position:relative; min-height: 200px">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a">
</div>
(Vertical and horizontal alignment)
Not recommendad:
Another way of doing it would be centering an enclosing paragraph:
<p style="text-align:center"><img src="https://via.placeholder.com/300"></p>
Update:
My answer above is correct if you want to start learning HTML/CSS, but it doesn't follow best practices
Actually, the only problem with your code is that the text-align attribute applies to text (yes, images count as text) inside of the tag. You would want to put a span tag around the image and set its style to text-align: center, as so:
span.centerImage {
text-align: center;
}
<span class="centerImage"><img src="http://placehold.it/60/60" /></span>
The image will be centered. In response to your question, it is the easiest and most foolproof way to center images, as long as you remember to apply the rule to the image's containing span (or div).
You can do:
<center><img src="..." /></center>
There are three methods for centering an element that I can suggest:
Using the text-align property
.parent {
text-align: center;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
Using the margin property
img {
display: block;
margin: 0 auto;
}
<img src="https://placehold.it/60/60" />
Using the position property
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
The first and second methods only work if the parent is at least as wide as the image. When the image is wider than its parent, the image will not stay centered!!!
But:
The third method is a good way for that!
Here's an example:
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}
<div class="parent">
<img src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/img_01.jpg" />
</div>
On the container holding image you can use a CSS 3 Flexbox to perfectly center the image inside, both vertically and horizontally.
Let's assume you have <div class="container"> as the image holder:
Then as CSS you have to use:
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
And this will make all your content inside this div perfectly centered.
Only if you need to support ancient versions of Internet Explorer.
The modern approach is to do margin: 0 auto in your CSS.
Example here: http://jsfiddle.net/bKRMY/
HTML:
<p>Hello the following image is centered</p>
<p class="pic"><img src="https://twimg0-a.akamaihd.net/profile_images/440228301/StackoverflowLogo_reasonably_small.png"/></p>
<p>Did it work?</p>
CSS:
p.pic {
width: 48px;
margin: 0 auto;
}
The only issue here is that the width of the paragraph must be the same as the width of the image. If you don't put a width on the paragraph, it will not work, because it will assume 100% and your image will be aligned left, unless of course you use text-align:center.
Try out the fiddle and experiment with it if you like.
img{
display: block;
margin-right: auto;
margin-left: auto;
}
If you are using a class with an image then the following will do
class {
display: block;
margin: 0 auto;
}
If it is only an image in a specific class that you want to center align then following will do:
class img {
display: block;
margin: 0 auto;
}
The simplest solution I found was to add this to my img-element:
style="display:block;margin:auto;"
It seems I don't need to add "0" before the "auto" as suggested by others. Maybe that is the proper way, but it works well enough for my purposes without the "0" as well. At least on latest Firefox, Chrome, and Edge.
Simply change parent align :)
Try this one on parent properties:
text-align:center
You can use text-align: center on the parent and change the img to display: inline-block → it therefore behaves like a text-element and is will be centered if the parent has a width!
img {
display: inline-block
}
To center a non background image depends on whether you want to display the image as an inline (default behavior) or a block element.
Case of inline
If you want to keep the default behavior of the image's display CSS property, you will need to wrap your image inside another block element to which you must set text-align: center;
Case of block
If you want to consider the image as a block element of its own, then text-align property does not make a sens, and you should do this instead:
IMG.display {
display: block;
margin-left: auto;
margin-right: auto;
}
The answer to your question:
Is the property text-align: center; a good way to center an image
using CSS?
Yes and no.
Yes, if the image is the only element inside its wrapper.
No, in case you have other elements inside the image's wrapper because all the children elements which are siblings of the image will inherit the text-align property: and may be you would not like this side effect.
References
List of inline elements
Centering things
.img-container {
display: flex;
}
img {
margin: auto;
}
this will make the image center in both vertically and horizontally
I would use a div to center align an image. As in:
<div align="center"><img src="your_image_source"/></div>
If you want to set the image as the background, I've got a solution:
.image {
background-image: url(yourimage.jpg);
background-position: center;
}
One more way to scale - display it:
img {
width: 60%; /* Or required size of image. */
margin-left: 20% /* Or scale it to move image. */
margin-right: 20% /* It doesn't matters much if using left and width */
}
Use this to your img CSS:
img {
margin-right: auto;
margin-left: auto;
}
Use Grids To Stack images. It is very easy here is the code
.grid {
display:grid;
}
.grid img {
display:block;
margin:0 auto;
}
If your img element is inside a div, which is itself inside another div whose display has been set as flexbox, as in my case here:
(HTML)
<nav class="header">
<div class="image">
<img
src=troll
alt="trollface"
></img>
</div>
<div class="title">
Meme Generator
</div>
<div class="subtitle">
React Course - Project 3
</div>
</nav>
(CSS)
.header{
display: flex;
}
.image{
width: 5%;
height: 100%;
}
.image > img{
width: 100%;
}
You could set your .image div to align itself vertically by doing this:
.image{
width: 5%;
height: 100%;
align-self: center;
}
display: block with margin: 0 didn't work for me, neither wrapping with a text-align: center element.
This is my solution:
img.center {
position: absolute;
transform: translateX(-50%);
left: 50%;
}
translateX is supported by most browsers
I discovered that if I have an image and some text inside a div, then I can use text-align:center to align the text and the image in one swoop.
HTML:
<div class="picture-group">
<h2 class="picture-title">Picture #1</h2>
<img src="http://lorempixel.com/99/100/" alt="" class="picture-img" />
<p class="picture-caption">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus sapiente fuga, quia?</p>
</div>
CSS:
.picture-group {
border: 1px solid black;
width: 25%;
float: left;
height: 300px;
#overflow:scroll;
padding: 5px;
text-align:center;
}
CodePen:
https://codepen.io/artforlife/pen/MoBzrL?editors=1100
Sometimes we directly add the content and images on the WordPress administrator inside the pages. When we insert the images inside the content and want to align that center. Code is displayed as:
**<p><img src="https://abcxyz.com/demo/wp-content/uploads/2018/04/1.jpg" alt=""></p>**
In that case you can add CSS content like this:
article p img{
margin: 0 auto;
display: block;
text-align: center;
float: none;
}
Use:
<dev class="col-sm-8" style="text-align: center;"><img src="{{URL('image/car-trouble-with-clipping-path.jpg')}}" ></dev>
I think this is the way to center an image in the Laravel framework.
To center an image with CSS.
img{
display: block;
margin-left: auto;
margin-right: auto;
}
You can learn more here
If you want to center image to the center both vertically and horizontaly, regardless of screen size, you can try out this code
img{
display: flex;
justify-content:center;
align-items: center;
height: 100vh;
}

How do I get the background color only behind the text?

This is my code but I want the text to only have background color behind it, and not stretch across the entire screen? Any ideas?
.section_title {
background-color: orange;
text-align: center;
margin: 0px auto;
}
HTML is
<div class="col-md-12">
<div class="section_title">
<h2>Choose a Pack to Print</h2>
</div>
</div>
An option is adding display: inline-block; to the CSS of the text element.
One problem I found with display: inline-block; is it clears floats incorrectly. Instead, I use width: fit-content;
.highlight {
background: yellow;
padding: 0.5em;
width: fit-content;
}
<h1 class="highlight">Highlight for text only!</h1>
<h1 class="highlight">Highlight me too!</h1>
There's a few ways to do this, but probably the best way is to make the h2 inline or inline-block.
Using inline-block will allow you to set width/height.
.section-title {
text-align: center;
}
.section-title h2 {
display: inline-block;
}
The other way to do this is to set a width on the h2 and set the margin to auto;
.section-title h2 {
margin-left: auto;
margin-right: auto;
width: 50%; /* for example */
}
If you want all your headings to be a set width, I'd choose the second one (allowing for text to wrap). If you want the box to be flexible and hug the contents, I'd use the first.

CSS Responsive Grid - items getting stuck on those with longer height

I'm building a portion of a portfolio site that displays a 4-column grid with images and their titles using relative units of measurement so that it scales with the browser window's size. Right now it works fine with each .item assigned a property of float:left and max-widths defined as a percentage of the overall #container's width (in this case 1100px or 68.75em). It works fine with the exception of the titles, which go below each image in a span. When one of the titles is longer than the 220px (or 20% of the container) the height increases and the items in the next row will get "stuck" on it.
I can fix this easily using PHP to insert a clear:both div after every 4th div (to effectively make a new "row" in the html) but I plan on using media queries or some other device to reduce the amount of columns to 3, 2 and 1 as the browser window shrinks. It would be easier if I could simply have the rows defined by floating the items. Possible solutions?
Styles:
body {
font-size: 100%;
line-height: 100%; /* Neat */
font-family: Helvetica, Arial, sans-serif;
}
#container {
max-width: 68.75em; /* 1100px */
margin: 40px auto;
border: 1px solid #000;
}
.item {
float: left;
width: 20%;
max-width: 20%;
height: auto;
padding: 2.5%;
background-color: #eee;
}
.item img {
display: block;
width: 100%;
max-width: 100%;
height: auto;
}
.item span {
width: 100%;
max-width: 100%;
margin-top: 1em;
display: block;
text-transform: uppercase;
line-height: 1.5em;
}
HTML:
<div id="container" class="cf"> <!-- "cf" comes from my reset, it's a clear-fix -->
<div class="item">
<img src="images/placeholder.png" height="220" width="220" alt="" title="" />
<span>A Title that is Slightly Longer than the Others</span>
</div>
<div class="item">
<img src="images/placeholder.png" height="220" width="220" alt="" title="" />
<span>A Title</span>
</div>
... (repeat those divs)
</div>
Possible options:
Set the height sufficient to cover two line cases
.grid-item { display: inline-block; vertical-align: top; }
Change the title span to a div, set height and overflow: hidden
I would suggest giving it a short title. If that's not possible, you could try
.item > span{
height: 0;
position: relative;
}
The images will display appropriately, but the too-long titles will display over the image beneath said title. I don't know if this is acceptable for your needs or not, but you may find it useful.

3x3 Grid Acting Strange at Certain Widths

I have a responsive 3x3 grid of video thumbnails that use pretty photo to lightbox vimeo clips. It works great at some resolutions, but as you resize the width of the browser window, it rapidly changes from 3x3 to this nonsense.
This is the offending CSS.
.thumb
{
float: right;
width: 25%;
height: auto;
margin: 2%;
}
.maincontent
{
margin:0 auto;
width:90%;
float:right;
}
And a snippet of the HTML if that matters.
<div class="maincontent">
<a href="http://vimeo.com/428525" rel="prettyPhoto" title="">
<img class="thumb" src="http://ftfrmedia.com/images/thumbnails/Overview-thumb.jpg" alt="Company Overview" width="60" />
</a>
I'm seriously stumped...
I think this is a problem with floats in .thumb and img.
Replacing the floats with display: inline-block should resolve the issue:
.thumb {
height: auto;
margin: 2em;
width: 25%;
}
img {
display: inline-block;
*display: inline; /* IE7 hack as it does not */
*zoom: 1; /* support display: inline-block */
height: 3em;
margin-left: 0.5em;
margin-right: 0.5em;
margin-top: -1.6em;
width: 3em;
}
Update
As pointed out by #DaveHaigh below, there is no need to use display: inline-block on the img element as this already accepts a height and width property.
Therefore, just removing the floats will fix this problem. If you would like to keep the right to left flow of the images then you can add direction: rtl to their parent:
.maincontent {
direction: rtl;
}
The downside to this would be that it would also flow any text right to left, but this does not appear to be a problem in your website as you are not using any text within the .maincontent element.
remove the floats on the .thumb and img. And also replace the margin: 2em; on .thumb with a percent value e.g. 2%.