Set div to have its siblings width

Here’s another Flexbox solution which allows for the second child to wrap to match the width of the variable height sibling.

.wrapper > div {
  border: 1px solid;
}

.child {
  display: flex;
}

.child div {
  flex-grow: 1;
  width: 0;
}

.wrapper {
  display: inline-block;
}
<div class="wrapper">
    <div>This div is dynamically sized based on its content</div>
    <div class="child"><div>This div will always be the same width as the preceding div, even if its content is longer (or shorter too).</div></div>
</div>

Edit:

To support multiple divs under .child, where each div is on its own line, add break-after: always;

.child div {
  flex-grow: 1;
  width: 0;
  break-after: always;
}

Leave a Comment