HTML Blox

Providing the building blox of the web.

  • HTML Blox Home
  • Blog
    • Design
    • Flash
    • General
    • HTML
    • Javascript
    • PHP
    • Programming
    • Web Servers
  • Forums
  • Where to Start?
  • Lessons
    • CSS
    • Design
    • HTML
    • Javascript
    • Programming
  • References
    • HTML
  • Resources
  • About
  • Contact
  • Go Green!

CSS Lessons

  1. Lesson 1: The Basics
  2. Lesson 2.1: Dimensions - Units of Measurement
  3. Lesson 2.2: Dimensions - The Properties
  4. Lesson 2.3: Dimensions - Borders
  5. Lesson 2.4 - Dimensions: Adding It All Up
  6. Lesson 3: Colors
  7. Lesson 4.1: Layouts - Floating
  8. Lesson 4.2: Layouts - Absolute Positioning
  9. Lesson 5.1: Background Basics
  10. Lesson 5.2: Backgrounds - Picturesque Backgrounds - The Pieces
  11. Lesson 5.3: Backgrounds - Picturesque Backgrounds - Putting it Together

CSS Reference

  1. No reference yet.

Links

  • W3C HTML 4.01 Strict Compliant
  • W3C CSS 2.1 Compliant
  • Web Standards Group Member
  • Follow us on Twitter
  • Number Overflow - Free Advanced Scripts and Tutorials
  • Azure Ronin Web Design - Professional Web Design and Development
  • XML Sitemap
  • U Comment, I Follow

Feeds

  • RSS 2.0 Feed
  • RDF/RSS 1.0 Feed
  • RSS 0.9 Feed
  • Atom Feed

Lesson 4.2: Layouts – Absolute Positioning

del.icio.us Digg Blinklist reddit

In the last lesson we looked at building our layout with floating elements (primarily divisions). In this lesson, we are going to look at another method of arranging our elements: absolute positioning, abbreivated AP.

Layouts with Absolute Positioning

If you remember from the HTML Lessons, when we discussed absolute vs. relative links, we said that absolute links are absolute because they don’t rely on where the page the link is one is. Relative links are relative to the page they are on.

Well, there is also absolute and relative positioning as well. The absolute positioning is absolute because it is positioned based on actual pixels from the corner of the screen; it doesn’t matter where it is in the document. Relative positioning is relative to it’s natural location in the document flow.

You are actually already familiar with relative positioning; it is the default position value for all elements. Since we haven’t been changing it, the relative positioning is what has been causing the elements to be placed where they have been placed up until this point.

Absolutely Absolute

If you basically have all of your elements as being absolute positioned, every element’s position will be based on how many pixels you set, based on the browser. For absolute position, you use the position property and set it to absolute. Position can also take relative (the default), fixed, static, and inherited. We’ll discuss fixed and static in the next lesson.

Once you have set the position to absolute, you can use the following for properties: top, right, bottom, left to set the position. Each of those properties takes a dimension value (pixel, inches, percents, etc.) and sets the position from that edge. So, if you set top to 150pixels, the element will then be 150pixels down from the top of the page. If you set bottom to 5em, it will appear 5em from the bottom. Here are a few more examples:

Code

div {
  /* 10 pixels top and 10 pixels left from the upper left corner of the screen. */
  top: 10px;
  left: 10px;
}

p {
  /* 50 pixels from the bottom of the page */
  bottom: 50px;
}

Now, this can be useful, but on more than rare occasion, this can cause trouble if you try to absolutely absolute position your elements you’ll run into a lot of trouble usually because it gets stretch or moved all around and usually broken on different resolution and browser window sizes.

A better method is to absolutely position the element within a relatively positioned parent.

Relatively Absolute

As we discussed in the last section, if all of your elements are absolutely positioned, they are based on the edges of the page. However, we have a way to give it a bit more flexibility, while still retaining the control of absolute positioning. This is to absolutely position elements within a relatively positioned parent.

When you absolutely position an element within a parent element that has it’s position explicitly set to relative, the absolutely position element is positioned based on the edges of the parent, not the screen.

Notice I said “explicitly set”. As I mentioned earlier, by default position is set to relative. However, if you place an element within one of these elements and give the child absolute position, the child will still be positioned based on the page’s edges. You have to explicitly say “position: relative” to be able to relatively absolute position something.

This technique gives us more control while still retaining the code. Thanks to this method we don’t necessarily have to implicitly set every element an absolute position. We can allow the natural page flow to manage a lot of the work for us, and then just position the children within the parent.

Dimensions by Positions

One other interesting thing to note about absolute positioning is that you can actually use it to define your height and width, instead of the height and width properties. You can do this by setting the pair of opposite positions to both have a value. For example, let’s say you have a division with position: absolute, and you set it’s left to 10px and it’s right to 10px. Assuming you don’t have a width explicitly set, the division will stretch between 10px from the left and 10px from the right, giving it a width based on the width of the window.

If a width was explicitly set it would place it either 10px from the left or 10px from the right and have the width that it is set to in it’s width property. I say “or” because it may vary between different browsers. It would usually go to the left. However, because of this uncertainty you want to avoid having width, left, and right all explicitly set.

Practice, Practice, Practice

As with division layouts, absolute positioned layouts also require a lot of practice and experience to be able to use them efficiently. A key tip for creating absolutely positioned layouts is to draw out your layout elements and determine the corner for each of these elements. Then you can easily define them. There are thousands of possible ways to arrange elements within your layout and it would be impossible to discuss them all.

Simple practice and experimentation and you’ll be able to figure out how to lay out even complex layouts. And on that note, if you plan a layout and it seems overly complex and has a lot of grouping divisions elements, take a look at your layout. There is usually a better way to implement that is a lot simpler. If it seems too complex, it usually is.


In this lesson we talked about how to layout elements using absolute positioning. In the next lesson we’ll talk briefly about setting our position to static and fixed. We’ll then compare floating and absolutely positioned layouts, and talk about how to use the two types together.

This entry was posted on Tuesday, November 11th, 2008 at 7:17 pm and is filed under CSS Lessons. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Please Login or Register or fill out the following to leave a reply:

If you haven't posted an approved comment before, your comment will not show up until approved.

  • HTML Blox Home
  • Blog
  • Design Blog
  • Flash Blog
  • General Blog
  • HTML Blog
  • Javascript Blog
  • PHP Blog
  • Programming Blog
  • Web Servers Blog
  • Forums
  • Where to Start?
  • Lessons
  • CSS Lessons
  • Design Lessons
  • HTML Lessons
  • Javascript Lessons
  • Programming Lessons
  • References
  • HTMLReferences
  • About
  • Resources
  • Contact
  • Go Green!
  • Sitemap

Copyright © 2008 HTML Blox

Unless otherwise noted, all images created and copyright HTML Blox.

Operated by Azure Ronin Web Design