Width flows down, height flows up
Why a child set to height: 50% can end up 1 line tall, and why a card won’t shrink past a long URL.
Contents4 sections
Here are two boxes, both 320px tall. Each holds a child set to height: 50%. One child gets its half and the other comes out 1 line tall. Which is which?
Which child gets half of its parent’s height?
Live: your browser’s own layoutYour browser draws the answer as soon as you pick.
Box A
How tall is the child?
min-height: 320px
Box B
How tall is the child?
flex item in a height: 320px row
The CSS for both boxes
/* Box A */
.parent { min-height: 320px; }
.child { height: 50%; }
/* Box B */
.row { display: flex;
height: 320px; }
.parent { /* no height */ }
.child { height: 50%; }min-height. B’s parent is a stretched flex item with no height of its own.The usual rule says a percentage height needs a parent with a height set. Both parents look like they have one, but only B’s child got its half. (Box A is from Josh Comeau’s The Height Enigma.)
Width goes in, height comes out
The browser works out widths from the top of the page down and heights from the bottom up. A parent tells each child how wide it may be, and the child answers with how tall it turned out. It can’t work the other way around. A paragraph’s height is the number of lines it wraps into, and that depends on its width. Many widths give the same height, so a height can’t tell the browser which width to use.
I wish this were on the first page of every CSS tutorial.
How tall is the card at this page width?
Live: your browser’s own layoutDrag the page width.
page: the width you set
This paragraph wraps to fit whatever width the card is given, and the card grows taller to hold every line it needs.
Drag the page width in the figure and the card’s height follows it. The line under the numbers lists every width that gives the height you’re looking at.
Most of the CSS sizing bugs that confuse people go against that order. There are two kinds.
Bug 1: a box asks for a size that doesn’t exist yet
A percentage is a fraction of the containing block, which is usually the parent. That nearly always works for a width, because the parent’s width arrives from above before the child is laid out. But a percentage height asks the parent for a number it only knows after its children are done.
Box A’s parent is 320px tall on the screen. How is that not a height? Its height is still auto, and the min-height only sets a floor under it. The spec calls a size “definite” when the browser can work it out without laying anything out, and auto is not definite. So the child’s 50% has nothing to read, and CSS 2.1 says it computes to auto. That’s 1 line.
Box B’s parent is a flex item in a 320px row. It has no height of its own, but the default align-items: stretch makes it as tall as the row. The flexbox spec counts that stretched height as definite, as long as the row’s own height is definite. So the child reads 320px and takes half. The spec even gives its reason. The exception is there so percentages in flex items resolve “where authors expected them to resolve.”
For Box A, changing min-height to height is enough. Every fix for this bug works the same way. It makes a size definite before the child asks for it.
Bug 2: the content pushes a width back up
Widths don’t only flow down. Before the real layout, the browser works out each box’s min-content width from the bottom up. That’s the narrowest a box can be without overflowing, usually the width of its longest word or URL.
A flex item defaults to min-width: auto, which in practice means its min-content width. So a long URL sets a floor under its card, and the row can’t shrink the card below it.
Which box stops the card from shrinking?
Live: your browser’s own layoutThe card spills out of the page. Try each fix.
page: the width you set
Flexbox spec: automatic minimum size
In the figure, a card with a long URL spills out of the page. There are three fixes to try. “Let the link shrink” does nothing. The link already clips, so its own minimum is already 0. The floor is on the card. Put min-width: 0 or overflow: hidden on the card and the floor is gone. The card fits, and the link gets its ellipsis!
So min-width: 0 has to go on the flex item with the floor, and that is not always the box you are looking at. Every flex item on the way up gets the same floor.
A 1fr grid column has the same floor. A long word can push the column wider than its container. That’s why the fix for a grid blowout is minmax(0, 1fr).
The four fixes
When a box won’t fill or won’t shrink, find where its size comes from before you change any CSS. If the box is waiting for a height, use one of the first three rows in the table below. If its content set a floor, use the last row, on the flex item with the floor.
| Fix | Examples |
|---|---|
| Make it definite from above | html, body { height: 100% }, an explicit height on the parent |
| Let the parent push it down | align-items: stretch without height: 100%, flex: 1, grid cells |
| Skip the tree | 100vh, 100svh (the window height with the mobile browser bars showing), position: absolute |
| Cut the upward flow | min-width: 0 or overflow: hidden on the item with the floor, minmax(0, 1fr), overflow-wrap: anywhere |