Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make image take full height of flex item

Tags:

html

css

flexbox

I have a container that is 100vh (minus the fixed nav height).

<section class="container">

Inside this container I have some text:

 <div class="text">
    <p>title</p>
 </div>

Which can be of any length as the content is dynamic.

Below this text I have an image:

 <div class="image">
    <img src="https://s-media-cache-ak0.pinimg.com/736x/d1/a6/64/d1a664bca214bf785a293cbc87950fc4.jpg">
 </div>

The image needs to fill the rest of the 100vh (- nav height) container.

I use:

.container{
    display:flex;
    flex-flow: column nowrap;
    ....

Fiddle

The issue I am having is that I need the image to be the height of the rest of the space.

How can I do this? In my fiddle, if your screen is small it is being cut off and if your screen is large it does not fill the space. Height: 100% fails to work, making it too large.

Flex solutions only please, no table tricks - thanks!

like image 545
panthro Avatar asked Jun 01 '16 17:06

panthro


People also ask

How do I make my flexbox 100% height?

Getting the child of a flex-item to fill height 100%Set position: relative; on the parent of the child. Set position: absolute; on the child. You can then set width/height as required (100% in my sample).

How do I make an image full height in HTML?

Make the image container ( . image ) a flex container with height: 100% . You can then fine-tune the image's aspect ratio and alignment with object-fit / object-position . Save this answer.

Can you set height of flexbox?

By default, a flex container has the following width and height assigned. width: auto; height: auto; But you can set a fixed height and width to the flex container.

How do you fit a picture in Flex?

You can easily shrink an image by using the flex-wrap property in CSS and it specifies whether flex items are forced into a single line or wrapped onto multiple lines. The flex-wrap property allows enabling the control direction in which lines are stacked.


1 Answers

Make the image container (.image) a flex container with height: 100%.

You can then fine-tune the image's aspect ratio and alignment with object-fit / object-position.

nav {
    position:fixed;
    background:grey;
    width:100%;
    height: 100px;
}

main {
  padding-top: 100px;
}

.container {
    display:flex;
    flex-flow: column nowrap;
    height: calc(100vh - 100px);
    background: green;
    border: 3px solid brown;
}

.text { background: yellow; }


/* ***** NEW ***** */
.image {
  display: flex;
  justify-content: center;
  height: 100%;
} 
img {
  width: 100%;
  object-fit: contain;
}
<nav>Nav</nav>
<main>
    <section class="container">
        <div class="text"><p>title</p></div>
        <div class="image">
            <img src="https://s-media-cache-ak0.pinimg.com/736x/d1/a6/64/d1a664bca214bf785a293cbc87950fc4.jpg">
        </div>
    </section>
    <section class="container">
        <div class="text"><p>hello</p></div>
        <div class="image">
            <img src="https://s-media-cache-ak0.pinimg.com/736x/d1/a6/64/d1a664bca214bf785a293cbc87950fc4.jpg">
        </div>
    </section>
</main>

Revised Fiddle

Note that the object-fit property is not supported by IE. For more details and a workaround see: https://stackoverflow.com/a/37127590/3597276

like image 64
Michael Benjamin Avatar answered Oct 12 '22 23:10

Michael Benjamin