This is my default look with all images set to 100% width:
This images must have different height and width because of have to keep ratio. Because of that I can not use size with px, vw, etc. – only precents.
If I set images width to 60% i looks like that:
Images have 60% width but parent element has size like with 100% width image inside.
How make that parent element will has width like image?
<div class="casing">
<div class="casing__item">
<div class="item__icon"><img src="PK.png"></div>
<div class="item__label"><span>PK</span></div>
</div>
<div class="casing__item">
<div class="item__icon"><img src="PK-PW2.png"></div>
<div class="item__label"><span>PK+PW2</span></div>
</div>
<div class="casing__item">
<div class="item__icon"><img src="PK2W-PW2.png"></div>
<div class="item__label"><span>PK2W+PW2</span></div>
</div>
</div>
I tried with:
.casing__item {
display: flex;
flex-wrap: wrap;
gap: 0;
}
.casing__item > div {
width: auto;
}
.casing__item img {
width: 60%;
}
.casing__item {
display: table;
}
.casing__item > div {
display: table-cell;
vertical-align: middle;
}
.casing__item img {
width: 60%;
}
2
Answers
From what I can understand from your post, you want to be able to make a row of images smaller or larger while retaining their natural aspect ratio and remaining matched in height? Then just specify the height and let the width remain as
auto
.If I have misunderstood your post, please edit it to make your requirements clearer.
I’m a little confused as to what the desired result you want is. If you simply mean that you want the width of the parent to only be as wide as the child elements, you can set the width of the parent element to
inline-block
.This will make the width of the parent expand to be as wide as its child elements, and no more.