I have a list of images (not in a list, but could be if that might solve the problem) that I want to fill the whole width of a div. I've tried the code at the bottom, and while it does justify any text in the p tag it doesn't do the same for images. How can I get it to evenly space the images across the full width of the div?
<div>
<p>
<img src="image1.png" alt="foo" />
<img src="image2.png" alt="foo" />
<img src="image3.png" alt="foo" />
<img src="image4.png" alt="foo" />
<img src="image5.png" alt="foo" />
</p>
</div>
My CSS:
div { width: 30em; }
p { text-align: justify; }
Entire trick is to use float: left; CSS property on the img element and all the text will automatically wrap around that image. So if you want to align text and image in the same line in HTML like this... In short that is it.
Use the align-items property with the "center" value to place the items at the center of the container. Set the justify-content property to "center". Put the image's maximum width to 100% with the max-width property. Set the flex-basis property of the "image" class to specify the initial main size of your image.
Save this question.
An <img> element is an inline element (display value of inline-block ). It can be easily centered by adding the text-align: center; CSS property to the parent element that contains it. To center an image using text-align: center; you must place the <img> inside of a block-level element such as a div .
Because no one mention in the previous answers. Nowadays (2017) and for awhile ago you can use CSS3 flexbox
, by applying the property justify-content:space-between
div {
display: flex;
justify-content: space-between
}
<div>
<img src="//placehold.it/50" alt="foo" />
<img src="//placehold.it/50" alt="foo" />
<img src="//placehold.it/50" alt="foo" />
<img src="//placehold.it/50" alt="foo" />
<img src="//placehold.it/50" alt="foo" />
</div>
The simplest way to make it is :
.justify-image{ text-align: justify;}
.justify-image img{display:inline-block;}
.justify-image:after{content:""; display: inline-block; width: 100%; height: 0;}
<p class="justify-image">
<img src="https://via.placeholder.com/150x40" alt="my img">
<img src="https://via.placeholder.com/150x40" alt="my img">
<img src="https://via.placeholder.com/150x40" alt="my img">
<img src="https://via.placeholder.com/150x40" alt="my img">
<img src="https://via.placeholder.com/150x40" alt="my img">
<img src="https://via.placeholder.com/150x40" alt="my img">
</p>
text-align:justify;
(a <p>
paragraph in this case.)display:inline-block;
else they will not interact as "text" (in justify text last line is not affected you need to add one to make it works.):after
.
*:after
:
Don't forget content:"";
. Without it, it will not appear. In my example I've used an inline-block
with width:100%
. It will always break the line after the content in my paragraph.
Happy coding ! :)
In justified text, the last line of text in a paragraph is not expanded to fill the whole width. So to make the inline images spread out you need enough content to pull the paragraph out to two or more lines, eg.:
<div>
<p>
<img src="image1.png" alt="foo" />
<img src="image2.png" alt="foo" />
<img src="image3.png" alt="foo" />
<img src="image4.png" alt="foo" />
<img src="image5.png" alt="foo" />
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
</p>
</div>
(or some other equally-ugly but less-visible version involving
.) This is a bit of a nasty hack.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With