CSS float
Property
The float
property in CSS positions elements by making them float to the left or right within a container. It is commonly used for text wrapping, layouts, and image positioning.
1. Syntax
Value | Description |
---|---|
left | Floats the element to the left of its container. |
right | Floats the element to the right of its container. |
none | Default value; the element does not float. |
inherit | Inherits the float value from its parent. |
2. Basic Usage
✅ Floating an Image to the Left
✅ The image floats to the left, and the text wraps around it.
✅ Floating an Image to the Right
✅ The image floats to the right, with text wrapping around it.
3. Floating Multiple Elements for Layouts
✅ Creating a Two-Column Layout
✅ Creates a two-column layout using float
.
4. Clearing Floats (clear
Property)
Floating elements can affect the layout of surrounding elements. Use clear
to fix layout issues.
✅ Clearing Floats with clear: both;
✅ This prevents overlapping or collapsing issues.
5. Best Practices & Alternatives
✔ Use flexbox
or grid
for modern layouts instead of float
.
✔ Use float
only for text wrapping or legacy layouts.
✔ Always clear floats to avoid layout issues.