Sub topic 12
Block formatting contexts and display: flow-root
This chapter assumes you know how margins collapse, how overflow works, and how block and inline boxes differ. It explains the mechanism behind several earlier behaviours: a block formatting context.
What a block formatting context is
A block formatting context (BFC) is a region of the page where block boxes are laid out in their own isolated space. Inside a BFC, block boxes stack vertically, one after another. The layout inside a BFC does not interact with the layout outside it in three ways:
- Margins of a child do not collapse through the BFC's edge.
- The BFC grows tall enough to contain its floated children.
- The BFC does not overlap floats that sit beside it.
The root <html> element always creates a BFC. Other elements create one when certain properties are set.
What creates a BFC
Common triggers include:
| Declaration | Notes |
|---|---|
| `display: flow-root` | Purpose-built, no side effects |
| `overflow` other than `visible` (for example `hidden` or `auto`) | Can clip content or add scrollbars |
| `float` other than `none` | Also moves the element aside |
| `position: absolute` or `fixed` | Removes the element from normal flow |
| `display: inline-block`, `flex`, `grid`, `table-cell` | Changes how children are laid out |
Most triggers do something else too. That is why display: flow-root exists.
Problem 1: margin collapsing through a parent
A child's top margin can collapse with its parent's top margin when nothing separates them. The child's margin then appears outside the parent.
<style>
.container { background: lightblue; }
.container > h1 { margin-top: 2rem; }
</style>
<div class="container">
<h1>Title</h1>
</div>The blue background starts below the gap, not above the heading, because the h1 margin pushed the whole container down. Adding display: flow-root to the container creates a BFC. The margin is then kept inside the container, and the blue background includes the gap.
.container {
display: flow-root;
}
.container > h1 {
margin-top: 2rem;
}Problem 2: a parent that ignores floated children
Floated elements are taken out of normal flow. A parent that contains only floats collapses to zero height.
<style>
.gallery { border: 2px solid #333; }
.gallery img { float: left; width: 100px; }
</style>
<div class="gallery">
<img src="a.jpg" alt="First photo">
<img src="b.jpg" alt="Second photo">
</div>The border shows as a thin line because the images do not contribute to the height. A BFC must contain its floats, so this fixes it:
.gallery {
display: flow-root;
}Before flow-root, developers used overflow: hidden or a "clearfix" pseudo-element for this job. Both still work, but overflow: hidden can clip shadows and focus outlines of children.
Problem 3: text wrapping around a float
By default, a block box next to a float extends underneath it, and only its text wraps. If you give that block a BFC, the whole box stays beside the float instead. This is a simple way to build a media layout without Flexbox.
.media-image {
float: left;
width: 120px;
margin-right: 1rem;
}
.media-body {
display: flow-root;
}The text in .media-body now forms a column beside the image and does not wrap beneath it.
Common errors
- Using
overflow: hiddento contain floats, then finding that abox-shadowor dropdown is cut off. - Expecting
flow-rootto stop margin collapsing between siblings. Siblings in the same BFC still collapse. Only the parent-child edge is affected. - Expecting margins to be isolated on a flex or grid container's items. Flex and grid items do not collapse margins at all, so you do not need
flow-rootthere.
Exercise
A .card contains only a floated image and has a 1px border, but the border renders as a thin line above the image. Which single declaration fixes this without clipping any content, and why does it work?
Show answer
Add display: flow-root to .card. It makes the card establish a block formatting context, and a BFC grows to contain its floated children, so the border now surrounds the image. It has no side effects such as clipping, unlike overflow: hidden.
Exercise
An h1 with margin-top: 2rem sits inside a div with a background colour, and the gap appears outside the coloured area. Name two ways to keep the gap inside the div.
Show answer
Make the div create a BFC with display: flow-root, or give the div a padding or border on its top edge so the margins are separated. Using flow-root is the cleanest because it adds no visual change.