CSS Float

The float property in CSS is used for positioning and formatting content. It allows you to float an element to the left or right within its container, with surrounding text and inline elements wrapping around it.

Values of the Float Property

The float property can take the following values:


Basic Usage

Here are some examples demonstrating how to use the float property:


Float Left
Try yourself
        
            .float-left {
    float: left;
    width: 150px;
    height: 150px;
    margin: 10px;
    background-color: lightblue;
}
.content {
    overflow: auto;
}
        
    

In this example, the <div> element will float to the left, and the text will wrap around it.


Float Right
Try yourself
        
            .float-right {
    float: right;
    width: 150px;
    height: 150px;
    margin: 10px;
    background-color: lightgreen;
}
.content {
    overflow: auto;
}
        
    

In this example, the <div> element will float to the right, and the text will wrap around it.


Clearing Floats

Clearing floats is an important aspect to manage the layout correctly when using the float property. Use the clear property to specify whether an element can be next to floated elements or if it should be moved down.

Try yourself
        
            .clear-both {
    clear: both;
}
        
    

In this example, the element will clear both left and right floats, ensuring it appears below any floated elements.


Practical Tips and Notes

Here are some practical tips and important notes to keep in mind when using the float property: