spacer

Position

 
ON THIS PAGE
spacer
The CSS properties in the table below can be used to define how various components on your web page are positioned and offset with respect to the normal flow. These properties are typically applied to block elements but you can also apply them to images and some form elements. These include the following:



CSS Properties List 5 - Position
Property NameSome Possible ValuesWhat It Does
positionrelative | absolute | fixedSets type of positioning
top20px | 10%Offsets element from top
left20px | 10%Offsets element from left
bottom20px | 10%Offsets element from bottom
right20px | 10%Offsets element from right


NOTES:
  • Position ~ The position property 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:


    WHAT IS THE NORMAL FLOW?

    Web browsers are designed to display HTML elements on a web page according to certain standards. Block elements, such as p, div, blockquote, are typically stacked vertically one on top of the other:

    Example 1 - NORMAL FLOW OF BLOCK ELEMENTS
    THE FIRST DIV ELEMENT.
    THE SECOND DIV ELEMENT.
    THE THIRD DIV ELEMENT.


    On the other hand, inline elements like span, code or A are typically lined up horizontally until available space runs out after which the content will break to a new line:

    Example 2 - NORMAL FLOW OF INLINE ELEMENTS
    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 the html or the body element. 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 the position property and setting the value to anything but static.

    To illustrate, let's take our three div elements in Example 1 and put them all inside another div element. Then we'll use the position property, set it to relative and, 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 colored div relative to the normal flow.

    First we'll use the position property on the green div and set it to relative. Now we can use the top and left properties to offset the div element 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 POSITIONING
    THE 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 a div element, 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 green div in Example 4 and simply change the value of the position property from relative to absolute. 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 POSITIONING
    THE 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 the position property is set to anything but static. 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 the top and left properties together would shift the element away from the top/left corner. Likewise you could combine the use of the bottom and right properties together. Using the right and left properties together just simply wouldn't make sense.

    In the following example code, the div is 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: