The CSS float Property

The float property specifies how an element should float within its container.

It places an element on the left or right side of its container, allowing text and inline elements to wrap around it.

The float property can have one of the following values:

Tip: The float property is often used to wrap text around images!


CSS float: right Example

The float: right value indicates that an element should float to the right within its container.

The following example specifies that the image should float to the right.

Tip: If you make the screen smaller, you will see that the text wraps around the image.

CSS float: left Example

The float: left value indicates that an element should float to the left within its container.

The following example specifies that the image should float to the left:

CSS float: none Example

The float: none value is the default value for float, and the element is displayed just where it occurs in its container.

In the following example the image will be displayed just where it occurs in the container:

The CSS clear Property

The clear property specifies what should happen with the element that is NEXT to a floating element.

The clear property prevents elements from wrapping around or beside the floated content.

This property can have one of the following values:

Example

Here, clear: left; pushes div2 down below the floating div1, on its left side:

The CSS Clearfix Hack

If a floated element is taller than the containing element, it will “overflow” outside of its container. We can then add a clearfix hack to solve this problem:

Images Side By Side

The grid of boxes can also be used to display images side by side:

Navigation Menu

You can also use float with a list of hyperlinks to create a horizontal menu: