Skip to content

CSS Float: Wrap Text Around an Image, and Clear It

float: left pushes an element to the left side of its container and lets the text that follows flow around its right side, the way a photo sits in a newspaper column. float: right does the same on the other side. That job, wrapping text around a picture, is still what floats are for. Page layouts with columns used to be built with floats too, but flexbox and grid have taken over that part.

Text wrapping around a floated image

The picture is pulled to the left and the paragraph fills the space beside it, then continues underneath once it runs past the bottom of the picture. Delete float: left; and the image sits on its own above the text instead.

Wrap text around an image

Three things make a floated image look right: a width (or the float takes the image’s full size), a margin on the side that faces the text, and the image coming before the text in the HTML. A float only affects content that comes after it, so an image placed at the end of a paragraph floats next to whatever follows the paragraph, not next to the paragraph itself.

A float also turns the element into a block, so width, height and vertical margins work even on an inline element like <span>. Without a width, a floated box shrinks to fit its content.

For the right side, swap the margin over too, so the gap still faces the text:

An image floated right

margin: 0 0 8px 14px means nothing on top and right, 8px below and 14px on the left. Set the margin to 0 and the words press right up against the picture. Rounded corners come from border-radius; floats don’t mind.

clear: start below the float

Text keeps wrapping until it has gone past the bottom of the float. When the paragraph beside the picture is short, the next heading wraps up next to the picture as well, which usually isn’t what you meant:

A heading that should start below the image

“Opening hours” belongs to a new section, but it sits next to the cup. Add clear: both; to the h3 rule and it moves down below the picture. clear: left only clears left floats and clear: right only right ones; both is the safe default.

The parent that collapses

A float is taken out of the normal flow, so its parent doesn’t count it when working out its own height. Put a floated picture in a bordered card with only a line of text, and the border shrinks around the text while the picture hangs out of the bottom:

A card that doesn’t contain its floated image

Add display: flow-root; to .card and the border wraps the whole picture again. That one line is the modern fix. It makes the card a new “block formatting context”, a fancy name for a box that keeps its floats inside. All current browsers support it.

Two ways to contain floats
/* the modern fix: one line */
.card {
  display: flow-root;
}

/* the old fix, still in a lot of stylesheets */
.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

The old clearfix adds an invisible, empty item at the end of the box and clears it, which pushes the bottom of the box down past the floats. You’ll see it in older themes and frameworks, and it still works. overflow: hidden or auto also contain floats, but they cut off anything that pokes out of the box, like shadows and dropdowns (the overflow tutorial shows what gets clipped).

Backgrounds that slide under a float

Floats push text aside, not boxes. A paragraph next to a float still starts at the left edge of the container; only its lines of text are shortened. You notice when the paragraph has a background:

A yellow note sliding under the picture

Look below the cup: the yellow and the orange border carry on to the far left edge. The box started behind the picture all along, even though the words start to the right of it. Add display: flow-root; to .note and the whole box moves beside the float, border, background and all.

float: right flips the order

Several items floated right line up from the right edge inwards, so the first one in the HTML ends up furthest right:

Links floated right

The HTML says Menu, Hours, Contact. The bar shows Contact, Hours, Menu, reading left to right. Screen readers and the Tab key still follow the HTML order, so the visual order and the reading order no longer match. This was a standard way to build nav bars once; today display: flex with justify-content: flex-end keeps the order and lines them up on the right (see the navbar tutorial).

Float layouts, and why flexbox replaced them

Before flexbox and grid, floats were the main tool for putting boxes side by side, so plenty of older sites still have columns made of floats:

A float layout and its grid version
/* how two columns were built with floats */
.main    { float: left;  width: 70%; }
.sidebar { float: right; width: 30%; }
.page::after { content: ""; display: table; clear: both; }

/* the same with grid */
.page {
  display: grid;
  grid-template-columns: 7fr 3fr;
  gap: 24px;
}

It worked, with a list of things to watch out for. Every row needed a clearfix. Columns couldn’t match each other’s height, so a sidebar background stopped short of the content next to it. Vertical centering wasn’t possible. And adding padding to a 70% and a 30% column made them too wide to fit, so the second one dropped below the first (which is why box-sizing: border-box became popular). Flexbox and grid solve all of these directly. For new layouts, use flexbox for a row of things and grid for rows and columns together.

Wrap text around a circle with shape-outside

Text normally wraps around a float’s rectangle, even when the picture is round. shape-outside gives the float a different shape for the text to follow:

Text that follows a round image

The lines next to the circle get longer towards its top and bottom and shortest at the middle, hugging the curve. Delete the shape-outside line to compare: the text then lines up in a straight edge, leaving empty corners. A few things it needs:

  • A float. On a box that isn’t floated, shape-outside does nothing.
  • A size. The circle is measured from the float’s box, so give it a width and height.
  • Room for a gap. shape-margin adds space around the shape, but the text can never come closer than the float’s own margin edge, so it’s easiest to start with a plain margin-right as above.

ellipse(), polygon() and even the transparent parts of a PNG work as shapes too. For the PNG version, shape-outside: url(…) only works with an image from your own site, or one served with CORS headers. Anything else is ignored without an error, and the text goes back to the rectangle.

A drop cap

Floats work on pseudo-elements, which makes the big first letter of a story easy:

A drop cap with ::first-letter

::first-letter picks the “O”, the float lets the text wrap beside it, and the small line-height stops it from taking up extra space. Getting it to line up exactly with the text lines takes some fiddling with the margin and font size. The newer initial-letter: 3 property does the alignment for you; Chrome and Edge supported it when this page was written, Safari only with a -webkit- prefix, and Firefox not yet, so keep the float version as the base.

When float does nothing

  • Inside a flex or grid container. The children are flex or grid items, and float is ignored on them. To push one item to the right in a flex row, give it margin-left: auto.
  • With position: absolute or fixed. Positioning wins and the float is dropped.
  • When the float comes after the text. It only affects what follows it in the HTML. Move the image above the paragraph.
  • When there’s no room. If the float plus the text’s minimum width don’t fit, the text moves below the float instead of beside it. On phones, a big floated image often leaves a thin strip of one or two words. Drop the float under a certain width with a media query.

One more you may see in newer code: float: inline-start and float: inline-end. They mean left and right in English but swap automatically for right-to-left languages like Arabic and Hebrew. Current browsers support them.

Questions people ask

How do I wrap text around an image in CSS?

Float the image and put it before the text in the HTML: img.photo { float: left; width: 140px; margin: 0 14px 8px 0; }. The paragraph that follows flows around the right side of the image. Use float: right and swap the margin to the left side to put the image on the right.

What does clear: both do?

It makes an element start below any floats before it, instead of wrapping beside them. Put it on a heading or section that should begin under a floated image: h3 { clear: both; }. clear: left and clear: right only clear floats on that one side.

Why does my container collapse when its children are floated?

Floats are taken out of the normal flow, so the parent doesn't include them when it works out its height. If all the children are floated, the parent shrinks to nothing and its border or background disappears. Add display: flow-root to the parent and it contains the floats again.

What is a clearfix?

An older trick to make a parent contain its floats: .clearfix::after { content: ""; display: table; clear: both; }. It adds an empty item at the end of the box that clears the floats, which pushes the box's bottom edge down. It still works, but display: flow-root on the parent does the same in one line.

Should I use float for page layout?

Not for new pages. Floats were the main way to build columns before flexbox and grid, and they needed clearfixes, couldn't make columns the same height and couldn't center things vertically. Use flexbox for a row of items and grid for rows and columns. Keep float for wrapping text around an image.

Why is float not working?

Check whether the element is inside a flex or grid container (float is ignored on their children), whether it has position: absolute or fixed (positioning wins), and whether the floated element comes before the text in the HTML. A float only affects content that follows it.

How do I wrap text around a circle?

Float a square element with a width and height, round it with border-radius: 50%, and add shape-outside: circle(50%). The lines of text then follow the curve instead of the square box. Add a margin on the text side for a gap; shape-margin alone can't push the text past the float's margin edge.

What is the difference between float and position: absolute?

A floated element moves to the left or right and the text around it makes room, flowing beside it. An absolutely positioned element is lifted out completely: nothing makes room for it and it can cover other content. Use float when text should wrap around something, and absolute for overlays like a badge on a card.

Tools that help