Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make flex item siblings the same height

Tags:

html

css

flexbox

I have this simple example:

.container {
  display: flex;
  background-color: #ddd;
  align-items: stretch;
}

.logo {}

.text {
  font-size: 300%;
}
<div class="container">
  <div class="logo"><img src="https://via.placeholder.com/103x121.png/ff0" alt="test logo"></div>
  <div class="text">TEXT</div>
</div>

I need image to get same height as sibling div on right side not vv, i.e. shrink image to actual text height and keep aspect ratio.

like image 421
d3im Avatar asked Oct 30 '25 19:10

d3im


2 Answers

How is one sibling supposed to know the height of another sibling? That's outside the scope of CSS. You'll need a script for that.

But both siblings can share equal height if they refer to the height of their parent.

This may not be what you want, but here's one potential solution:

.container {
  display: flex;
  background-color: #ddd;
  height: 50px;
}

.logo {
  height: 100%
}

img {
  object-fit: contain;
  height: 100%;
}

.text {
  font-size: 300%;
}
<div class="container">
  <div class="logo"><img src="https://via.placeholder.com/103x121.png/ff0" alt="test logo"></div>
  <div class="text">TEXT</div>
</div>

Also see: One flex/grid item sets the size limit for siblings

like image 174
Michael Benjamin Avatar answered Nov 01 '25 10:11

Michael Benjamin


Use the same font-size value for the image height, but can't be % units.

.container {
  display: flex;
}

.logo img {
  height: 3em;
  width: auto;
}

.text {
  font-size: 3em;
}
<div class="container">
  <div class="logo"><img src="https://via.placeholder.com/103x121.png/ff0" alt="test logo"></div>
  <div class="text">TEXT</div>
</div>
like image 42
Stickers Avatar answered Nov 01 '25 09:11

Stickers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!