Cascading Stylesheets and You (4)

CSS Positioning

Positioning

CSS Positioning

CSS 2.1 has three positioning schemes:

Normal Flow Includes

Block Formatting

Inline Formatting

Relative Positioning

Absolute Positioning

Absolute Positioning

Fixed Positioning

Floats

A few things to keep in mind:

Floats: A Visual Journey

Here is a basic normal flow with no elements floated:

Normal Flow

Floats: A Visual Journey

Floated elements shift to the right or left on the current line:

Normal Flow

The top of the floated object is aligned to the top of the current line box.

Floats: A Visual Journey

Floated elements are shifted until its outer edge meets the containing block edge...

Normal Flow

Floats: A Visual Journey

...or until its outer edge meets the outer edge of another float.

Normal Flow

Floats: A Visual Journey

Notice how the containing block does not grow to enclose the floated block...

Normal Flow

Floats: A Visual Journey

...unless of course your name is Microsoft Internet Explorer for Windows:

Normal Flow

(Previous image rendered in Firefox, current image rendered in Internet Explorer 6).

Floats: A Visual Journey

If there is not enough horizontal space, floated boxes are shifted down until there is room, or there are no more floats.

Normal Flow

Floats: A Visual Journey

Line boxes flow around floated boxes on the opposite side to which the box is floated.

Normal Flow

Floats: A Visual Journey

div { clear: left; }

Floats: A Visual Journey

A box with no clear property:

Normal Flow

Floats: A Visual Journey

The same box with a clear value of left.

Normal Flow

Examples

Lets take a look at some examples.