跪拜 Guibai
← All articles
Strut · IFC · CSS

That 12px Child Element Is 63px Tall Because of a Ghost Called Strut

By ssshooter ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Unexplained whitespace in CSS layouts is a persistent source of frustration and wasted debugging time. Knowing that a strut exists and how to eliminate it replaces guesswork with a deterministic fix, applicable to any project where inline-block elements misbehave inside a parent.

Summary

When a block container holds an inline-level child, the browser inserts an invisible strut that inherits the parent's font-size and line-height. Baseline alignment then forces the container to be at least as tall as that strut, creating unexpected whitespace above and below the child. The effect is most jarring when a single small inline-block sits inside a large-font parent with no other text.

Three fixes eliminate the gap: zeroing out the parent's font-size and line-height to kill the strut, switching the child to display: block so it establishes its own formatting context, or converting the parent to a flex or grid container. Each approach removes the IFC that spawns the strut in the first place.

The underlying mechanism ties into CSS's four formatting contexts — IFC, BFC, FFC, and GFC — which govern how elements participate in layout. Recognizing which context is active explains why a container's height doesn't always match its visible contents.

Takeaways
A strut is an invisible inline-level box the browser inserts inside a block container that directly holds inline-level children.
The strut's height equals the parent's computed line-height, not the child's.
Baseline alignment between the strut and the child forces the container to be at least as tall as the strut.
Setting font-size: 0 and line-height: 0 on the parent suppresses the strut entirely.
Changing the child to display: block makes it generate its own IFC, isolating it from the parent's strut.
Switching the parent to display: flex or display: grid removes the IFC, so no strut is created.
CSS has four formatting contexts: IFC, BFC, FFC, and GFC, each governing a different layout model.
Conclusions

Strut behavior is not a bug but a deliberate design for mixed inline content, yet it surprises developers because the browser's invisible box is never mentioned in most CSS tutorials.

The baseline-alignment default is the root cause; changing vertical-align on the child can also shift the layout, though it doesn't remove the strut itself.

Modern layout methods like flexbox and grid sidestep an entire class of IFC quirks, making them a more robust default than inline-block for component containers.

Concepts & terms
Strut
An invisible inline-level box that the browser inserts at the start of a block container's Inline Formatting Context. Its height is determined by the parent's font-size and line-height, and it participates in baseline alignment, often causing unexpected vertical space.
IFC (Inline Formatting Context)
A layout context created inside a block container that directly contains inline-level elements. In an IFC, boxes are laid out horizontally in a line and wrapped when they exceed the container width, with line heights governed by the tallest inline box on each line.
BFC (Block Formatting Context)
A layout context where block boxes are laid out vertically one after another. It prevents margin collapse between the container and its children, contains floats, and isolates the container from external float interference.
FFC (Flex Formatting Context)
A layout context triggered by display: flex or display: inline-flex. Child elements become flex items and are arranged along a main axis and cross axis, with powerful alignment and space-distribution controls.
GFC (Grid Formatting Context)
A layout context triggered by display: grid or display: inline-grid. The container is divided into rows and columns, and child elements can be placed into specific grid cells, spanning multiple rows or columns for two-dimensional layouts.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗