Codectionary / Developer documentation / CSS

float and clear

float was CSS's original tool for wrapping text around an image or building multi-column layouts, pulling an element to one side while other content flows around it. clear stops an element from wrapping around floated siblings. Flexbox and Grid have replaced float for layout purposes, but float is still legitimately used for wrapping text around images.

Syntax

float: left | right | none;
clear: left | right | both;

Examples

Wrapping Text Around an Image

The use case float remains genuinely good for today.

img.inline-photo {
  float: left;
  margin: 0 16px 8px 0;
  width: 200px;
}
/* Paragraph text will wrap around the right and bottom of the image */

clear to Stop Wrapping

Preventing an element from sitting beside a float.

.footer {
  clear: both; /* moves below any floated elements, regardless of side */
}

Parameters and values

  • float (left | right | none): Pulls an element to one side, letting content wrap around it
  • clear (left | right | both | none): Prevents an element from sitting beside floated elements

Best practices

  • Use Flexbox or Grid instead of float for building page layouts and component structure - that is what they were designed to replace
  • Reserve float genuinely for wrapping text around an image or similar inline content, which is still its best use case today
  • Remember a floated element is taken out of normal flow, which can cause its parent to collapse to zero height unless cleared or given overflow: auto
  • Avoid float-based grid systems (common in older frameworks) in new projects - modern layout tools are simpler and more capable

At a glance

Purpose
Presentation and layout
File extension
.css
Runs in
Web browsers
Usually used with
HTML and JavaScript

Specifications & further reading

Related CSS documentation