Master float and clear properties for wrapping text and creating layouts
The float property was originally designed to wrap text around images (like in magazines). While modern layouts use Flexbox and Grid, float is still useful for text wrapping and is important to understand for legacy code.
For modern layouts, prefer Flexbox or Grid over floats. However, floats remain the best choice for text wrapping around images.
The float property removes an element from normal
document flow and shifts it to the left or right, allowing content to
wrap around it.
/* Float to the left */
.left {
float: left;
}
/* Float to the right */
.right {
float: right;
}
/* No float (default) */
.none {
float: none;
}
/* Inherit from parent */
.inherit {
float: inherit;
}
Floats the element to the left side, allowing text and inline elements to wrap around its right side.
/* Classic example: Image with text wrapping */
img {
float: left;
margin-right: 20px;
margin-bottom: 10px;
width: 200px;
}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. The image floats to the left, and this text wraps around it naturally. This is how magazine layouts work, with images integrated into text content. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Floats the element to the right side, allowing text and inline elements to wrap around its left side.
/* Float image to right */
.profile-pic {
float: right;
margin-left: 20px;
margin-bottom: 10px;
width: 150px;
border-radius: 50%;
}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. The image floats to the right, and this text wraps around its left side. This creates an elegant layout where text flows naturally around visual elements. Ut enim ad minim veniam, quis nostrud exercitation.
Multiple floated elements stack next to each other horizontally until they run out of space.
/* Three-column layout with floats */
.column {
float: left;
width: 33.33%;
padding: 10px;
}
/* Four boxes side by side */
.box {
float: left;
width: 25%;
height: 100px;
}
When all children of a container are floated, the container's height collapses to zero because floated elements are removed from normal document flow.
Notice: The red container has collapsed (very thin height) because both children are floated.
The clear property controls how elements behave next to
floated elements. It forces an element to move below floated elements.
/* Clear left floats */
.clear-left {
clear: left;
}
/* Clear right floats */
.clear-right {
clear: right;
}
/* Clear both left and right floats */
.clear-both {
clear: both; /* Most commonly used */
}
/* No clearing (default) */
.clear-none {
clear: none;
}
The clearfix is a technique to fix the float collapse problem by making the container recognize its floated children.
/* Modern clearfix */
.clearfix::after {
content: "";
display: table;
clear: both;
}
/* Usage */
.container {
/* container will now contain floated children */
}
<div class="container clearfix">
<div class="float-left">...</div>
<div class="float-right">...</div>
</div>
/* Simple overflow fix */
.container {
overflow: auto; /* or overflow: hidden */
}
/* This creates a new block formatting context
which contains the floats */
/* Float the parent */
.container {
float: left;
width: 100%;
}
/* Parent contains floated children
but parent itself is floated */
Success! The green container now has proper height because we used overflow: auto.
/* Magazine-style article layout */
.article-image {
float: left;
width: 300px;
margin: 0 20px 20px 0;
border-radius: 8px;
}
.article-content {
text-align: justify;
}
.article-container::after {
content: "";
display: table;
clear: both;
}
/* Two columns with float */
.sidebar {
float: left;
width: 25%;
padding: 20px;
}
.main-content {
float: right;
width: 75%;
padding: 20px;
}
.container::after {
content: "";
display: table;
clear: both;
}
/* Card grid using floats */
.card {
float: left;
width: calc(33.33% - 20px);
margin: 10px;
padding: 20px;
background: white;
border: 1px solid #ddd;
}
.card-container::after {
content: "";
display: table;
clear: both;
}
/* Pull quote floated to side */
.pull-quote {
float: right;
width: 40%;
margin: 0 0 20px 20px;
padding: 20px;
background: #f5f5f5;
border-left: 4px solid #667eea;
font-style: italic;
font-size: 1.2em;
}
/* Buttons floated in a row */
.button-left {
float: left;
}
.button-right {
float: right;
}
.button-group::after {
content: "";
display: table;
clear: both;
}
/* Float for text wrapping (GOOD) */
img {
float: left;
margin-right: 20px;
}
/* Flexbox for navigation (BETTER) */
nav {
display: flex;
justify-content: space-between;
}
/* Grid for page layout (BETTER) */
.page {
display: grid;
grid-template-columns: 250px 1fr;
}
This is a demonstration of how floating works with text. The colored box on the left is floated, and this text wraps around it naturally. This technique is commonly used in blogs, articles, and magazine-style layouts where you want images integrated with text content. Notice how the text flows around the floated element smoothly.
Q1: What does the float property do?
Q2: What is the clearfix technique used for?
Q3: Which clear value clears both left and right floats?
Q4: What is the modern alternative to floats for layouts?
Q5: When should you still use floats today?
::after { content: ""; display: table; clear: both; }
overflow: auto on
parent contains floats