Skip to content Skip to sidebar Skip to footer

Create A Div With Text And Image Scaled To Fit Remaining Space?

I would like a div to contain text and also an image automatically scaled to fit the remaining space that the text did not occupy in the div. I've come to the conclusion that the

Solution 1:

I would like a div to contain text and also an image automatically scaled to fit the remaining space that the text did not occupy in the div.

For that to happen it is the image div that needs to be flex: 1, so it grows and take the available space, and the text div should have the default, which is flex: 0 1 auto, which mean, don't grow (0), allow to shrink (1), sized by content (auto).

Stack snippet

body { margin: 0; }

.item {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.item-img {
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
  flex: 1;
}

.item-desc {
}
<divclass="item"><divclass="item-img"style="background-image:url(http://placehold.it/400)"></div><divclass="item-desc">
    An image. This text will break it if too long. 
    An image. This text will break it if too long. 
    An image. This text will break it if too long. 
    An image. This text will break it if too long. 
    An image. This text will break it if too long. 
  </div></div>

Post a Comment for "Create A Div With Text And Image Scaled To Fit Remaining Space?"