Flexbox element height regardless of its content

I know this sounds like another flexbox question that is being addressed elsewhere, but I can't seem to find a solution from existing google or stackoverflow posts.

The problem is simple.

I have 3 child divs inside a parent div, one of which contains content larger than its final size. I would like each child to have 1/3

their parent REGARDLESS their content.

Here is a fiddle: http://jsfiddle.net/xeqo0msq/

Here is the HTML:

<div class="parent">
   <div class="child red">
       <div class="content">1</div>
   </div>
   <div class="child blue">2</div>
   <div class="child green">3</div>
</div>

      

And the related CSS:

.parent {
   display: flex;
   flex-direction: column; /*or row*/
   height: 100%;
   width: 100%;
}

.child {
   flex: 1;
}

.content{
   width: 800px;
   height: 100px;
}

.red {background: red;}
.blue {background: blue;}
.green {background: green;}

      

For flex-direction: row;

it works as expected: every child is 1/3

their parent, although the first content is larger. The width acts as if it is independent of the content.

screenshot of Flex Row

For the flex-direction: column;

first child, more than the other two.

Flex Column Screenshot

Is it possible to achieve this with flexbox and flex-direction: column;

?

+3


source to share


1 answer


When defined as flex-direction: row

, the parent's width is limited by the screen, so it is known. Flex distributes the width to three children.

But when flex-direction: column

, the parent's height is unknown (determined by the children). Although you define it as height: 100%

, but this one 100%

refers to the height of its parent, which is body

. Height is body

not the height of the screen.



There are three options for you:

  • Just add value height

    in .child

    to make them height adjustable: JSFiddle

  • Limit height .parent

    like JSFiddle .

  • If you need each child occupies 1/3 of the screen height

    you need to define the height html, body

    as 100%

    like this demo: JSFiddle

+2


source







All Articles