Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Absolutely Fixed Position—Relatively Speaking


WebReview.com: November 19,2001: Tag of the Week: Absolutely Fixed Position—Relatively Speaking

November 19, 2001 > Tag of the Week

Absolutely Fixed Position—Relatively Speaking

By Chuck Musciano

Positioning control, and the lack thereof, has long been a sore spot for many HTML authors. In its purest sense, HTML should never need positioning information, since it conveys semantic context to the browser, who then determines the appropriate layout. In actual usage, where HTML is a vehicle for all sorts of crass advertising and focused content, positioning control is critical to effective document creation. While HTML itself is lacking in this department, CSS2 once again comes to the rescue.

Positioning Models

Within CSS2, each element can adopt one of four positioning models. The default model, static, mirrors the traditional HTML positioning model, with elements placed within a continuous flow that represents the HTML page.

The relative model places an element at some position relative to where it would have been placed using the static model. Only the current element is affected by relative placement; subsequent elements return to their normal position in the flow.

The absolute model fixes an element at an absolute position within the document. The element is not part of the flow and does not affect the positioning of items before or after it. Essentially, the element is lifted from the flow and placed at a specific position in the document.

The fixed model is similar to the absolute model, except that the element is positioned within the browser window instead of the document page. This means that as the document scrolls within the browser window, fixed elements will not move.

You control the positioning model used by an element with the position property. By supplying one of the four previously mentioned keywords, you determine how the element will be positioned in the document. This property is inherited so that elements contained within a positioned element are placed in a similar fashion.

Managing an Element's Position

Once you've determined the positioning model for an element, you can use the left, right, top, and bottom properties to place the element as needed. The top and left properties are interpreted differently, depending on the positioning model used. For all the models, the right and bottom properties specify where the respective edges of the element should be, relative to the top and left edges. Thus, you can use right and bottom to control the size of an element.

For static positioning, the top and left properties are ignored. This makes sense since the browser gets to decide where static elements are placed in the flow.

The relative model interprets the top and left values as offsets to the position the element would have taken using static positioning. Negative values shift the element up and to the left, positive values move it down and to the right. You can alter the position of relative elements by dynamically changing the top and left property values, creating crude animation effects. Be aware that relative elements may overlap or obscure other elements.

The absolute model uses the top and left properties to determine the position, within the document page, for the element. Depending on how the document is scrolled by the user, the element may not be visible in the browser window.

The fixed model behaves like the absolute model, except that the top and left properties specify a position within the browser window, with a position of (0,0) being the upper left corner of the window. Negative values may move the element out of the window, as will large values that exceed the size of the browser window. Since the fixed element does not move as the document scrolls, it is almost guaranteed to obscure some portion of the underlying document as the user scrolls.

While previous versions of HTML attempted to provide some positioning control, CSS2 finally codifies all the flavors of position management in a simple way. You should finally be able to position elements in your documents easily and correctly using these new features of CSS2.


Chuck is the author of the best-selling HTML: The Definitive Guide and now, the fourth and expanded edition, HTML & XHTML: The Definitive Guide. He also writes on a variety of Internet and web-related topics for a number of online magazines.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.