Formatting context edge cases
Definition and purpose
A line box is formed from inline-level participants. Their alignment and line-height contribute to its geometry. Block layout has different rules, including collapsing adjoining margins in eligible situations. Neither behavior is explained merely by adding all declared dimensions.
Bottom baseline
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #parent { font-size: 0; line-height: 0; } #a, #b { display: inline-block; width: 40px; background: #dbeafe; } #a { height: 20px; } #b { height: 40px; } </style> </head> <body> <div id="parent"> <span id="a"></span> <span id="b"></span> </div> </body></html>The short box starts 20px below the tall box.
Syntax and mental model
Empty inline-blocks with no internal line box use the bottom margin edge as baseline; with zero margins and zero parent line-height the two probes align at their bottom. vertical-align: top aligns them at the line-box top instead. For block flow, a parent establishing a block formatting context (display: flow-root) prevents its margin from collapsing with its in-flow children. It does not prevent every possible collapse between neighboring blocks.
Top alignment
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #parent { font-size: 0; line-height: 0; } #a, #b { display: inline-block; width: 40px; background: #dbeafe; } #a { height: 20px; } #b { height: 40px; } #a, #b { vertical-align: top; } </style> </head> <body> <div id="parent"> <span id="a"></span> <span id="b"></span> </div> </body></html>Both top edges coincide.
Common mistakes
vertical-align on an ordinary block does not reposition it like a flex alignment property. Font metrics can change real text line geometry; the zero-font fixture is deliberately limited to empty boxes.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Aspect ratio transfer and replaced sizing. Next lesson: Flex line collection and hypothetical size.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Formatting context edge cases — knowledge check
Five questions on formatting context edge cases.
Which property aligns inline-level boxes vertically?
Where is the baseline of these empty inline-blocks?
What does flow-root establish?
Does flow-root prevent parent-child margin collapse?
What can affect real text line geometry?