Understanding Block Formatting Contexts and Display Types in CSS
A Block Formatting Context (BFC) is part of the visual CSS rendering of a web page. It defines how block boxes interact with each other, how floats are contained, and how margins collapse. Certain display values trigger the creation of a BFC.
display: block – Standard block-level elements participate in normal flow and may form a BFC under specific conditions.
display: flow-root – Explicitly creates a new BFC, isolating its children from floats outside the element.
display: flex / display: inline-flex – Flex containers create a BFC, which affects float containment and margin collapsing inside the container.
display: grid / display: inline-grid – Grid containers also establish a BFC, affecting layout and containment of child elements.
In this example, display: flow-root creates a new BFC. Floated child elements do not overflow outside the container, and margins inside the container behave predictably.
Use BFC-triggering display types to contain floats and prevent overflow issues.
Consider flow-root when you want a block to self-clear its children.
Flex and grid containers automatically create a BFC, so they also help with layout containment.
Understanding which display types create a BFC helps manage margin collapsing and element stacking.
You have a container with floated children that's collapsing to zero height. How would you fix it using a BFC-triggering property?
What happens to the margins between two adjacent divs when you add overflow: hidden to their parent?
A button with display: inline-block sits inside a card. The card's background doesn't wrap the button. What's happening and how do you fix it?
We're seeing margin collapsing between a header and main content in our design system. The header uses display: flow-root. Why might this still happen and how would you debug it?
A teammate added display: inline-block to a button inside a card, and now the card's background doesn't wrap the button. Walk me through your debugging process.
You're building a media object component (image + text). The text should not wrap under the image when it's taller. Which display property on the container creates the right BFC for this?
We're migrating a legacy codebase that heavily uses clearfix hacks. How would you systematically replace them with modern BFC-triggering properties without breaking existing layouts?
Design wants a new 'card grid' component where cards can have variable content height but must align perfectly. Compare using flexbox vs grid vs BFC-based approaches for this.
A third-party widget injects content into our page and breaks our layout because it creates its own BFC. How would you isolate it?
Our design system team is standardizing on a 'layout primitive' component library. How would you define the BFC behavior contract for these primitives so consuming teams don't accidentally break encapsulation?
We're adopting CSS containment (contain: layout) for performance. How does this interact with BFCs, and what migration strategy would you recommend for a large codebase?
Two teams own adjacent micro-frontends that share a page. Team A's component creates a BFC that affects Team B's scrolling behavior. How do you establish cross-team layout contracts?