Skip to content
tezvyn:

Block Formatting Context: Taming Floats and Margins

Source: developer.mozilla.orgHardHow cards are made

Block Formatting Context: Taming Floats and Margins

A Block Formatting Context (BFC) is a mini-layout sandbox on your page, isolating its contents. Use it to contain child floats that would otherwise overflow their parent, or to stop adjacent elements from collapsing their margins.

Why it exists

In the default CSS layout model, some behaviors can be counter-intuitive. Floated elements are removed from the normal document flow, which can cause their parent containers not to resize to contain them. Additionally, vertical margins of adjacent block elements can collapse into each other. A Block Formatting Context (BFC) was created to give developers control over these interactions.

The mental model

Think of a BFC as a self-contained layout region on your webpage. It's like putting a box around a set of elements and telling the browser: "The layout rules inside this box don't interact with the rules outside it." It establishes a boundary for floats and margin collapsing, effectively creating a mini-layout within the main layout.

How it works

A BFC is not a property you set directly, but rather a context created by applying certain other CSS properties to an element. Properties like overflow: hidden, display: inline-block, position: absolute, and the modern display: flow-root all establish a BFC. An element that creates a BFC will do three main things: first, it will contain any floats inside it, meaning the element will expand to enclose them. Second, it will not wrap around any floats external to it. Third, the margins of elements inside the BFC will not collapse with margins outside it.

When to use it

Trigger a BFC to solve specific layout problems. The most common use case is to contain floats. If you have a container with floated children and the container's background or border isn't wrapping them, making the container a BFC will fix it. Another use is to prevent an element's text from wrapping around a preceding floated element, creating a clean column next to it.

When not to use it

Avoid creating a BFC accidentally. Using overflow: hidden or position: absolute just for a visual effect can have unintended layout consequences by creating a BFC. If you don't need to contain floats or stop margin collapse, you don't need a BFC. For most modern layouts, Flexbox and Grid are superior tools that create their own formatting contexts (flex or grid formatting contexts) and are generally easier to reason about than floats.

One canonical example

Imagine a div with a border, containing a floated img. If text next to the image is shorter than the image, the div's height will only match the text, and the taller image will "leak" out, breaking through the div's bottom border. By applying display: flow-root to the parent div, you create a BFC. This forces the div to expand its height to fully contain the floated img, and its border will correctly wrap around all its contents.

Interview question

Which common layout issue involving a parent container and its floated child elements is directly resolved by establishing a Block Formatting Context (BFC) on the parent?

  • a.The parent container's width shrinking to fit its content, ignoring the width of its floated children.
  • b.Vertical margins between the parent container and its adjacent sibling elements collapsing into each other.
  • c.The parent container's text content wrapping unexpectedly around a floated element positioned outside of it.
  • d.The parent container's background or border failing to expand and enclose its floated children.Correct
Why?

The card explicitly states that a primary use of BFC is to 'contain floats' and gives an example where a parent's border fails to wrap a floated image, which a BFC fixes. While BFCs also prevent margin collapse (Option B), the question specifically asks about issues involving 'parent container and its floated child elements', making float containment the most direct answer.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.

See open roles