I have a container that contains random amount of images(1-3) and text.
If there is one image, the image needs to take the whole width, if there are two half the width etc
How can I implement such a thing?
Here is my code –
.images {
display: flex;
}
.images img {
max-height: 520px;
}
<div className="container">
<div className="user-container">
<div className="avatar">M</div>
<div className="user-data">
<div>Username</div>
<div>Time</div>
</div>
</div>
<div className="content">
Some Random Text
</div>
<div className="images">
<img src="https://picsum.photos/id/222/200/300">
<img src="https://picsum.photos/id/231/200/300">
</div>
</div>
2
Answers
You have to tell the images in question to resize themselves evenly. For this you set
flex-grow
andflex-shrink
both to1
(you can use the shorthandflex: 1 1
for this). After that also tell the images to start filling the available place from a size of0
This will space out all your images, regardless of how many you have.
If you do not specify the
width: 0
the images will try to take their intrinsic width, resulting in overflowing.You can simply achieve this by using flex.
Set the parent to
display: flex
, then set the sameflex
to the childs,the items will grow or shrink to fit the space available in its flex container.
flexbox
flex property
Example based on your code:
https://codesandbox.io/s/gallant-fire-gjr1db?file=/style.css