Mystery Of The CSS Float Property
When developers first started to make the transition to HTML layouts without tables, one CSS property that suddenly took on a very important role was the float property. The reason that the float property became so common was that, by default, block-level elements will not line up beside one another in a column-based format. Since columns are necessary in virtually every CSS layout, this property started to get used — and even overused — prolifically.
The CSS float property allows a developer to incorporate table-like columns in an HTML layout without the use of tables. If it were not for the CSS float property, CSS layouts would not be possible except using absolute and relative positioning — which would be messy and would make the layout unmaintainable
Definition and Syntax
The purpose of the CSS float
property is, generally speaking, to push a block-level element to the left or right, taking it out of the document flow. This allows naturally-flowing content to wrap around the floated element. This concept is similar to what you see every day in print literature, where photos and other graphic elements are aligned to one side while other content (usually text) flows naturally around the left- or right-aligned element.
The photo above shows the “Readers’ sites” section of .net magazine, which displays 3 readers’ photos each aligned left in their respective columns with text wrapping around the aligned images. That is the basic idea behind the float
property in CSS layouts, and demonstrates one of the ways it has been used in table-less design.
The effectiveness of using floats in multi-columned layouts was explained by Douglas Bowman in 2004 in his classic presentation No More Tables:
Bowman explained the principles behind table-less design, using Microsoft’s old site as a case study. Floats were used prominently in his mock overhaul of the Microsoft layout.
Syntax
The float
CSS property can accept one of 4 values: left
, right
, none
, and inherit
. It is declared as shown in the code below.
#sidebar { float: left; }
The most commonly-used values are left
and right
. A value of none
is the default, or initial float
value for any element in an HTML page. The value inherit
, which can be applied to nearly any CSS property, does not work in Internet Explorer versions up to and including 7.
The float
property does not require the application of any other property on a CSS element for float
to function correctly, however, float
will work more effectively under specific circumstances.
Generally, a floated element should have an explicitly set width (unless it is a replaced element, like an image). This ensures that the float behaves as expected and helps to avoid issues in certain browsers.
#sidebar { float: left; width: 350px; }
Specifics on Floated Elements
Following is a list of exact behaviours of floated elements according to CSS2 Specifications:
- A left-floated box will shift to the left until its leftmost margin edge (or border edge if margins are absent) touches either the edge of the containing block, or the edge of another floated box
- If the size of the floated box exceeds the available horizontal space, the floated box will be shifted down
- Non-positioned, non-floated, block-level elements act as if the floated element is not there, since the floated element is out of document flow
- Margins of floated boxes do not collapse with margins of adjacent boxes
- The root element (
<html>
) cannot be floated - An inline element that is floated is converted to a block-level element
Float in Practice
One of the most common uses for the float
property is to float an image with text wrapping it. Here is an example:
The CSS applied to the image in the box above is as follows:
img { float: left; margin: 0 15px 5px 0; border: solid 1px #bbb; }
The only property required to make this effect work is the float
property. The other properties (margin and border) are there for aesthetic reasons. The other elements inside the box (the <p>
tags with text inside them) do not need any styles applied to them.
As mentioned earlier, floated elements are taken out of document flow, and other block elements remain in flow, acting as if the floated element is not even there. This is demonstrated visually below:
In the above example, the <p>
element is a block-level element, so it ignores the floated element, spanning the width of the container (minus padding). All non-floated, block-level elements will behave in like manner.
The text in the paragraph is inline, so it flows around the floated element. The floated box is also given margin settings to offset it from the paragraph, making it visually clear that the paragraph element is ignoring the floated box.