
Floats are useful for wrapping text around an image, but can cause confusing results when used as a layout system. Three recurring problems are a parent that appears too short, a following section that starts alongside a float, and floated items that no longer fit on one row.
1. The container does not enclose its floats
A normal block may ignore its floated children when calculating its height. Establish a new block formatting context on the parent with display: flow-root.
.figure-wrap { display: flow-root; }
.figure-wrap img { float: left; margin: 0 1rem 1rem 0; }2. The next section starts too early
If a following block should begin below an earlier float, give it clear: both, or use clear: left or right if only one side matters. This solves a different problem from containing floats inside their parent.
3. Floating columns no longer line up
Floats can fall to the next line when their combined widths, margins, borders, or padding exceed the available space. Inspect the computed sizes and check the narrowest viewport. For columns, consider Flexbox for a row or Grid for two-dimensional alignment.
4. The clearfix pattern
On a legacy layout, add a generated block after the content and clear it. Put the class on the parent that contains the floats.
.clearfix::after {
display: block;
clear: both;
content: "";
}Use the positioning guide to compare float, clear, and normal flow.