The float and clear properties are part of CSS's early layout system used to wrap text around images and create multi-column designs before flexbox and grid. The float property allows elements to shift to the left or right of their container, while the clear property prevents elements from wrapping around floated elements. Floats still appear in legacy layouts and can break layout flow; test clearing and responsive wrapping.
What is the purpose of the float and clear properties?
Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.
Overview
Originally designed for print-style text wrapping, float and clear were later repurposed for web layouts before modern techniques like flexbox and grid. float moves elements to one side of their container, allowing text and inline elements to flow around them. clear ensures that subsequent elements do not wrap around floated elements.
Property | Values | Purpose |
|---|---|---|
float | left | right | none | inline-start | inline-end | Moves an element to one side, allowing surrounding content to wrap around it. |
clear | left | right | both | none | Prevents elements from appearing next to floated elements on specified sides. |
Example: Floating an Image
img {
float: right;
margin: 10px;
}
p {
text-align: justify;
}
In this example, the image floats to the right, and text flows naturally around it, similar to magazine-style layouts.
Example: Clearing Floats
.footer {
clear: both;
background: #e2e8f0;
padding: 10px;
}
This ensures the footer starts below all floated elements, rather than wrapping beside them.
Common Issue: Collapsing Parent Containers
Floated child elements are removed from normal document flow, which may cause parent containers to collapse in height. This can be fixed using a clearfix method:.clearfix::after { content: ''; display: block; clear: both; }
Modern Alternatives
- Use
display: flex;for horizontal alignment and responsive layouts. - Use
floattoday primarily for text wrapping around media, not layout structure.
Practical scenario
Legacy content uses floats for image alignment next to text.
Common pitfalls
- Parent collapsing because floats are removed from flow.
- Clearfix hacks missing in nested layouts.
- Mixing floats with flex/grid causing unpredictable results.
Floats are legacy; prefer flex/grid. Test old layouts for wrapping and clearing.
float is like placing a photo in a newspaper column — text wraps around it. clear tells the next paragraph, “don’t sit beside the photo; start below it.”
floatpositions elements left or right, letting text wrap around them.clearstops wrapping on specified sides.- Useful for images, text flows, and minor alignments — not complex layouts.
- Modern layouts prefer flexbox or grid for greater control.