CSS Positioning With Layers Tutorial

Look at the source code for this page. The page starts with a normal <table> here. The <td> element to the right here contains a <div> that is CSS-styled with "position:relative;" which provides the parent positioning reference for the contents inside it, namely two <img>s - the rainbow sky graphic and the Happy Elf - that are each defined with "position:absolute;" The Absolute Positioning removes these two <img>s from the document layout normal flow, and they are rendered (ie. "positioned") in relation to the parent block-level element - the aforementioned relatively-positioned <div>. Note that the Happy Elf graphic has been given a z-index to make sure it is rendered on top of the rainbow sky, even though its <img> tag appears first in the HTML code.

The <p>aragraph in the bottom right-hand corner of this document is absolutely-positioned to butt up against that corner of the page. Contained within that paragraph is the <img> tag that produces our friend, the absolutely-positioned Happy Elf graphic. Note that even though this time the parent block-level element is absolutely-positioned, the Happy Elf's <img> is identical to the one in the <td> above in its positional offset - a few pixels offset from the top and left. The point being that you can use position an element in relation to either a relatively-positioned element or an absolutely-positioned element.

A key to understanding the difference between aboslute positioning and relative positioning is that elements with absolute positioning are removed from the normal document flow. That is, their presence in the document does not affect where elements that have relative positioning are rendered within the document.

For more information, mouse over the Happy Elf in the bottom corner. Then view the page source for this document to see the code involved. Return to Previous Page

Relative Positioning

Absolute Positioning



If you resize your browser window, you'll notice that the table in the top section remains in a static position, although its width expands and contracts with the size of the browser window. But the paragraph in the bottom right-hand corner overwrites the other elements in the document and "moves" as you resize. This illustrates how absolutely-positioned elements are rendered outside the normal flow of the document.

Relative Positioning - Relative positioning means the element is positioned in relation to the document's normal flow. That is, in relation to where the browser would have normally positioned it without any CSS styling instructions.

Absolute Positioning - Absolute positioning means the element is positioned in relation to its parent positioned block-level element. Absolutely positioned elements are removed from the document flow and its presence in the HTML does not affect the positioning of other elements in the document.

Footer Info