1. Technology

Absolute vs. Relative - Explaining CSS Positioning

CSS Positioning is More than Just X,Y Coordinates

By

When you're going to use CSS positioning, the first thing you need to do is use the CSS property position to tell the browser if you're going to use absolute or relative positioning.

What most people don't realize when they're using CSS to lay out Web pages is that there are actually four states to the position property:

  • static
  • absolute
  • relative
  • fixed

Static is the default position for any element on a Web page. If you don't define the position, it will be static and will display on the screen based on where it is in the HTML document and how it would display inside the normal flow of the document.

If you apply positioning rules like top or left to an element that has a static position, those rules will be ignored.

Absolute CSS Positioning

Absolute positioning is the easiest to understand. You start with the CSS position property:

position: absolute;

This tells the browser that whatever is going to be positioned should be removed from the normal flow of the document and will be placed in an exact location on the page. It is also taken out of the normal flow of the document - it won't affect how the elements before it or after it in the HTML are positioned on the Web page.

If you want to position an element 10ems from the top of the document window, you would use the "top" offset to position it there with absolute positioning:

position: absolute;
 top: 10em;

This element will then always display 10em from the top of the page - no matter what else displays there in normal flow.

The four positioning properties are:

To use them, you need to think of them as offset properties. In other words, an element positioned right 2em is not moved right 2em. It's right side is offset from the right side of the window 2em. The same is true for the other three.

Relative Positioning

Relative positioning uses the same four positioning properties as absolute positioning. But instead of basing the position of the element upon the browser view port, it starts from where the element would be if it were still in the normal flow.

For example, if you have three paragraphs on your Web page, and the third has a position: relative style placed on it, it's position will be offset based on it's current location - not from the original sides of the view port.

<p>Paragraph 1.</p>
 <p>Paragraph 2.</p>
 <p style="position: relative;left: 2em;">Paragraph 3.</p>

In the above example, the third paragraph will be positioned 3em from the left side of the container element, but will still be below the first two paragraphs. It would remain in the normal flow of the document, and just be offset slightly. If you changed it to position: absolute; anything following it would display on top of it, because it would no longer be in the normal flow of the document.

What About Fixed Positioning?

Fixed positioning is a lot like absolute positioning. The position of the element is calculated in the same way as the absolute model - from the sides of the view port. But fixed elements are then fixed in that location, like a watermark. Everything else on the page will then scroll past that element.

position: fixed;

Keep in mind, when you fix an element in place on your site, it will print in that location when your Web page is printed out. For example, if your element is fixed at the top of your page, it will appear at the top of every printed page - because it's fixed to the top of the page. You can use media types to change how the printed pages display fixed elements:

@media screen {
 h1#first { position: fixed; }
 }
 @media print {
 h1#first { position: static; }
 }
  1. About.com
  2. Technology
  3. Web Design / HTML
  4. CSS
  5. Advanced CSS
  6. CSS Positioning
  7. Absolute vs. Relative - Explaining CSS Positioning

©2014 About.com. All rights reserved.