Skip to content
Tutorials Center
My learning

Topics

Lesson 61 of 139 ยท Layout Fundamentals

Floats

Use CSS float to wrap text around images and pull quotes, stop wrapping with clear, contain floats with flow-root, and shape text with shape-outside.

Open a printed magazine and you will often see a photo tucked into the corner of an article, with the text flowing neatly around it. That is exactly what float was designed for. A floated element is pushed to the left or right side of its container, and the text and inline content that follows wraps around it.

For about fifteen years, floats were also abused to build entire page layouts, because CSS had nothing better. Those days are over: flexbox and grid do layout far better. But floats are still the right tool for their original job, and you will meet them in older code. This lesson shows how they work, how to stop and contain them, and how to make text wrap around a curved shape.

How a float behaves

float takes one of these values:

Value Effect
none The default: no floating.
left / right Pushes the element to that side of its container.
inline-start / inline-end The logical versions: the start or end side in the current writing direction. In right-to-left languages, inline-start is the right side.

When an element floats:

  1. It is taken partly out of the normal flow. Block boxes after it move up as if it were not there.
  2. But line boxes (the lines of text inside those blocks) shorten to make room for it, so the text wraps around the float.
  3. It becomes a block box. A floated <img> or <span> respects width, height and vertical margins, just like display: block.
  4. Its width shrinks to fit its content unless you set one.

Point 1 and 2 together are the whole trick: the blocks ignore the float, but the text inside them avoids it. You can see this when a paragraph has a background color: the background runs underneath the float while the words wrap around it.

An image floated into an article

The photo floats left and the pull quote floats right. Notice the margins: a float needs a margin on the side facing the text, or the words touch its edge. Percentage widths keep both floats in proportion as the column gets narrower.

Pull quotes and accessibility. A pull quote repeats text from the article, so a screen reader would read it twice. On a real site you might add aria-hidden="true" to it. Images still need meaningful alt text, exactly as in the HTML lesson on images.

Several floats in a row

Floats with the same direction line up next to each other, like words in a line, and wrap to the next "row" when there is no more room. If a float does not fit beside the previous one, it moves down until it does. This is how people once built grids of cards, and why those grids broke so easily: a taller card could snag the next float and leave odd gaps. Today a flex or grid container gives you a far more reliable row.

clear: starting below a float

Sometimes you want an element to not wrap, but start below any floats. That is what clear does:

Value Moves the element below...
left any earlier left floats
right any earlier right floats
both all earlier floats
inline-start / inline-end floats on the logical start or end side
none nothing (the default)

A typical case is a heading that should begin a new section cleanly, not squeeze next to the previous section's photo:

CSS
article h2 {
  clear: both;
}

clear works by adding space above the element, called clearance, until its top edge sits below the floats. Because of that, a margin-top you set on a cleared element may seem to disappear: the clearance replaces it. If you need a gap, put a margin-bottom on the float instead.

Containing floats: display: flow-root

Because floats leave the normal flow, a container does not automatically grow to include them. If the float is taller than the text next to it, it sticks out of the bottom of its container and overlaps whatever comes next. If a container holds only floats, it collapses to zero height.

The modern fix is one line: display: flow-root on the container. It creates a new block formatting context, which always wraps its floats. You met it in The display Property.

Containing a float with flow-root

In the first comment the avatar spills out of the border and pushes into the second comment. The others use flow-root and wrap their avatars properly.

You will find older ways of doing this in existing code:

  • The clearfix hack: a ::after pseudo-element with content: ""; display: table; clear: both; on the container. It works, but flow-root does the same job with no trick.
  • overflow: hidden (or auto) on the container also creates a formatting context. It clips shadows and dropdowns that stick out, though, so it has side effects.

Flex and grid containers contain their children automatically. In fact, float has no effect at all on a flex or grid item.

Wrapping around shapes with shape-outside

Text normally wraps around a float's rectangle. shape-outside lets it wrap around a different shape, using the same functions as clip-path: circle(), ellipse(), polygon() and inset(). It only works on floats.

Text flowing around a circle

shape-outside also accepts an image URL, using the image's transparent areas as the shape. Images used this way must be same-origin or served with CORS headers, so the gradient and circle() approach is simpler for most cases.

Why not use floats for layout?

Before flexbox and grid, sidebars, columns and card grids were all built from floats. It worked, with effort, but it was fragile:

  • Columns could not have equal heights without tricks.
  • Vertical centering was nearly impossible.
  • Every container needed a clearfix.
  • One slightly too-wide column dropped below the others.
  • Changing the visual order meant changing the HTML.

Flexbox and grid solve all of these directly. So the rule today is simple: use float to wrap text around something; use flexbox or grid to arrange boxes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is float designed for today?

    Floats push an element to one side so inline content wraps around it. Layout is the job of flexbox and grid.
  2. 2. A container holds only floated children and has zero height. What is the modern fix?

    flow-root creates a block formatting context, which grows to contain its floats.
  3. 3. What does clear: both do?

    Clearing adds space above the element until it sits below any earlier left and right floats.
  4. 4. Which property makes text wrap around a circle instead of the float's rectangle?

    border-radius and clip-path only change how the float looks. shape-outside changes the shape the text wraps around.

Summary

  • float: left or right (or the logical inline-start/inline-end) pushes an element to one side, and inline content wraps around it.
  • Floats leave the normal flow: blocks ignore them, but line boxes shorten to avoid them.
  • clear moves an element below earlier floats.
  • display: flow-root makes a container wrap its floats; the old clearfix and overflow: hidden do the same with side effects.
  • shape-outside lets text follow a circle, ellipse or polygon.
  • Use floats for wrapping text, and flexbox or grid for layout.

Next: CSS Shapes.