Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > CSS

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

The Four Schemes

There are four positioning schemes in CSS positioning:

  • Static

  • Absolute

  • Relative

  • Fixed

All these themes have a specific relationship to normal flow.

Static Positioning

Static positioning is simply placing a box in the normal flow. Because static is the default behavior of an unpositioned box, using the declaration position: static; is comparable to the rule p {text-align: left;}. There's no need to apply a rule that mimics default behavior unless you are overriding another rule.

For this reason, you'll rarely see the static value in use, but it's good to know what it is.

Absolute Positioning

Absolute positioning absolutely positions a box within its containing block. You can then use any one or combination of left, top, right, and bottom properties to position the box.

The critical point is that an absolutely positioned box is always positioned to its containing block, not the browser itself.

NOTE

Absolutely positioned boxes are taken out of the normal flow, so they do not affect the layout of sibling boxes.

I'll now apply a style rule to one of the paragraphs in the document and pull it out of the normal flow using absolute positioning:

p#position {
    position: absolute;
    left: 10px;
    right: 300px;
    top: 10px;	
}

Figure 3 shows that the block is positioned 10 pixels to the left, 300 pixels to the right, and 10 pixels from the top of its containing block (which in this case is the html element). The box has now been pulled out of the flow and actually sits on top of the other boxes, which remain in the normal flow.

Figure 3Figure 3

NOTE

Because no width has been assigned to any of the boxes, even the positioned box will move dynamically when the browser is resized. However, it will always retain its positioning. If you want, you can always add specific widths.

Consider the following example: I created a containing block called "main" to contain the content block, which in turn houses the content. I added style rules to position the content box absolutely within another absolutely positioned box (Listing 2).

Listing 2 Adding structural divisions and positioning them absolutely.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Daddy Long Legs</title>

<style type="text/css">

#main {
    position: absolute;
    left: 10px;
    top: 5px;
    width: 500px;
    height: 350px;
    border: 2px solid green;
}

#content {
    position: absolute;
    left: 100px;
    top: 10px;	
    border: 1px solid red;
}

</style>

</head>

<body>

<div id="main">

<div id="content">
<p>It was a distressing time; and poor Jerusha Abbott, being the oldest orphan, 
had to bear the brunt of it. But this particular first Wednesday, like its 
predecessors, finally dragged itself to a close.</p>

<p>Jerusha escaped from the pantry where she had been making sandwiches for the 
asylum's guests, and turned upstairs to accomplish her regular work. Her 
special care was room F, where eleven little tots, from four to seven, occupied 
eleven little cots set in a row. </p>

<p>Jerusha assembled her charges, straightened their rumpled frocks, wiped their 
noses, and started them in an orderly and willing line towards the dining-room 
to engage themselves for a blessed half hour with bread and milk and prune pudding.</p>
</div>
</div>
</body>
</html>

Figure 4 shows that the containing block is positioned, and the content block is positioned in relation to the containing block, not the browser.

Figure 4Figure 4

Relative Positioning

Relative positioning works differently. Instead of blocks being positioned to their containing block, blocks in relative positioning are positioned to the normal flow. The following CSS sets up the content division to be positioned relatively.

#main {
    position: absolute;
    left: 10px;
    top: 5px;
    width: 500px;
    height: 350px;
    border: 2px solid green;
}
#content {
    position: relative;
    left: 100px;
    top: 10px;	
    border: 1px solid orange;
    padding: 10px;
}

Figure 5 shows that the content division disregards its containing block and is instead relating to the browser chrome.

Figure 5Figure 5

And that's the main difference between absolute and relative! That's it—it's really that simple.

Fixed Positioning

Fixed positioning is calculated in the same way as absolute positioning with respect to containing blocks in that it pulls the positioned box out of the normal flow. In the case of fixed positioning, the fixed reference is the browser viewport.

A fixed block remains in place, no matter what else happens around it. So, if I set my containing block to be fixed, that block will be immovable, whereas anything else will either be positioned or in the normal flow.

#main {
    position: fixed;
    left: 10px;
    top: 5px;
    width: 500px;
    height: 350px;
    border: 2px solid green;
}

I removed the containing block and left the original paragraphs in the normal flow. As shown in Figure 6, as I resize my browser, the block remains fixed and does not flow with the browser resize.

Figure 6Figure 6

CAUTION

Fixed positioning could be very useful but for the fact that it lacks support in Internet Explorer for Windows (all versions including the pervasive IE 6.0). So its use is limited to those situations in which you're optimizing for other browsers.

  • + Share This
  • 🔖 Save To Your Account