# Block Formatting Context (BFC)

- **Canonical URL:** https://frontend.rocks/concepts/block-formatting-context/
- **Domain:** CSS & Layout
- **Baseline / Maturity Status:** Baseline · Widely
- **Last Verified:** 2026-11-15
- **TL;DR:** Stop parent-child vertical margin collapse and zero-height float wrappers without clipping shadows—display: flow-root isolates a box's layout region.

## Prerequisites
- [Render Tree & Layout](https://frontend.rocks/concepts/render-tree-layout.md): BFCs partition the Layout box tree into independent block-flow regions.

## Primary Sources
- https://drafts.csswg.org/css-display-3/#block-formatting-context
- https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_display/Block_formatting_context

---
## TL;DR

A Block Formatting Context (BFC) is an isolated layout compartment. Boxes inside a BFC never collapse vertical margins with the outside world, never overlap external floats, and expand their parent's `auto` height to enclose floated children.

## Interactive Lab

Toggle `display: flow-root` to trap floated children and isolate vertical margins in `ConstraintSimulatorLab`.

## Under the Hood

In the layout engine, a Block Formatting Context (BFC) partitions the `LayoutTree` into a self-contained block-flow region with its own float exclusion space and margin-collapsing boundary. Inside that sealed compartment, floats and vertical margins cannot leak outward, and external floats cannot intrude.

1. **Margin Isolation**: Normally, adjacent vertical margins between a parent and its first child collapse into a single margin. Establishing a BFC stops that collapse at the container edge.
2. **Float Enclosure**: During [Render Tree & Layout](/concepts/render-tree-layout), each BFC tracks its own float exclusion space, forcing `height: auto` to wrap floated descendants.
3. **Clean Creation**: `display: flow-root` creates a BFC with zero clipping or scrollbar side effects, unlike `overflow: hidden`.

```css
/* Clean BFC without clipping shadows or tooltips */
.media-card {
  display: flow-root;
}

.media-card > img {
  float: inline-start;
  margin-inline-end: 1rem;
}
```

Compare this with [CSS Containment](/concepts/css-containment) (`contain: layout`), which also establishes a BFC while additionally blocking layout invalidation walks, and [CSS Logical Properties](/concepts/css-logical-properties), which map block and inline axes to the document writing mode.

## Problem Genealogy

- **1998–2015**: Developers relied on `.clearfix::after` pseudo-elements or `overflow: hidden` to contain floats, accidentally clipping shadows and dropdowns.
- **2017+**: `display: flow-root` shipped specifically to create a side-effect-free BFC.

## Production Pitfalls

- **Accidental clipping with `overflow: hidden`**: Using `overflow: hidden` solely for a BFC slices off `box-shadow`, focus outlines, and `position: sticky` descendants.
- **Unexpected margin collapse**: A `margin-top` on the first heading inside a `<section>` escapes above the section's background unless a BFC or padding intervenes.
- **When not to use `display: flow-root`**: If the container already uses `display: flex` or `display: grid`, its children already live inside flex/grid formatting contexts, `flow-root` is redundant.
