Absolute positions are absolutely your friend

Written by That Web Guy on 25th July 2009. 9 comments

Absolute positions are absolutely your friend
  • Avant Innovations
  • Advertise Here

In an ideal world, we shouldn't need absolute positioning for anything, but sometimes relative positions don't get us any closer to solving some design issues or have unexpected results in different browsers.

During the olden days of web design when many designs were justified left of the browser, so using absolute positions was an exercise in no-brainery: simply position all elements to a top and left coordinate. This worked because the top and left position of the browser is constant, even if the user resizes it.

But on a design that is centred in the browser, top and left are no longer constant (due to varying browser widths) and absolute positions aren't as common, but they can be your saviour as long as you make the right preparations.

How it works

In the example below, I have a 150px x 100px div with positioning set to absolute, and coordinates set to 15 and 15.

<div id="menu">
<div>

#menu {
position: absolute;
width: 150px;
height: 100px;
top: 15px;
left: 15px;
border:solid 1px red;
}

The important thing to understand here is that the coordinates need to be relative (remember this term) to something in order to work. Most people describe it as being relative to the browser - because that's what it physically looks like - but in reality it's relative to the element it's contained in (remember that too - I'll come back to it), which happens to be the body.

Realising this little fact should reveal a clue. Give up? This means that you can use absolute positioning and make it relative to the elements it's contained in, as long as the containing element has a position set to relative. Check this out:

#header {
position: relative;
width: 300px;
height:100px;
margin: 0 auto;
border:solid 1px blue;
}
#menu {
position: absolute;
width: 150px;
height: 100px;
top: 15px;
left: 15px;
border:solid 1px red;
}

With the containing element (#header) assigned a relative position, the menu will now be positioned absolute to that element. This means that no matter where you move #header (even if you centre it in the browser), #menu will position itself top 15 pixels and left 15pixels of where ever #header is, not where the body is.

absolute

You can start to see how this could solve certain design issues, and as long as you don't use it for absolutely (sorry) everything, it can be your best friend.

Is this worth sharing?

That Web Guy

About That Web Guy

That Web Guy (Mikey to his friends) is a veteran web designer based in Perth, Western Australia, and currently Design Director at Perth Web Design. When he's not XHTML'ing or messing around in Photoshop, Mikey can usually be found preaching web standards evangelism onto unsuspecting victims.

Feel free to send That Web Guy a message some time, follow him on Twitter, or make a donation.

Comments

Not a Member

Josh

Another useful lesson mike, this will come in handy down the road. Thanks!

Sunday 26th July 2009 | 12:02 AM Reply Comment URL Back to top

Not a Member

Jeff Chapman

Hmmm... Still not sure why position: relative wouldn't work just as well in this situation.

Sunday 26th July 2009 | 12:42 AM Reply Comment URL Back to top

Not a Member

Branigan

I just put that to the test by wrapping one of my old absolute positioned designs in a div with margin:0 auto; with 960px; and it worked. Pretty neat.

Monday 27th July 2009 | 05:25 AM Reply Comment URL Back to top

CSS Babe

CSS Babe

Absolutely useful :-) Thanks again.

Monday 27th July 2009 | 10:26 AM Reply Comment URL Profile Back to top

That Web Guy

That Web Guy

Responding to this comment by Josh

Glad to know it - you're welcome Josh.

Monday 27th July 2009 | 10:59 AM Reply Comment URL Profile Back to top

Jasmine

Jasmine

It's all very clear to me now. You are the man Web Guy.

Monday 27th July 2009 | 01:11 PM Reply Comment URL Profile Back to top

Not a Member

Luke

Thank you, thank you, thank you! I cannot thank you enough for this useful tidbit! I always thought that the absolute positioning was relative to the body, not the tag that it is in... BRILLIANT!

Tuesday 1st September 2009 | 01:37 PM Reply Comment URL Back to top

Not a Member

Pete

Hi Mikey,

I finally got around to focusing on my site that broke in IE, deciding to focus on versions 7 and above.
After reading your article here it dawned on me that I had "loose" html elements (elements outside any of the site containers) that in IE were being read as relative to the body, not the containers in the site. All the standards compliant browsers handled this without issues but in IE these elements separated as the browser window expanded or contracted.
The solution was to add a simple "wrap" right after the body tag and set it to relative. From that IE could tell the elements what to position themselves to I'm guessing.
Anyway, it works. I haven't checked it on IE6 but I'm thinking not to worry about that.
Thank you for the reference to this article on Twitter, it was the trigger that I needed.
Regards,
Pete
Twitter: @pychap

Wednesday 2nd September 2009 | 11:58 PM Reply Comment URL Back to top

That Web Guy

That Web Guy

Responding to this comment by Pete

Excellent Pete! Glad it worked out for you.

Thursday 3rd September 2009 | 05:44 AM Reply Comment URL Profile Back to top

FYI: You are currently not logged in. It's cool though - you can still comment. But only members get a profile page, access to the download section and they can pimp their own web sites. Feel free to register or Login now!

Your name:

Your comments:

Note: HTML tags are automatically stripped from comments.

Are you human?
Turing

Sorry, I have to ask. So what sort of animal is this? (Hint: you don't have to be perfectly specific)

Back to top

Login to That Web Guy Blog

Login

Not registered? | Forgot your Password? Cancel Login