CSS Float
The CSS float property is used to position an element to the left or right of its container, allowing text or inline elements to wrap around it. It is commonly used for creating layouts, floating images, or aligning content within a container.
Key Points on CSS Float:
- The
float
property accepts values:left
,right
,none
, andinherit
. - When an element is floated, it is removed from the normal document flow.
- Floated elements require proper
clear
handling to avoid layout issues with following content. - The
clear
property prevents content from flowing around floated elements. - Float is often used with
width
to control the size of the floated element. - Floated containers may collapse if their child elements are floated; use
clearfix
to handle this. - Flexbox and CSS Grid are modern alternatives to float for layout purposes but float is still useful for certain designs.
Property | Value | Description |
---|---|---|
clear | left, right, both, none, inherit | The clear property is used to avoid elements after the floating elements which flow around it. |
float | left, right, none, inherit | It specifies whether the box should float or not. |
none | Value of float | It specifies that the element is not floated, and will be displayed just where it occurs in the text. This is a default value. |
left | Value of float | It is used to float the element to the left. |
right | Value of float | It is used to float the element to the right. |
initial | Value of float | It sets the property to its initial value. |
inherit | Value of float | It is used to inherit this property from its parent element. |
Syntax for CSS Float:
Syntax Example
/* Float syntax */
.element {
float: left | right | none | inherit;
}
/* Example */
.img {
float: left;
margin: 10px;
}
Examples of CSS Float:
The following examples demonstrate the usage of the float property:
Code Example: Float Left
.img {
float: left;
margin: 10px;
width: 150px;
}
Output

This image is floated to the left, and the text wraps around it.
Code Example: Float Right
.img {
float: right;
margin: 10px;
width: 150px;
}
Output

This image is floated to the right, and the text wraps around it.
Code Example: Clearfix for Floated Elements
.clearfix::after {
content: "";
display: table;
clear: both;
}
Output

Content is wrapped around a floated image. The clearfix ensures the container accounts for the floated element.
Common Float-Related Properties:
- float: Determines whether the element should float to the left, right, or not at all.
- clear: Specifies on which sides (left, right, or both) other elements cannot float.
- width: Defines the width of the floated element.
- margin: Adds spacing around the floated element to separate it from other content.
- clearfix: Ensures that parent containers account for floated child elements.
The float property provides flexibility in aligning content, but it should be used judiciously in modern layouts. Alternatives like Flexbox and Grid offer better solutions for complex designs.