Position
ON THIS PAGE
| Property Name | Some Possible Values | What It Does |
|---|---|---|
| position | relative | absolute | fixed | Sets type of positioning |
| top | 20px | 10% | Offsets element from top |
| left | 20px | 10% | Offsets element from left |
| bottom | 20px | 10% | Offsets element from bottom |
| right | 20px | 10% | Offsets element from right |
NOTES:
- Position ~ The
positionproperty can be used to define how an element is displayed with respect to the normal flow. The values you can use are described in the following:
relative- The element is positioned relative to the normal flow. This may be used in conjunction with the offset properties.absolute- The element is removed from the normal flow and positioned with respect to its containing block. This may be used in conjunction with the offset properties.
fixed- Same asabsoluteexcept the element is fixed in place (not supported by Internet Explorer 6).static- The element is positioned normally. (Offset properties do not apply).
WHAT IS THE NORMAL FLOW?
Web browsers are designed to display HTML elements on a web page according to certain standards. Block elements, such asp,div,blockquote, are typically stacked vertically one on top of the other:
THE FIRST DIV ELEMENT.THE SECOND DIV ELEMENT.THE THIRD DIV ELEMENT.
On the other hand, inline elements likespan,codeorAare typically lined up horizontally until available space runs out after which the content will break to a new line:
THE THIRD SPAN ELEMENT.THE SECOND SPAN ELEMENT.THE THIRD SPAN ELEMENT.
THE CONTAINING BLOCK
Depending on the user's browser, the root element of a web page can be either thehtmlor thebodyelement. This root element represents the browser viewport and —by default— becomes the containing block for all other elements on your web page. Absolutely positioned elements are offset with respect to the border of this principal containing block.
However, any block element on your web page may also be defined as a containing block for other elements inside of it. How do you do this? By simply using thepositionproperty and setting the value to anything butstatic.
To illustrate, let's take our threedivelements in Example 1 and put them all inside anotherdivelement. Then we'll use thepositionproperty, set it torelativeand, presto... we have a containing block. For demonstrational purposes, let's give it a red border and set its width to 300 pixels. Elements inside this containing block may be now absolutely positioned with respect to its borders.
Example 3 - A CONTAINING BLOCK (RED BORDER) IS ESTABLISHED USING position: relative;THE FIRST DIV ELEMENT.THE SECOND DIV ELEMENT.THE THIRD DIV ELEMENT.Example 3 - A CONTAINING BLOCK (RED COLOR CODE) IS ESTABLISHED USING position: relative;
<div style="position: relative; border: 5px solid red; width: 300px;">
<div style="background: blue; color: white;">THE FIRST DIV ELEMENT.</div>
<div style="background: green; color: white;">THE SECOND DIV ELEMENT.</div>
<div style="background: purple; color: white;">THE THIRD DIV ELEMENT.</div>
</div>
RELATIVE POSITIONING
So exactly what is relative positioning? Well let's take Example 3 and offset the position of the second green coloreddivrelative to the normal flow.
First we'll use thepositionproperty on the greendivand set it torelative. Now we can use thetopandleftproperties to offset thedivelement from its normal position:
The green div is relatively positioned (placed in the normal flow). It is then offset 5 pixels from the top and 10 pixels from the left of where it would normally appear.Example 4 - RELATIVE POSITIONINGTHE FIRST DIV ELEMENT.THE SECOND DIV ELEMENT.THE THIRD DIV ELEMENT.
Example 4 - RELATIVE POSITIONING
<div style="position: relative; border: 5px solid red; width: 300px;">
<div style="background: blue; color: white;">THE FIRST DIV ELEMENT.</div>
<div style="position: relative; top: 5px; left: 10px; background: green; color: white;">THE SECOND DIV ELEMENT.</div>
<div style="background: purple; color: white;">THE THIRD DIV ELEMENT.</div>
</div>
ABSOLUTE POSITIONING
So maybe you'd like to set some kind of coordinates on your web page and have adivelement, for example, displayed exactly there regardless of what else is going on. This can be accomplished using absolute positioning. This kind of positioning will remove the element from the normal flow such that it may be offset from the edge of its containing block. Let's take the greendivin Example 4 and simply change the value of thepositionproperty fromrelativetoabsolute. The following result is what we get:
The green div is absolutely positioned (removed from the normal flow). It is then offset from the top/left sides of the containing block (red border).
(Note that the width of the absolutely positioned green div no longer defaults to 100% of the available space but rather is displayed just wide enough to hold its content.)Example 5 - ABSOLUTE POSITIONINGTHE FIRST DIV ELEMENT.THE SECOND DIV ELEMENT.THE THIRD DIV ELEMENT.
Example 5 - ABSOLUTE POSITIONING
<div style="position: relative; border: 5px solid red; width: 300px;">
<div style="background: blue; color: white;">THE FIRST DIV ELEMENT.</div>
<div style="position: absolute; top: 5px; left: 10px; background: green; color: white;">THE SECOND DIV ELEMENT.</div>
<div style="background: purple; color: white;">THE THIRD DIV ELEMENT.</div>
</div>
- Top
Left
Bottom
Right
These are offset properties that can be used in conjunction with any element that is positioned. An element is said to be positioned when thepositionproperty is set to anything butstatic. The offset properties shift the element away the specified side of the containing block according to a specified distance. Any CSS length measurement may be used as the value but it's probably easiest to stick with either pixels or percentages.
Logically speaking, what you want to do is use a pair of offset properties that shift the element away from one corner. To accomplish this, you would simply combine the use of any horizontal and vertical side. Hence using thetopandleftproperties together would shift the element away from the top/left corner. Likewise you could combine the use of thebottomandrightproperties together. Using therightandleftproperties together just simply wouldn't make sense.
In the following example code, thedivis absolutely positioned. Then it is offset 5 pixels from the top and 10 pixels from the left:
<div style="position: absolute; top: 5px; left: 10px;">Some content...</div>
A major player in tableless designs is the use of the
float property. The next page in this tutorial will give you the lowdown on how to create floats and how to align text...
MORE CSS PROPERTIES:
SEE ALSO:
- How to Use CSS - An Overview
- Inline Styles
- Embedded Style Sheets
- External Style Sheets
- Class Selectors
| <~ BACK | TOP | NEXT ~> |
