stackdisplay
stackdisplay

Reputation: 2045

Fit image with height 100% in flex

I have a responsive background image in a div. And i want a two column flex layout on top of it, in which the left layout have image of height 100% of the parent div with width auto scale like responsive image. and the right part is a two line text centering in the flex.

This is the closest i can get so far. But the flex does not stretch to fit the background div image and the left image does not scale accordingly when browser resize.

Note: None of the width and height in px should be specified in the code

.parent {
  display: flex;
  align-items: center;
}

.left {
  height: 100%;
}

.right {
  flex: 1;
  background: blue;
}
  <div style="position: relative;">
    <img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTkyfCkzwQ7Lx4v3YRNao0lQgM-VkEj6iLWTHE8KqHF5tk4cl15WQ" style="width: 100%">
    <div style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;">
      <div class="parent">
        <div class="left">
          <img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRsxhzGxiYQU_vp2YlN1LTMxQsYMhFDqTZLwfqMylCwqIahCu00Mf_0aDQ">
        </div>
        <div class="right">
          <p>
            123
          </p>
          <p>
            456
          </p>
        </div>
      </div>
    </div>
</div>

Expectation: enter image description here

Upvotes: 7

Views: 9564

Answers (2)

Ярослав
Ярослав

Reputation: 85

add align-self: normal to the image

Upvotes: -1

Harry Kitchener
Harry Kitchener

Reputation: 149

Remove align-items: center; from the parent div.

If you want to center align the text elements, use padding or position: relative/position: absolute

Then apply height: 100%; to the image.

Upvotes: 1

Related Questions