← Back to HTML & CSS, page 8

html-css-0143 · Easy fresher

A child has height: 50% but it does not change. Why, and how do you fix it?

A percentage height needs the parent's height to be definite. With an auto-height parent the percentage is treated as auto, to avoid a circular dependency. Give the parent a height, use viewport units, or use flex or grid.

Full answer

A percentage height is a share of the height of the containing block. If the parent's height is auto, which means it depends on its content, the browser cannot compute the share, because the child's height would then change the parent's height, and the parent's height would change the child's. To avoid the loop, a percentage height on a child of an auto-height parent is treated as auto.

Percentage widths do not have this problem, because a block's width comes from its parent and not from its content.

Fixes depend on what you want:

  1. Give the parent a definite height, such as height: 300px. Then 50% works. For a full-page layout, every ancestor in the chain must have a height: html, body { height: 100%; } and so on. Using min-height on the parent does not count as definite for this purpose in older rules, so check the result.
  2. Use viewport units for the page: height: 50vh ignores the parent entirely.
  3. Use Flexbox or Grid. In a flex column with a set height, flex: 1 on a child makes it fill the space. In grid, grid-template-rows: 1fr 1fr splits the height without percentages.
  4. For a box with a shape such as a square, use aspect-ratio instead of a height percentage.

In most layouts, Flexbox or Grid is the better choice than a chain of height: 100%.

In the first box the child ignores 50% and is as high as its text. In the second box the child is 100px high. In the third box the child takes all 200px of the height through flex: 1.

When a percentage height has no effect, check each ancestor for a definite height.

html
<style>
  .parent { width: 200px; background: #eee; }
  .child { height: 50%; background: #cde; }
  .p-fixed { height: 200px; }
  .p-flex { height: 200px; display: flex; flex-direction: column; }
  .p-flex .child { flex: 1; height: auto; }
</style>
<div class="parent" id="a"><div class="child" id="ca">content</div></div>
<div class="parent p-fixed" id="b"><div class="child" id="cb">content</div></div>
<div class="parent p-flex" id="c"><div class="child" id="cc">content</div></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Percentage heights need a definite parent height.
  • An auto-height parent makes the percentage act as auto.
  • Width percentages work because width comes from the parent.
  • Set heights up the chain, or use vh units.
  • Flex and grid usually replace the percentage.

Common mistakes

  • Setting height: 100% on a child but leaving the parent with auto height.
  • Fixing it by setting height on every ancestor when flex or grid would be simpler.
  • Confusing it with percentage width, which works with an auto-height parent.

Follow-ups

  • Why do percentage widths not have this problem?
  • How do you make a child fill the rest of the parent's height?

Tips

  • Explain the circular dependency: the parent's height depends on the child.
  • Give the three fixes: definite parent height, viewport units, flex or grid.