Use nested flex containers.
Get rid of percentage heights. Get rid of table properties. Get rid of vertical-align
. Avoid absolute positioning. Just stick with flexbox all the way through.
Apply display: flex
to the flex item (.item
), making it a flex container. This automatically sets align-items: stretch
, which tells the child (.item-inner
) to expand the full height of the parent.
Important: Remove specified heights from flex items for this method to work. If a child has a height specified (e.g. height: 100%
), then it will ignore the align-items: stretch
coming from the parent. For the stretch
default to work, the child's height must compute to auto
(full explanation).
Try this (no changes to HTML):
.container {_x000D_
display: flex;_x000D_
flex-direction: column;_x000D_
height: 20em;_x000D_
border: 5px solid black_x000D_
}_x000D_
_x000D_
.item {_x000D_
display: flex; /* new; nested flex container */_x000D_
flex: 1;_x000D_
border-bottom: 1px solid white;_x000D_
}_x000D_
_x000D_
.item-inner {_x000D_
display: flex; /* new; nested flex container */_x000D_
flex: 1; /* new */_x000D_
_x000D_
/* height: 100%; <-- remove; unnecessary */_x000D_
/* width: 100%; <-- remove; unnecessary */_x000D_
/* display: table; <-- remove; unnecessary */ _x000D_
}_x000D_
_x000D_
a {_x000D_
display: flex; /* new; nested flex container */_x000D_
flex: 1; /* new */_x000D_
align-items: center; /* new; vertically center text */_x000D_
background: orange;_x000D_
_x000D_
/* display: table-cell; <-- remove; unnecessary */_x000D_
/* vertical-align: middle; <-- remove; unnecessary */_x000D_
}
_x000D_
<div class="container">_x000D_
<div class="item">_x000D_
<div class="item-inner">_x000D_
<a>Button</a>_x000D_
</div>_x000D_
</div>_x000D_
_x000D_
<div class="item">_x000D_
<div class="item-inner">_x000D_
<a>Button</a>_x000D_
</div>_x000D_
</div>_x000D_
_x000D_
<div class="item">_x000D_
<div class="item-inner">_x000D_
<a>Button</a>_x000D_
</div>_x000D_
</div>_x000D_
</div>
_x000D_
My problem is that
.item-inner { height: 100% }
is not working in webkit (Chrome).
It's not working because you're using percentage height in a way that doesn't conform with the traditional implementation of the spec.
10.5 Content height: the
height
propertypercentage
Specifies a percentage height. The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly and this element is not absolutely positioned, the value computes toauto
.auto
The height depends on the values of other properties.
In other words, for percentage height to work on an in-flow child, the parent must have a set height.
In your code, the top-level container has a defined height: .container { height: 20em; }
The third-level container has a defined height: .item-inner { height: 100%; }
But between them, the second-level container – .item
– does not have a defined height. Webkit sees that as a missing link.
.item-inner
is telling Chrome: give me height: 100%
. Chrome looks to the parent (.item
) for reference and responds: 100% of what? I don't see anything (ignoring the flex: 1
rule that is there). As a result, it applies height: auto
(content height), in accordance with the spec.
Firefox, on the other hand, now accepts a parent's flex height as a reference for the child's percentage height. IE11 and Edge accept flex heights, as well.
Also, Chrome will accept flex-grow
as an adequate parent reference if used in conjunction with flex-basis
(any numerical value works (auto
won't), including flex-basis: 0
). As of this writing, however, this solution fails in Safari.
#outer {_x000D_
display: flex;_x000D_
flex-direction: column;_x000D_
height: 300px;_x000D_
background-color: white;_x000D_
border: 1px solid red;_x000D_
}_x000D_
#middle {_x000D_
flex-grow: 1;_x000D_
flex-basis: 1px;_x000D_
background-color: yellow;_x000D_
}_x000D_
#inner {_x000D_
height: 100%;_x000D_
background-color: lightgreen;_x000D_
}
_x000D_
<div id="outer">_x000D_
<div id="middle">_x000D_
<div id="inner">_x000D_
INNER_x000D_
</div>_x000D_
</div>_x000D_
</div>
_x000D_
1. Specify a height on all parent elements
A reliable cross-browser solution is to specify a height on all parent elements. This prevents missing links, which Webkit-based browsers consider a violation of the spec.
Note that min-height
and max-height
are not acceptable. It must be the height
property.
More details here: Working with the CSS height
property and percentage values
2. CSS Relative & Absolute Positioning
Apply position: relative
to the parent and position: absolute
to the child.
Size the child with height: 100%
and width: 100%
, or use the offset properties: top: 0
, right: 0
, bottom: 0
, left: 0
.
With absolute positioning, percentage height works without a specified height on the parent.
3. Remove unnecessary HTML containers (recommended)
Is there a need for two containers around button
? Why not remove .item
or .item-inner
, or both? Although button
elements sometimes fail as flex containers, they can be flex items. Consider making button
a child of .container
or .item
, and removing gratuitous mark-up.
Here's an example:
.container {_x000D_
height: 20em;_x000D_
display: flex;_x000D_
flex-direction: column;_x000D_
border: 5px solid black_x000D_
}_x000D_
_x000D_
a {_x000D_
flex: 1;_x000D_
background: orange;_x000D_
border-bottom: 1px solid white;_x000D_
display: flex; /* nested flex container (for aligning text) */_x000D_
align-items: center; /* center text vertically */_x000D_
justify-content: center; /* center text horizontally */_x000D_
}
_x000D_
<div class="container">_x000D_
<a>Button</a>_x000D_
<a>Button</a>_x000D_
<a>Button</a>_x000D_
</div>
_x000D_
4. Nested Flex Containers (recommended)
Get rid of percentage heights. Get rid of table properties. Get rid of vertical-align
. Avoid absolute positioning. Just stick with flexbox all the way through.
Apply display: flex
to the flex item (.item
), making it a flex container. This automatically sets align-items: stretch
, which tells the child (.item-inner
) to expand the full height of the parent.
Important: Remove specified heights from flex items for this method to work. If a child has a height specified (e.g. height: 100%
), then it will ignore the align-items: stretch
coming from the parent. For the stretch
default to work, the child's height must compute to auto
(full explanation).
Try this (no changes to HTML):
.container {_x000D_
display: flex;_x000D_
flex-direction: column;_x000D_
height: 20em;_x000D_
border: 5px solid black_x000D_
}_x000D_
_x000D_
.item {_x000D_
display: flex; /* new; nested flex container */_x000D_
flex: 1;_x000D_
border-bottom: 1px solid white;_x000D_
}_x000D_
_x000D_
.item-inner {_x000D_
display: flex; /* new; nested flex container */_x000D_
flex: 1; /* new */_x000D_
_x000D_
/* height: 100%; <-- remove; unnecessary */_x000D_
/* width: 100%; <-- remove; unnecessary */_x000D_
/* display: table; <-- remove; unnecessary */ _x000D_
}_x000D_
_x000D_
a {_x000D_
display: flex; /* new; nested flex container */_x000D_
flex: 1; /* new */_x000D_
align-items: center; /* new; vertically center text */_x000D_
background: orange;_x000D_
_x000D_
/* display: table-cell; <-- remove; unnecessary */_x000D_
/* vertical-align: middle; <-- remove; unnecessary */_x000D_
}
_x000D_
<div class="container">_x000D_
<div class="item">_x000D_
<div class="item-inner">_x000D_
<a>Button</a>_x000D_
</div>_x000D_
</div>_x000D_
_x000D_
<div class="item">_x000D_
<div class="item-inner">_x000D_
<a>Button</a>_x000D_
</div>_x000D_
</div>_x000D_
_x000D_
<div class="item">_x000D_
<div class="item-inner">_x000D_
<a>Button</a>_x000D_
</div>_x000D_
</div>_x000D_
</div>
_x000D_