CSS Float
Learn CSS float property for wrapping text around images and creating multi-column layouts (legacy technique).
The float property was historically used for layouts but is now primarily used for wrapping text around images. Modern layouts use Flexbox and Grid.
Float Basics
.float-left {
float: left;
margin-right: 20px;
}
.float-right {
float: right;
margin-left: 20px;
}
.no-float {
float: none; /* Default */
}Text Wrap Around Image
.article-image {
float: left;
width: 300px;
margin: 0 20px 20px 0;
}
.article-image-right {
float: right;
width: 300px;
margin: 0 0 20px 20px;
}Clearing Floats
Clear Property
.clear-left {
clear: left; /* Clear left floats */
}
.clear-right {
clear: right; /* Clear right floats */
}
.clear-both {
clear: both; /* Clear all floats */
}Clearfix
/* Modern clearfix */
.clearfix::after {
content: "";
display: table;
clear: both;
}
/* Alternative */
.container {
overflow: auto;
}Float Layouts (Legacy)
/* Two-column layout */
.sidebar {
float: left;
width: 30%;
}
.main {
float: right;
width: 65%;
}
.container::after {
content: "";
display: table;
clear: both;
}Common Issues
Parent Collapse
/* Problem: floated children cause parent to collapse */
.parent {
/* Solution 1: clearfix */
}
.parent::after {
content: "";
display: table;
clear: both;
}
/* Solution 2: overflow */
.parent {
overflow: auto;
}Margin Collapse
.floated {
float: left;
/* Margins don't collapse on floated elements */
margin: 20px;
}Best Practices
Float Usage
Modern Approach: Use Flexbox or Grid for layouts instead of float.
Use float for:
- Wrapping text around images
- Legacy browser support
Avoid float for:
- Page layouts (use Flexbox/Grid)
- Centering elements
- Equal-height columns
Practical Example
.article {
max-width: 800px;
margin: 0 auto;
}
.article img {
float: left;
max-width: 40%;
margin: 0 20px 20px 0;
border-radius: 8px;
}
.article::after {
content: "";
display: table;
clear: both;
}Float is primarily a legacy technique - use modern layout methods for new projects!
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on
CSS Positioning
Master CSS positioning to precisely control element placement using static, relative, absolute, fixed, and sticky positioning.
CSS Overflow
Control how content behaves when it's too large for its container using CSS overflow properties for better content management.
© 2026CoderrShyamAll Rights Reserved.