Normal Flow, the Containing Block and Block Formatting Contexts
How the browser lays out boxes by default: normal flow with block-level and inline-level boxes, anonymous boxes, in-flow and out-of-flow elements, the containing block for each kind of positioning, and block formatting contexts and display: flow-root.
kvi5hnuCode Sprout team member10 min readOn this page
Before flexbox, grid or positioning, every element is placed by normal flow, the browser's default layout. Most of a typical page is still laid out this way: headings, paragraphs and lists stacking down the page, with text and links flowing inside them. Several confusing behaviors, such as margins escaping containers (article 40), percentages resolving against unexpected boxes, and floats spilling out of their parents, make sense once you know three ideas: normal flow, the containing block, and formatting contexts.
Normal flow

In normal flow, boxes come in two kinds:
- Block-level boxes, generated by elements such as
<p>,<h1>,<div>,<ul>and<section>, stack one after another along the block axis: top to bottom in horizontal writing. Each starts on a new line and, withwidth: auto, fills the width of its container (article 37). - Inline-level boxes, generated by elements such as
<a>,<span>,<strong>,<img>and text itself, flow along the inline axis, left to right in English, and wrap onto new lines when they run out of space. Each line is a line box (article 45).
Which kind an element generates comes from its display value (article 46), not from the HTML element itself. Browsers' default stylesheets give <div> display: block and leave <span> at the initial display: inline.
The axes follow the writing mode: in vertical Japanese text, blocks stack right to left and lines run top to bottom (article 47). The rules are the same; only the directions change.
Anonymous boxes
A block container holds either block-level children or inline-level content, not a mix. When HTML mixes them, the browser wraps each run of loose inline content in an anonymous block box:
<div class="note">
Opening hours changed: <!-- wrapped in an anonymous block box -->
<p>Sundays 10:00 to 16:00.</p>
</div>
Anonymous boxes can't be selected, so you can't style them directly. They inherit from their parent: here, the loose text gets .note's font and color. Similarly, text directly inside an element forms anonymous inline boxes. Flex and grid containers also wrap loose text in anonymous items (article 61).
In flow and out of flow
An element is in flow if it takes part in normal flow. Two things take an element out of flow:
- Floating (article 54): the box moves to one side, and following content wraps around it.
- Absolute or fixed positioning (article 56): the box is removed entirely, and the content around it closes up as if it weren't there.
Relative and sticky positioning keep the element in flow: it still takes its original space. Flex and grid items are in flow, but in a flex or grid layout rather than normal flow.
The containing block
Every box is sized and positioned relative to its containing block. Percentages for width, padding and margins (article 39), and offsets such as top and left, are resolved against it. Which box it is depends on the element's position:
Element's position | Containing block |
|---|---|
static, relative or sticky | the content box of the nearest ancestor that is a block container (or establishes a formatting context) |
absolute | the padding box of the nearest ancestor whose position isn't static |
fixed | the viewport |
the root element, <html> | the initial containing block: a rectangle the size of the viewport |
For absolute and fixed, some other properties also make an ancestor the containing block, even without position: transform, filter, backdrop-filter, perspective, contain: paint or layout, and will-change naming any of these. (container-type doesn't, in Chrome.) That's the reason a position: fixed modal sometimes scrolls with its parent: an ancestor has a transform. Article 56 covers positioning in detail.

The same 50% gives 100px and 130px, because the two children have different containing blocks. The blue bar shows the transform trap from the paragraph above.
If no ancestor qualifies, an absolutely positioned element uses the initial containing block. The initial containing block has the viewport's size, but sits at the top of the document, so it scrolls with the page.
Formatting contexts
A formatting context is a region that lays out its own contents by one set of rules, isolated from the outside:
| Formatting context | Lays out | Created by |
|---|---|---|
| block (BFC) | block boxes stacking, floats | the root, and the triggers below |
| inline | line boxes of text and inline boxes | any block container with inline content (article 45) |
| flex | flex items | display: flex or inline-flex (article 61) |
| grid | grid items | display: grid or inline-grid (article 66) |
| table | table parts | display: table (article 74) |
Flex, grid and table containers are always independent: margins don't collapse through them and floats don't enter or escape them. The interesting case is the block formatting context, because a plain block doesn't create one, so its contents interact with the outside.
What a new block formatting context does
A box that establishes a new BFC:
- Contains its floats. Without one, a parent's height ignores floated children, which then hang out of the bottom.
- Stops margin collapsing between itself and its children (article 40).
- Doesn't overlap floats next to it. A normal block's box runs underneath a neighboring float, with only its text wrapping around it; a BFC box narrows to fit beside the float.
body { font: 0.85rem/1.4 system-ui, sans-serif; margin: 1rem; }
section { margin-bottom: 1rem; }
h3 { font-size: 0.85rem; margin: 0 0 0.3rem; color: #8a3b12; }
.box { background: #f4efe6; border: 2px solid #8a3b12; padding: 0.4rem; }
.float { float: left; width: 3rem; height: 4rem; margin-right: 0.5rem; background: #2f7d4f; }
.flow-root { display: flow-root; }
.after { margin: 0.3rem 0 0; }

The measurements show both effects. The box contains its float, and the box beside a float narrows by the float's width plus its margin.
What creates one
Many properties create a new BFC as a side effect. Tested in Chrome:
| Creates a new BFC | Doesn't |
|---|---|
display: flow-root | a plain display: block |
display: inline-block, table-cell, table-caption | overflow: clip |
float other than none | position: relative or sticky |
position: absolute or fixed | opacity, transform, z-index, isolation |
overflow other than visible and clip | contain: size or inline-size alone |
contain: layout, content, strict or paint | |
container-type: size or inline-size | |
multi-column containers (column-count, column-width), column-span: all | |
| flex and grid items that are block containers |
The ones in the left column have other effects too: overflow: hidden clips and creates a scroll container (article 43), floats move, inline-block changes the outer display. display: flow-root was added to CSS so you can create a BFC with no side effects at all. It's the modern replacement for the old "clearfix" hack and for overflow: hidden used only to contain floats.
.media { display: flow-root; } /* contains a floated image, keeps child margins inside */
In flex and grid layouts, this rarely comes up: those containers are independent formatting contexts already, and their items each establish their own. Containment and container-type (articles 78 and 79) create one because they isolate layout for performance and container queries.
What's current
display: flow-roothas worked in all browsers since 2019, and is the clean way to create a block formatting context.- Container query containers (
container-type) establish block formatting contexts, which can change margin collapsing and float containment when you add them to an existing layout.
Common questions
Why is my container's height zero when its children are floated?
Floats are out of flow, so a normal parent doesn't grow to contain them. Give the parent display: flow-root, or use flexbox or grid instead of floats for the layout.
Why does position: fixed scroll with the page inside my component?
An ancestor has transform, filter, contain: paint or a similar property, which makes it the containing block for fixed descendants. Move the fixed element outside that ancestor, or use the top layer (article 60) for dialogs and popovers.
What's the difference between the containing block and the parent? Usually they're the same box, but not always. An absolutely positioned element's containing block is the nearest positioned ancestor, which may be several levels up, and it's that ancestor's padding box, not its content box.
Does every element create a formatting context? No. A plain block box takes part in its parent's block formatting context. It establishes an inline formatting context for its own text, but its block children, floats and margins interact with the surrounding BFC.
Is display: flow-root the same as overflow: hidden for clearing floats?
For containing floats and stopping margin collapse, yes. But overflow: hidden also clips content, such as shadows, focus rings and dropdowns, and makes a scroll container, which affects sticky descendants. flow-root does nothing else.
Summary
- In normal flow, block-level boxes stack along the block axis, and inline-level content flows in line boxes along the inline axis.
- Loose inline content next to blocks gets an anonymous block box. Floats and absolute or fixed positioning take elements out of flow.
- The containing block is the reference box for percentages and offsets: the parent's content box for static and relative elements, the nearest positioned ancestor's padding box for absolute ones, the viewport for fixed ones.
transform,filter,contain: paintand similar properties make an ancestor the containing block for absolute and fixed descendants.- A new block formatting context contains floats, stops margin collapsing with its children and sits beside floats.
display: flow-rootcreates one with no side effects.
Practice questions
A <div> contains a floated image and a short paragraph, and the image hangs out of the <div>'s bottom border. Fix it with one declaration that has no other side effects.
Answer
div { display: flow-root; }
How wide is the .badge element below? The card uses content-box sizing.
.card {
position: relative;
width: 400px;
padding: 20px;
border: 2px solid;
}
.card .badge {
position: absolute;
width: 50%;
}
Answer
220px. Its containing block is the card's padding box: 400 + 2 × 20 = 440px, and 50% of that is 220px.
A position: fixed toolbar works on most pages, but scrolls away on one page where it sits inside an animated wrapper. What's the likely cause?
Answer
The wrapper uses transform (or will-change: transform, or a filter), which makes it the containing block for fixed descendants, so the toolbar is fixed relative to the wrapper instead of the viewport.
Which of these create a new block formatting context: overflow: auto, overflow: clip, display: inline-block, position: relative, container-type: inline-size?
Answer
overflow: auto, display: inline-block and container-type: inline-size. overflow: clip and position: relative don't.
Previous: Overflow: Scroll Containers, overflow: clip, text-overflow, line-clamp and Scrollbars
Next: Inline Formatting: Line Boxes, line-height, vertical-align and text-box-trim

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.