Floats & Wrapping Text

Floats were designed to let text wrap around images, like in a magazine. For years they were also (mis)used to build whole page layouts. Today Flexbox and Grid handle layout, but floats are still the right tool for wrapping text — and understanding them helps with older code.

Floating an image

Try it yourself
Result

float: left or right moves an element to one side and lets inline content (text) wrap around it. The margin keeps text from touching the image.

Clearing floats

Try it yourself
Result

clear: left, right or both makes an element start below any floats on that side.

Containing floats

Try it yourself
Result

Floats don't count towards their parent's height, so a container with only floated children collapses. display: flow-root fixes this cleanly. (Older code uses the "clearfix" hack, a ::after pseudo-element with clear: both.)

Wrapping text around shapes

Try it yourself
Result

shape-outside makes text wrap around a circle, ellipse, polygon or image transparency instead of the float's rectangle.

Drop caps with initial-letter

Try it yourself
Result

Don't use floats for layout

Float-based grids needed clearfixes, careful widths and negative margins, and broke easily. Flexbox and Grid make columns, equal heights and vertical centring simple:

Try it yourself
Result

Try it yourself

Try it yourself
Result