Tutorials Bugs Masterclass Free stuff Test pages Proposals

Basic concepts

Advanced concepts

RichInStyle.com CSS2 tutorial - Positioning

Contents

Positioning schemes

Floats

Clear

Positioning

Absolute positioning
Relative positioning
Specifying the offsets of elements

The meaning of auto

Relative positioning

Absolute (including fixed) positioning

Containing blocks and absolutely positioned elements

What is the containing block?

Support
Positioning examples

Fixed example

Relative example

Absolute example

Positioning schemes

There are 3 positioning schemes in CSS 2. Firstly, normal flow, which includes block elements, inline elements, list-items, etc., and the positioning of these elements relative to their ordinary positions; secondly, floats, and thirdly, absolute positioning, which positions content relative to a fixed point.

Floats

The float property alters the normal behavior of an element. For example, it can be used to create a drop cap effect.

Possible values for the float property are left, right and none, where none is the initial value. The effect of setting float is similar to making the element part of the specified margin - float: left will cause it to be part of the left margin. Subsequent non-floated block elements to the float treat it as though it wasn't there, and thus overlapping results. However, inline content (such as text inside the float) will wrap around the float.

Floated block elements do, however, take the position of floats into account. E.g.:

<div style="height: 100px; width: 100px; border: solid; float; left">
</div>
<div style="height: 100px; width: 100px; border: solid; float; left">
</div>

That would be rendered thus (except without the space between the floats):

+---+ +---+
|   | |   |
|   | |   |
+---+ +---+

Here's an example showing how inline content flows around floats:

<P>
  <SPAN style="float: left; font-size: 72px; line-height: 72px; width: 50px">T</SPAN>he cat sat on the mat<BR> again <BR>once more. </P>
<P>
  New paragraph flows round it and continues after it.
</P>

---------------he cat sat on the mat
        |      again
        |      once more
        |
        |
        |      New paragraph flows round it
and continues after it.

Note how the top of the floating T is aligned with the top of the line, and how the next block element continues to wrap around it.

SPAN.dropcap {float: left;
font-size: 48px; width: 30px; line-height: 48px}

The above example can be used to create a drop cap.

Float applies to all elements and is not inherited.

All browsers support float buggily, and IE 3 not at all.

Clear

The clear property prevents an element from flowing round a floating element

Possible values for clear are left (preventing the element from starting (i.e., a cleared element can contain floating elements, but it will not flow round floating ones) around a left floated element), right (preventing the element from starting around a right floated element), both (preventing an element from starting around a floated element), or none (allowing floating on both sides) - the initial value.

To clear the element, the element is moved down below the float.

Clear applies only to block elements and is not inherited.

Note that clear does not prevent floating inside the element - if <P style="clear: left">Some text<IMG style="float: left"> then the image would float normally, but if <IMG style="float: left"><P style="clear: left">Some text</P>, then P would start below the image - clear only relates to whether an element can start in a given position.

IE 3 does not support clear, and all other browsers have problems with it, so test carefully.


Positioning

In CSS positioning, an element's position is specified relative to a reference point.

Generated content may not be positioned. Positioning properties are not inherited.

In order to specify the position of elements, four properties are used. These properties are top, left, right and bottom.

To specify the type of positioning that is desired, the position property is used. This property takes four values: static, absolute, fixed and relative.

Position: relative specifies that the element will be relatively positioned, position: absolute that it will be absolutely positioned, position: static that the element will flow normally, and position: fixed that the element will be fixed with respect to the document window or viewport.

It is important to note that positioned elements cannot be floated, and therefore that position declarations other than static will override float declarations.

In addition, positioned elements do not collapse margins.

Absolute positioning

In absolute positioning, the position of an element is specified relative to edges of a box known as a containing block.

In addition, unlike in static positioning, boxes are removed from normal flow entirely, and as such, they do not affect the position of other elements. Ordinary absolute positioning is of little use except for scripting purposes, where the fact that it does not cause reflow of content makes it ideal.

Fixed positioning allows you to create such effects as frame-style navigation or adbars in a fixed place.

Note that all absolutely positioned elements are block-level elements, and therefore display: inline declarations have no effect.

Relative positioning

In relative positioning, elements are moved relative to their normal position, but with subsequent elements treating them as though they were in their original position.

Specifying the offsets of elements

There are four properties that can be used to specify the offsets of positioned elements:

  1. top
  2. bottom
  3. left
  4. right

The values for these properties can be specified as a length, % (relative to the height of the containing block, in the case of top and bottom, and to the width in the case of left and right). Alternatively auto can be specified (initial value). These properties are not inherited and apply only to positioned elements.

If a % is specified for or top bottom, but the containing block's height is unknown (i.e., it is not explicitly specified (using the height property) or fixed (as the height of the viewport is)), it is ignored and treated is auto.

For example, top: 100px specifies an offset of 100 pixels.

The meaning of auto

The auto value places the element in the position it would normally be. Thus for relatively positioend elements 'auto' will be treated as 0. For absolutely positioned elements, it is interpreted as the value necessary to place the element in the position that it would be in but for the absolute positioning.

Relative positioning

Top specifies how much the element is moved down from its normal position. For example, position: relative; top: 10px moves the element 10 pixels down from its normal position, and position: relative; top: -10px moves the element 10 pixels up. Left specifies how much the element is moved the element is moved to the right of the normal position. For example, left: 10px moves the element 10 pixels to the right, and left: -10px, 10 pixels to the left. Right specifies how much the element is moved to the left of its normal position. For example, right: 10px moves the element 10 pixels left, and right: -10px moves the element 10 pixels right. Bottom specifies how much the element is moved above its the normal position. For example, bottom: 10px moves the element 10 pixels up, and bottom: -10px, 10 pixels down.

Absolute (including fixed) positioning

Top specifies the offset of the top margin edge below the top edge of the element's containing block, and left the offset to the right of the left edge of the element's containing block. Thus top: 50px places the top margin edge of an element 50 pixels below the top edge of the element's containing block.

Bottom refers to the offset of the bottom margin edge below the bottom edge of the box's containing block, and right the offset of the right margin edge to the left of the right edge of the element's containing block.

Containing blocks and absolutely positioned elements

This containing block, although it sounds rather complicated, is actually quite a simple concept. Consider the following box:

+----------------------------+
|                            |
|                            |
|                            |
|                            |
|                            |
|                            |
|                            |
|                            |
|                            |
|                            |
|                            |
+----------------------------+

If that is a containing block, you can then use the edges of that block to specify the position of an element.

So if you specify <DIV style="position: absolute; top: 100px; left: 100px; border: solid">Some content</DIV>, this will be the result:

+----------------------------+
|                            |
|                            |
|                            |
|              +------------+|
|              |Some content||
|              +------------+|
|                            |
|                            |
|                            |
|                            |
|                            |
+----------------------------+

As you can see, the top: 100px and left: 100px have placed the top edge of the element 100 pixels below the top edge of the containing block, and the left edge 100 pixels to the right of the left edge of the containing block.

What is the containing block?

For fixed positioning (a subset of absolute positioning) the containing block is always the viewport (i.e., the 'window' in which content is rendered in a browser). This means that the element is 'fixed' when you scroll the document. This means that the declaration top: 50% means the top margin edge of the element should be halfway down the viewport.

For ordinary absolutely positioned elements, the containing block is a more complex affair. The containing block is the nearest ancestor of the element with a position other than static.

If that ancestor is block-level, offsets are relative to the padding edges of that element; if, however, it is inline (therefore relatively positioned, since all absolutely positioned elements are block-level), offsets are relative to the content edges of the first box generated by that element.

If there is no such element, the content edge of <BODY> is used (in XML, the root element will be used).

Support

CSS positioning is supported by Netscape and IE 4 and 5.

Netscape does not color in the background properly on absolutely positioned elements unless the Netscape-specific layer-background-color is set to the same value as background-color.

Positioning examples

Fixed example

#adbar {postion: fixed;
height: 20%}
#menu {position: fixed;
width: 20%;
top: 20%}
#main {position: fixed;
top: 20%;
left: 20%}

<BODY>
<DIV id="adbar"> . . .</DIV>
<DIV id="menu"> . . .</DIV>
<DIV id="main"> . . .</DIV>
</BODY>

___________________________
|  adbar                   |
|                          |
|__________________________|
| menu  |                  |
|       | main             |
|       |                  |
|       |                  |
|       |                  |
|       |                  |
|       |                  |
|_______|__________________|

Relative example

<P>
Some content, with <SPAN style="position: relative; top: 100px">this that will be relatively positioned 100 pixels down</SPAN>.

Absolute example

<P style="position: absolute; top: 100px">
An element that will have its top margin edge 100 pixels below the top edge of the containing block (typically the top edge of BODY).

The next section considers advanced key concepts.