Full answer
A float pushes an element to the left or right edge of its container. The element leaves normal flow in one way: surrounding inline content, such as text, wraps around it. Block boxes behind it still use the full width and sit underneath.
Floats were designed for putting an image inside a paragraph, like a photograph in a newspaper column. For years they were also used to build whole page layouts, which is why many old stylesheets are full of them.
Two side effects cause most bugs.
- A parent does not grow to contain its floated children, so its height can collapse to zero.
- A later element may be pulled up beside the float.
clear: left,rightorbothon that element makes it move down below the float.
Today, display: flow-root on the parent contains floats cleanly, and Flexbox and Grid handle layout. Floats remain the right tool for wrapping text around an image. shape-outside can even make the text follow a curve.
In the first card the float is taller than the text, and since the card does not contain it, the card is only about as tall as the one line of text plus its padding and border. The second card uses flow-root and grows to hold the 120px photo, plus its margin, padding and border. In the third card, the paragraph wraps beside the photo, and the footer, which has clear: both, starts below it.
<style>
body { margin: 0; font: 16px/1.5 sans-serif; width: 600px; }
.card { border: 2px solid #333; padding: 8px; }
.card.contained { display: flow-root; }
.photo { float: left; width: 120px; height: 120px; margin: 0 12px 8px 0; background: #9cf; }
.footer { clear: both; background: #eee; }
</style>
<div class="card" id="plain"><div class="photo"></div><p>Short note.</p></div>
<div class="card contained" id="fixed"><div class="photo"></div><p>Short note.</p></div>
<div class="card">
<div class="photo" id="p3"></div>
<p>Students of the evening batch can collect their identity cards from the front office between ten and four. Please carry the fee receipt with you.</p>
<div class="footer" id="foot">Office hours</div>
</div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Float removes the element from normal flow but text wraps around it.
- A parent with only floated children collapses unless it is a BFC.
- clear: both on a later element moves it below the float.
- display: flow-root contains floats without extra markup.
- Use Flexbox or Grid for layout and float only for text wrapping.
Common mistakes
- using clear on the float itself. Clear applies to the element that should move below the float.
- adding an empty div with clear: both to contain floats. flow-root on the parent does the job without extra markup.
Follow-ups
- what happens to display on a floated element?
- What is a clearfix and why is it needed less today?
- How does shape-outside change a float?
Tips
- For modern layouts, reach for flex or grid first. Use float only when the content should wrap around something.
- Describe the newspaper image use case; it frames floats as a tool with a purpose.
- Give flow-root as the modern fix before the clearfix.