Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS Float (Floating)

CSS Float (floating)


What is CSS Float?

CSS Float moves an element to the left or right, and the elements around it will be rearranged as well.

Float is often used with images, but it is also very useful in layout.


How elements float

An element floating horizontally means it can only move left or right, not up or down.

A floating element will move as far left or right as possible until its outer edge touches the containing box or the border of another floating box.

Elements after a floating element will flow around it.

Elements before a floating element will not be affected.

If an image floats to the right, the text flow below will wrap around its left side:

Example

img

{

float:

right

;

}

Try it »


Floating elements next to each other

If you place several floating elements together, they will sit next to each other if there is space.

Here, we use the float property for the image gallery:

Example

.thumbnail

{

float:

left

;

width:

110

px

;

height:

90

px

;

margin:

5

px

;

}

Try it »


Clearing floats - using clear

After an element floats, the surrounding elements are rearranged; to avoid this, use the clear property.

The clear property specifies that no floating elements are allowed on either side of an element.

Add an image gallery to text using the clear property:

Example

.text

_

line

{

clear:

both

;

}

Try it »


More examples

Add a border and margin to an image and float it to the right of a paragraph

Let's add a border and margin to the image and float it to the right of the paragraph

Float headings and images to the right

Float the heading and image to the right.

Float the first letter of a paragraph to the left

Change the style so that the first letter of the paragraph floats to the left.

Create a webpage without tables

Use float to create a webpage header, footer, left content, and main content.


All floating properties in CSS

The numbers in the "CSS" column indicate the CSS version (CSS1 or CSS2) that defines the property.

property Description Value CSS
clear Specifies that no floating elements are allowed around the element. left
 right       
 both       
 none       
 inherit | 1 |

| float | Specifies whether a box (element) can float. | left
right
none
inherit | 1 |