CSS Floats
CSS Floats
What are floats in CSS, and what can you use them for? This tutorial walks you through this
essential CSS topic, with examples of usage.
A float is simply a box (for example, a div) that is shifted to the left side or the right side of its
container. We then say that the box is floated left or floated right.
One of the key benefits of floats is that they allow you to have bits of content sitting side by side,
rather than one below the other (much like you can do with table columns, but better!). This
allows you to do cool stuff like text columns, boxouts (like the one above), and advanced
positioning of your page elements.
Floats are a bit like the align="left" and align="right" attributes in img elements.
.
.
.
#leftFloat
{
float: left;
width: 100px;
}
#rightFloat
{
float: right;
width: 100px;
}
A left-floated boxout
Here we have a regular (non-floated) column of text, with a left-floated box on the left that the
regular text wraps around.
<p>
This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
</p>
This is our regular, non-floated column of text. This is our regular, non-floated column of text.
This is our regular, non-floated column of text. This is our regular, non-floated column of text.
This is our regular, non-floated column of text. This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
A right-floated boxout
This is similar to the last example, but this time the box is floated right rather than left.
<div style="float: right; width: 100px;
margin: 5px; padding: 5px; border: 1px solid black;">
<p>This is our right-floated text box.</p>
</div>
<p>
This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
</p>
This is our regular, non-floated column of text. This is our regular, non-floated column of text.
This is our regular, non-floated column of text. This is our regular, non-floated column of text.
This is our regular, non-floated column of text. This is our regular, non-floated column of text.
This is our regular, non-floated column of text.
In this example, we use two floats side-by-side to create two columns of text. We use two left-
floated divs to do this (not a left float and a right float as you might expect). The second floated
div sits to the right of the first floated div.
This is our left text column. This is our left text column. This is our left text column.
This is our right text column. This is our right text column. This is our right text column. This is
our right text column. This is our right text column.
The clear property is a way of saying "make sure this element does not run alongside a previous
float". By putting a div with clear: both after our floated columns, we're saying "don't run this
div, or anything after it, alongside our floated columns".
Without having an element with clear: both after our floated columns, all later content on the
page would attempt to flow alongside the columns. (Try it yourself and see what happens!) You
could say that we're telling the browser, "the floats end here".
Possible values for the clear property are: left (don't run alongside any left-floated boxes),
right (don't run alongside any right-floated boxes), both (don't run alongside any left- or right-
floated boxes), and none (do nothing). When in doubt, use both.