Wesley.Chung
Wesley.Chung

Reputation: 25

Image overflows fixed-height flex container

I'm using a fixed-height flex container to do the layout. In the container, there are three components: header, content, footer. I want to force the content to use the rest of the height (i.e. content height = fixed-height minus header and footer). In my content, it will include an image and some texts.

However, the image always overflows a fixed-height flex container even providing max-height: 100% to constrain the height, but I want to put header, content, and footer into a fixed flex container.

Does anyone know how to fix it?

code: https://codepen.io/mrchung402/pen/wvGPJxz

<div class="container">
  <div class="header">
     header
  </div>
  <div class="content"> 
    <div class="my-img">
      <img src="http://via.placeholder.com/274x295">
    </div>
    <div class="my-text">
      my-text
    </div>
  </div>
  <div class="footer">
    footer
  </div>
</div>

.container {
  border: solid 1px red;
  display: flex;
  max-width: 500px;
  height: 200px;
  flex-direction: column;
}
.header {
  flex: 0 0 auto;
  background: #A7E8D3;
}
.content {
  display: flex;
  flex-direction: column;
}
.footer {
  flex: 0 0 auto;
  background: #D7E8D4;
}
.my-img {
  max-height: 100%;
  height: auto;
  max-width: 100%;
}

img {
  max-height: inherit;
  height: inherit;
}

.my-text {
  background: #C7A8D4;
}

img overflows flex item

Upvotes: 1

Views: 244

Answers (1)

Sachin Kumar
Sachin Kumar

Reputation: 406

Instead of styling .my-img you should style .my-img img

.my-img img{
  height: 100%;
  width: 100%;
}

Upvotes: 1

Related Questions