CSS Float

The CSS float property is used to position an element to the left or right of its container, allowing other content to wrap around it. It is commonly used for layout purposes, such as wrapping text around images.

Understanding the Float Property

The float property can take the following values:

  • left: Floats the element to the left of its container, allowing other content to wrap around its right side.
  • right: Floats the element to the right of its container, allowing other content to wrap around its left side.
  • none: The default value. The element does not float and will be displayed in the normal flow of the document.
  • inherit: Inherits the float value from its parent element.

Syntax

The syntax for the CSS float property is:


/* Float Property Syntax */
selector {
    float: ;
}

        

Examples

Example 1: Floating Left


/* Float Left */
.float-left {
    float: left;
    width: 150px;
    margin-right: 10px;
    background: #f0f0f0;
}

            
This element is floated to the left.

This text wraps around the floated element. Notice how the text flows around the box that is floated to the left.

Example 2: Floating Right


/* Float Right */
.float-right {
    float: right;
    width: 150px;
    margin-left: 10px;
    background: #f0f0f0;
}

            
This element is floated to the right.

This text wraps around the floated element on the right side. Notice how the text flows around the box that is floated to the right.

Example 3: Clearing Floats


/* Clear Floats */
.clearfix::after {
    content: "";
    display: table;
    clear: both;
}

            
This box floats to the left.
This box floats to the right.
This box uses clearfix to clear the floats and start on a new line.