The float
property in CSS is used to position an element to the left or right within its container, allowing text and inline elements to wrap around it. Originally, float
was intended for wrapping text around images, but it can be used for more complex layouts as well.
float Property Values
float: left
The element is floated to the left of its container, and content will wrap around it on the right.
<div style="float: left; width: 200px; background-color: lightblue; margin-right: 10px;"> This div is floated to the left. </div> <p> This text will wrap around the floated element. The floated element stays to the left, and the text flows around it on the right side. </p>
float: right
The element is floated to the right of its container, and content will wrap around it on the left.
<div style="float: right; width: 200px; background-color: lightgreen; margin-left: 10px;"> This div is floated to the right. </div> <p> This text will wrap around the floated element. The floated element stays to the right, and the text flows around it on the left side. </p>
float: none
The element does not float. This is the default value.
<div style="float: none; background-color: lightcoral;">
This div does not float.
</div>
Clearing Floats
When elements are floated, it can sometimes cause layout issues with other elements on the page, especially when the container doesn't wrap around the floated elements as expected.
The clear
property is used to prevent these issues by moving the next element below the floated elements.
clear
Property Values
clear: left
The element cannot be positioned next to floated elements on the left side and will move down below them.
<div style="float: left; width: 200px; background-color: lightblue;"> Floated left </div> <div style="clear: left; background-color: lightgray;"> This element is cleared to the left, so it moves below the floated element. </div>
clear: right
The element cannot be positioned next to floated elements on the right side and will move down below them.
<div style="float: right; width: 200px; background-color: lightgreen;"> Floated right </div> <div style="clear: right; background-color: lightgray;"> This element is cleared to the right, so it moves below the floated element. </div>
clear: both
The element cannot be positioned next to floated elements on either the left or right side, and it will move below any floated elements.
<div style="float: left; width: 200px; background-color: lightblue;"> Floated left </div> <div style="float: right; width: 200px; background-color: lightgreen;"> Floated right </div> <div style="clear: both; background-color: lightgray;"> This element is cleared on both sides, so it moves below both floated elements. </div>
A common method to clear floats is by using a "clearing" element.
This is an empty element with the clear: both;
property applied to it.
<div style="float: left; width: 200px; background-color: lightblue;"> Floated left </div> <div style="float: right; width: 200px; background-color: lightgreen;"> Floated right </div> <div style="clear: both;"></div> <!-- This is the clearing element --> <p> This paragraph appears below the floated elements because of the clearing element. </p>
Clearing Floats with CSS Classes
Another way to clear floats is by using a utility class like .clearfix
, which can be added to the container to automatically clear its floated children.
<style> .clearfix::after { content: ""; display: table; clear: both; } </style> <div class="clearfix"> <div style="float: left; width: 200px; background-color: lightblue;"> Floated left </div> <div style="float: right; width: 200px; background-color: lightgreen;"> Floated right </div> </div> <p> This paragraph appears below the floated elements because the `.clearfix` class clears the floats. </p>
Using floats and clearing them correctly is essential for maintaining a well-structured and visually appealing layout.