← Back to HTML & CSS, page 1

html-css-0003 · Hard senior

What is a block formatting context (BFC), and how do you use it to fix layout bugs?

A block formatting context is a region where block boxes are laid out independently of the outside. It contains floats, stops a child's margin from collapsing through its parent, and keeps a normal block from running under a float. display: flow-root creates one without side effects.

Full answer

A block formatting context is a region of the page in which block boxes are laid out by the normal block rules, isolated from the layout outside. Everything inside a BFC stays inside it. Floats inside it are contained, and margins of the box itself do not collapse with its children.

Several things create a new BFC: the root element, floats, absolutely positioned boxes, display: inline-block, table cells, boxes with overflow other than visible, flex and grid items, contain: layout, and display: flow-root. flow-root was added for exactly this job. It creates a BFC with no side effects such as clipped content or scrollbars.

A BFC fixes three common bugs.

  1. A parent that does not wrap its floated child (the parent's height collapses).
  2. A child margin that leaks out through its parent (parent and child margins collapse).
  3. A normal block that runs underneath a float instead of sitting beside it.

Measured in Chrome: the first box is only 4px high (just its borders) because the float does not count, and the second box is 64px high. In the second pair, the first child's margin escapes, so parent and child share the same top edge, while inside the BFC the child sits 30px below its parent's top. In the last pair the first block still spans the full width, and only its text is pushed aside, while the BFC block starts after the 100px float and is 700px wide.

Before flow-root, people used overflow: hidden or a clearfix. Both still work, but overflow: hidden can clip shadows and focus rings, and the clearfix pseudo-element needs extra CSS.

html
<style>
  body { margin: 0; font-family: sans-serif; }
  section { display: flow-root; margin-bottom: 20px; }
  .float { float: left; width: 100px; height: 60px; background: #f90; }
  .box { border: 2px solid #333; }
  .bfc { display: flow-root; }
  .p .c { margin-top: 30px; height: 20px; background: #9cf; }
  .p { background: #eee; }
  .side { background: #9cf; }
</style>

<section>
  <div class="box" id="w1"><div class="float"></div></div>
  <div class="box bfc" id="w2"><div class="float"></div></div>
</section>

<section>
  <div class="p" id="p1"><div class="c" id="c1"></div></div>
</section>
<section>
  <div class="p bfc" id="p2"><div class="c" id="c2"></div></div>
</section>

<section>
  <div class="float"></div>
  <div class="side" id="s1">Runs under the float</div>
</section>
<section>
  <div class="float"></div>
  <div class="side bfc" id="s2">Sits beside the float</div>
</section>

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

  • A BFC is created by float, absolute or fixed position, inline-block, table cells, overflow other than visible, flex or grid items, contain and flow-root.
  • Floats inside a BFC are included in its height.
  • Margins of a BFC root do not collapse with its children's margins.
  • A block that creates a BFC does not overlap a neighbouring float.
  • display: flow-root is the modern trigger; overflow: hidden and clearfix are older ones.

Common mistakes

  • using overflow: hidden to contain floats and then wondering why a dropdown or box-shadow is cut off.
  • thinking a BFC stops margin collapse between siblings. It does not. Collapse between adjacent sibling margins happens inside the same BFC, and you stop it with padding, a border or a different layout mode such as flex or grid.

Follow-ups

  • do flex containers use a BFC?
  • Why can overflow: hidden cause a new bug when used to contain floats?
  • Is a flex container a block formatting context?

Tips

  • Remember the list of triggers by group: float, absolute and fixed, inline-block and table-cell, overflow not visible, flex or grid item, contain, flow-root. In an interview, name flow-root first as the modern choice.
  • Name the three symptoms first (collapsed parent, leaking margin, text under float). Then name the cure. Interviewers like a cause-and-fix pattern.
  • Say that flow-root was added to do this one job without clipping content.