Block formatting contexts
Definition and purpose
A block formatting context (BFC) is a region with its own block-flow layout rules. Establishing one can contain internal floats and prevent margin collapse across its boundary.
Float escapes ordinary height
<!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>#f { float: left; width: 60px; height: 50px; }</style> </head> <body> <div id="a"> <div id="f">Float</div> </div> <div id="b">After</div> </body></html>A has no in-flow content, so its ordinary auto height is zero.
Syntax and mental model
display: flow-root explicitly creates a BFC for a block container. A normal auto-height block may not enclose a float; a flow-root includes its participating floats in auto-height calculation. Overflow values such as hidden can also establish a BFC but may clip content.
Contain the float
<!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> #a { display: flow-root; } #f { float: left; width: 60px; height: 50px; } </style> </head> <body> <div id="a"> <div id="f">Float</div> </div> <div id="b">After</div> </body></html>A encloses its float without an explicit height.
Common mistakes
A BFC is not automatically a stacking context. Use flow-root for float containment rather than accidentally introducing scrolling or clipping.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Inner and outer display types. Next lesson: Floats and clear.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Block formatting contexts — knowledge check
Five questions on block formatting contexts.
Which explicitly creates a block formatting context?
Why can a float extend outside a normal auto-height block?
Why prefer flow-root over overflow:hidden for containment?
Does a BFC always create a stacking context?
Can parent-child margin collapse cross a new BFC boundary?