CSS Snippet

Classic Clearfix Techniques

Difficulty: Easy

Clearfix solves the classic float-collapse problem: a parent that contains only floated children has zero height. Modern flexbox and grid layouts make floats almost obsolete, but every codebase still has legacy CSS where you have to fix this. This snippet shows the modern `display: flow-root` one-liner alongside the historical pseudo-element clearfix and the empty-div trick, so you know what you are reading when you open old stylesheets.

Code Snippets
/

Classic Clearfix Techniques

Classic Clearfix Techniques

Clearfix solves the classic float-collapse problem: a parent that contains only floated children has zero height. Modern flexbox and grid layouts make floats almost obsolete, but every codebase still has legacy CSS where you have to fix this. This snippet shows the modern `display: flow-root` one-liner alongside the historical pseudo-element clearfix and the empty-div trick, so you know what you are reading when you open old stylesheets.

CSS
Easy
2 snippets
css-positioning
css-pseudo-elements
css-display

275 views

3

Use display: flow-root for any new code. It creates a new block formatting context, which has the side effect of containing floats inside the element, and it does so in a single declaration with no extra markup. The pseudo-element .clearfix::after { content: ""; display: block; clear: both; } is the version you will recognize from countless legacy codebases, including Bootstrap 3 and earlier. Both achieve the same result; the pseudo-element variant only exists because flow-root was not interoperable until 2018. Pick one technique per project and stick to it; mixing both on the same element is harmless but redundant.