Make flex item siblings the same height

10,272

Solution 1

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://upload.wikimedia.org/wikipedia/commons/0/07/Stora_Enso_web_logo.png" alt="test logo"></div>
  <div class="text">TEXT</div>
</div>

Also see: One flex item sets the height limit for siblings

Solution 2

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://upload.wikimedia.org/wikipedia/commons/0/07/Stora_Enso_web_logo.png" alt="test logo"></div>
  <div class="text">TEXT</div>
</div>
Share:
10,272

Related videos on Youtube

d3im
Author by

d3im

Updated on June 04, 2022

Comments

  • d3im
    d3im almost 2 years

    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://upload.wikimedia.org/wikipedia/commons/0/07/Stora_Enso_web_logo.png" 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.