On this page
Float & Clear (Legacy, and Why to Avoid It)
CSS Float and Clear: Legacy Layout and Modern Alternatives
Focus keyword: CSS float and clear. Before flexbox and grid, float was the main tool for building multi-column layouts. Today it is considered legacy for layout, but it is still useful for one job: wrapping text around an image. In this lesson you will learn how float and clear work, why float layouts cause problems, how to fix them with flow-root and clearfix, and what to use instead.
Trending keywords covered in this lesson
CSS float, clear both, clearfix hack, display flow-root, float vs flexbox, float vs grid, shape-outside, text wrap around image, collapsed parent, float legacy layout, modern CSS layout, legacy code maintenance, initial-letter drop cap, float inline-start, logical float.
1. What Is Float?
The float property pushes an element to the left or right edge of its container. The element is taken out of normal block flow, and inline content (text) wraps around it. Values: left, right, none (default), plus the logical values inline-start and inline-end that adapt to writing direction.
- A floated element becomes block-level automatically.
- Floats need an explicit width (or a replaced element like an image), otherwise they shrink to fit.
- Floats only move horizontally. There is no vertical float.
- Margins of floats never collapse.
2. What Is Clear?
The clear property tells an element to move below earlier floats. Values: left, right, both, none, and inline-start or inline-end. Use clear: both on a footer or on an empty element to force it under all floats.
3. The Collapsed Parent Problem
Since floated children leave the normal flow, a parent containing only floats has a height of 0. Its border and background collapse and following content wraps awkwardly. This was the famous float bug of the old web.
Fix 1: display: flow-root (modern)
Add display: flow-root to the parent. It creates a block formatting context that contains floats with no side effects.
Fix 2: Clearfix hack (legacy)
Add an empty generated element at the end of the parent: .clearfix::after { content: ''; display: table; clear: both; }. You will still see this in older projects.
Fix 3: overflow
Setting overflow: hidden or auto on the parent also contains floats, but it can clip shadows and dropdowns.
4. Why You Should Avoid Float for Layout
- Fragile: layouts break when content is taller, shorter or wider than expected.
- Needs hacks: clearfix, fixed widths and negative margins.
- No real alignment: vertical centering and equal height columns are hard.
- Not responsive by nature: columns need many media queries.
- Source order problems: float right reverses visual order, which confuses keyboard and screen reader users.
- Better tools exist: flexbox for one-dimensional rows and columns, grid for two-dimensional layouts.
5. The Legitimate Use: Text Wrap Around Images
Flexbox and grid cannot make text flow around a shape. Float can. Combine it with shape-outside and shape-margin to wrap text around a circle or a polygon, which is a popular editorial design trend. Give the floated element a width, a margin on the side facing the text and, for shapes, a matching clip-path or border-radius.
6. Float vs Flexbox vs Grid
- Float: text wrap, legacy code.
- Flexbox: navigation, toolbars, card rows, centering, equal height.
- Grid: page layouts, galleries, dashboards, responsive card grids.
7. Migrating Legacy Float Code
- Find the float container and replace clearfix with
display: flow-rootor convert it to flex. - Replace percentage widths plus float with
display: flexandflex: 1, or withgrid-template-columns. - Remove negative margin hacks and use
gap. - Test on small screens and with zoom.
8. Common Mistakes
- Forgetting to clear floats, so the footer climbs up beside the content.
- Clearing with an extra empty div in HTML.
- Using float and flex on the same element (float is ignored on flex items).
- Floating an element without width.
- Using float to build a complete layout in new projects.
Key Takeaways
Float wraps content around an element, and clear pushes content below floats. Contain floats with display: flow-root, keep float only for text wrapping and shapes, and use flexbox and grid for layouts.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Float the avatar to the left with a 16px right margin so the paragraph wraps around it. Make the avatar a circle and use shape-outside so the text hugs the curve.
Press Run to execute.
Show expected output
Avatar floats left, is circular and text wraps along the circle using shape-outsideThis is a self-check — compare your result with the expected output above.
Rewrite this two column float layout using display: flex and gap. Remove the clearfix and the fixed percentage widths.
Press Run to execute.
Show expected output
Row uses display: flex with gap, columns use flex: 1, no float and no clearfixThis is a self-check — compare your result with the expected output above.
Was this page helpful?