How do CSS floats and clearfix work?

BeginnerCSS interview questionBy , updated 17 November 2026

Short answer

float: left or float: right moves a box to one side of its container and lets the following text wrap around it, like an image in a magazine. Floats leave normal block flow, so a parent containing only floats collapses to zero height. Clearfix fixes that with a ::after element that has clear: both; today display: flow-root on the parent does the same job more cleanly.

What float was for

Float was designed for one thing: wrapping text around an image or a pull quote. Before flexbox and grid existed, developers also used it to build whole page layouts (columns were floated divs), which is where its reputation for being fiddly comes from. Today floats are back to their original job, and flexbox and grid do layout.

What a float does

When you float a box:

  1. It moves as far left or right as it can within its containing block, at the vertical position where it would otherwise have started.
  2. It is taken out of normal flow for block layout. Following block boxes act as if it were not there and slide up beneath it.
  3. Line boxes are not fooled. Text and inline content in those following blocks are shortened so they flow around the float.
  4. Its display is blockified: a floated <span> or <img> behaves as a block box and accepts width and height.
.article img {
  float: left;
  width: 12rem;
  margin: 0 1rem 0.5rem 0; /* keep text off the image */
}

Point 2 and point 3 together give the wrap effect: the paragraph's box runs under the image, but its text moves aside.

Some things ignore float completely. It has no effect on flex items or grid items, and an element with position: absolute or fixed computes float to none.

Clear

clear says "do not sit next to earlier floats". An element with clear: left, right or both is pushed down until its top edge is below every earlier float on that side.

.article h2 { clear: both; } /* each new section starts below the image */

The collapsing parent

Because floats are out of flow, they do not add to their parent's height. If every child floats, the parent is zero pixels tall:

<div class="gallery">            <!-- height: 0, its border is a flat line -->
  <img class="thumb" src="a.jpg" alt="" />
  <img class="thumb" src="b.jpg" alt="" />
</div>
<p>This paragraph rides up beside the images.</p>
.thumb { float: left; width: 8rem; }
.gallery { border: 1px solid #ccc; } /* the border does not wrap the images */

The parent's background and border do not reach the images, and the next element flows up beside them.

Clearfix: the classic fix

The clearfix adds an empty pseudo-element at the end of the parent and clears it. That pseudo-element is in normal flow, it must sit below the floats, and the parent grows to contain it, so the parent now ends below the floats too.

.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

content: "" makes the pseudo-element exist. clear: both pushes it below any floats. display: table (or block) makes it a box that can be cleared. Older "micro clearfix" versions also added a ::before with display: table so a child's top margin would not collapse through the parent.

display: flow-root: the modern fix

.gallery { display: flow-root; }

flow-root makes the element a new block formatting context, and a block formatting context always includes its floats when it works out its height. No extra class, no pseudo-element, and no side effects. It also stops a child's margin escaping through the parent. All current browsers support it.

Before flow-root, people often used overflow: hidden for the same effect. It also creates a block formatting context, but it clips anything that sticks out: box shadows, focus outlines and dropdown menus. The full set of rules is in What is a block formatting context (BFC) in CSS?, and the margin side is in What is margin collapsing in CSS?.

When floats are still the right tool

  • Text wrapping around an image or figure in articles, documentation and blog posts. Flexbox and grid put items beside each other; only floats let lines of text wrap around a box and then continue full width below it.
  • Pull quotes and side notes that the body text should flow around.
  • Non-rectangular wraps with shape-outside, which only works on floats: shape-outside: circle(50%) lets text hug a round portrait.
  • Drop caps, by floating ::first-letter (or using initial-letter where supported).

For columns, card rows, toolbars and page layouts, use flexbox or grid. Flexbox vs Grid: when to use which? covers that choice.

Practise it

Predict box model sizes and Predict percentage results both use display: flow-root to change the answer, so they test whether you know what a new formatting context contains. Keep a footer at the bottom of short pages with flexbox and with grid is the kind of layout people once built with floats and clears.

Common follow-up questions

Why does overflow: hidden fix a collapsed parent?

Any overflow value other than visible or clip makes the element a block formatting context root, and those include floats in their height. The clipping is a side effect, which is why display: flow-root was added as a property that does only the containment.

What is the difference between clear and clearfix?

clear is a property on an element that moves it below earlier floats. Clearfix is a technique that uses clear on a generated ::after element so the parent stretches to contain its floats without you adding extra markup.

Do floats work inside flexbox or grid?

No. If the parent is a flex or grid container, float on its children is ignored. A float only works inside block (flow) layout.

Why does text wrap around a float but backgrounds do not?

The following block's box really does sit underneath the float, so its background and border extend behind it. Only its line boxes are shortened. Making that block a formatting context (display: flow-root) stops it going under the float; it sits beside it instead.

Mistakes to avoid

  • Building page layouts or card grids with floats in new code

  • Reaching for overflow: hidden to contain floats, then losing shadows and dropdowns to clipping

  • Adding clearfix classes to flex or grid containers, where floats do nothing

  • Forgetting a margin on the floated image, so text touches its edge

  • Expecting float to work on an absolutely positioned element

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Predict box model sizes: box-sizing, padding, borders and margin collapseEasy25 min
  2. Predict percentage results: padding and margin from the width, heights inside auto parents, and transform percentagesEasy20 min
  3. Keep a footer at the bottom of short pages with flexbox and with gridEasy15 min