Note: This article is meant for CSS beginners.
For quite some time I’ve been noticing that when my friends begin to dig their noses on CSS they are easily freaked out and frustrated with CSS positioning.
Static, relative, absolute, fixed, float. What the hell is all this!
I visited Twitter to see if this was a general concern among CSS Beginners and I found that it actually is. Here are some tweets over the last week related to this subject:
@mxcl
Man I hate CSS positioning. Isn’t there an alternative to tables yet?@MeredithWhitney
now that i know how to use ‹ div›s in css.. i just need to learn how to position them properly. getting there@Plagiarizr
I’ve been trying to position that fucking twitter icon for 2 hours now. God damn, my CSS skills are dead.@iMNK
decided that I hate CSS. hrs spent trying to position things 4 a site and no further on. mite look seriously at Ruby on Rails. i <3 OOP@SirLyric
I win at CSS. On the other hand, it shouldn’t take 4 position: relatives and 4 floats to get the effect I need.
CSS positioning is quite easy. I guess no-one explained it plain and simple before.
I want to be crystal clear about the subject so I decided to separate the post into two parts.
The first part (this post) will focus on the four types of CSS positioning: static, relative, absolute and fixed.
The second part will focus on the float property that is obviously related to an element’s position in our site.
Let’s get started.
Position static {position:static;}
All the elements in our HTML document have a static position by default unless we specify another one. This position is how an HTML element looks like if it hasn’t any styles applied to it. This basically means a bunch of content blocks stacked one on top of the other.
These blocks have a 100% width unless we decide otherwise.
That’s why applying position:static; to an element in our CSS code is redundant, it does nothing.
Position static example (View the source code or inspect with Firebug).
¿A CSS position to make something default for every browser?
Yes, it’s necessary because of the cascading styles. Position static in our CSS code will be always used to override a previous position type from the cascade if we need to.
So that’s position static. The default one. Easy right? Let’s proceed to the other ones…
The other types of positioning (relative, absolute and fixed)
First of all, I need to say that position relative, absolute and fixed are deeply related with other CSS properties:
- top
- bottom
- left
- right
- z-index
Without them, these types of positioning won’t work as expected.
The top, bottom, left and right property values are generally defined in pixels. And they can be both positive or negative. (Example: top:1px; left:-2px;)
The z-index property values are just numbers (Example: 1, 2, 50, 150).
Top, bottom, left and right
These properties work with reference points. These points are different when dealing with relative, absolute or fixed positioning.
Further on the post I’ll explain the reference points for each type.
Z-index, what’s that?
Back to school and maths guys!
The X axis is related to the horizontal measure or movement.
The Y axis is related to the vertical measure or movement.
The Z axis is related to the depth measure or movement.
Of course web is 2D, but elements can be on top of the other using CSS positioning (relative, absolute, fixed).
That’s why it is related with z axis, we can achieve some kind of ‘depth’.
An element with a z-index:3; will be on top of an element with a z-index:1;
The number defines the order of the elements.
Higher number = in front, lower number = the back.
Simple examples:
As a graph:

As photoshop layers:

I’ll show an HTML example later on to finish this idea. For now just keep reading
What do position relative, absolute and fixed share in common?
They separate themselves from the content’s flow. They become independent in a new layer of content.
By doing this, their width is affected; it’s not 100% anymore. They will have the width needed for it’s content to fit, unless we apply a determined width size.
When we look at some examples next you’ll understand this better.
Now let’s look at the different positions and their reference points…
Position relative {position:relative;}
The reference points for relatively-positioned elements are defined by their size.
An example of this in an image:

The images has a fixed size and it’s corners define the reference points.
Let’s move the beetle using position relative:
The css code
img {position:relative; top:-30px; right:-30px;}
The result

In relative positioning we can use one property, or a pair to define movement. The possibilities are:
- Top
- Right
- Bottom
- Left
- Top + Left
- Top + Right
- Bottom + Left
- Bottom + Right
To help you understand how the element moves when it’s property has a positive or negative value here is another image:
(Click on it for a bigger size)
Is the same using position relative and a margin?
No, definetely not. You can see why in this example (View the source code or inspect with Firebug).
As I told you before, position relative (and absolute and fixed) separate themselves from the content’s flow, that’s why the element places itself on top of the previous one in this case, while the gray paragraphs with a top margin continue to flow along with the content.
Position absolute and fixed
In absolute and fixed positioning the top, right, bottom and left properties must be used always in pair. The possibilities are:
- Top + Left
- Top + Right
- Bottom + Left
- Bottom + Right
Why these pairs on relative, absolute and fixed positioning?
Because the browser needs reference points from where to move that object.
If you use a top and bottom property the browser gets puzzled, it doesn’t know in which direction to move it because the reference point is unclear. The same happens when using left and right at the same time.
Their reference points are related to the body tag, the element that wraps all the webpage:
(Click on it for a bigger size)
However they are not the same thing…
Position absolute {position:absolute;}
Position absolute with default reference points (the body tag)
Code example:
HTML
<div class="theParent">
<div class="theAbsoluteChild">The absolute child content</div>
</div>
CSS
.theParent {}
.theAbsoluteChild {position:absolute; top:0; left:0; z-index:1;}
Click here to take a look at the example (View the source code or inspect with Firebug).
As you can see, absolute positioned element’s don’t care even about their parent element.
Position absolute with custom reference points
The good thing about position absolute is that it’s the only type in which we can change it’s reference points. So they DO take their parent element into consideration. How we can do that? By making another kind of relationship between the absolute positioned element, and it’s parent (that will be the one defining the points).
To create custom reference points for absolute positioned elements we must assign to it’s parent element a position relative property.
Code example:
HTML
<div class="theParent">
<div class="theAbsoluteChild">The absolute child content</div>
</div>
CSS
.theParent {position:relative;}
.theAbsoluteChild {position:absolute; top:0; left:0; z-index:1;}
Click here to take a look at the example. (View the source code or inspect with Firebug).
When an absolute positioned element doesn’t have a parent element with position relative applied to it, it will use it’s default reference points. The onces related with the body tag.
In the example the coordinate 0 0 (top:0; left:0;) is related to the first top left pixel where the parent element starts.
If the element has 5 parent tags, it doesn’t matter, you can choose any of them. The closest parent to the absolute positioned element will be the reference.
That’s easy right? And kind of useful
Position fixed {position:fixed;}
Fixed position looks quite the same as absolute at first, but it’s not really like this. There are three differences:
- It’s reference points are always related to the body tag. We can’t relate it with a parent element. It ignores it.
- While in absolute the element stays in one place, in the fixed position it moves along with the scroll bar (See example link below)
- It’s almost 100% cross-browser except for IE6 that doesn’t understand the property (there are hacks to fix it if you like, google it)
All this is better explained with a simple example: Position fixed example (View the source code or inspect with Firebug). Scroll to check it.
Last thing: z-index in an HTML example
View the z-index example. (View the source code or inspect with Firebug).
Optional Download
Here you can download all the example files.
Final thoughts
Sites are usually done with 95% static elements with float properties (soon in part 2).
Anyway, it’s important for you to know these properties because they are used frequently in sites. But be selective.
It has no sense making everything absolute or relative.
I’ll explain floats soon so you can have a 100% knowledge on positioning of CSS, and decide for yourself when to use each type of position.
I hope the article helps you out and is easy to understand. I tried my best to accomplish that. See you soon with floats



WOW! Bravo! Amazing! Somebody stop me! I am really impressed with this well thought out, easy to understand explanation of CSS positioning. I will be sharing this with everyone I know who does anything with CSS. I really appreciate your contribution to a better CSS world. It may have taken you a while to put this together but man! it was worth it. Thanks again, and know that I will definitely be coming back to learn more. ; )
Thank you for your kind words Mike
EEEEEEEEEXcelent! Congrats Nacho.. great great article!
No doubt, nacho is a css ninja..
Nacho isn’t just a CSS ninja, he is a CSS Rockstar Ninja!! http://imarockstarninja.com/ ; )
Great article thanks!
Question: I am using absolute and then bottom: 0; and left: 0; however when I expand the browser size my image leaves the bottom of the screen. Should I be using a different position?
Thanks.
Hi Chris. I should see the live example in order to help you
Awesome article
Thank you …. very descript!!!!
A good read with great examples … looking forward to part 2.
Thank you you very much for the article! It’s a pleasure to read a well written, clearly thought out article like this.
Hey Nacho, you have done a really good job. Congrats!
And you have taken up one of the most important and confusing thing in CSS.
Good work!
Thanks!
It’s very well explained and easy to comprehend; definitely a good reference point for someone like me struggling with some of these.
Looking forward to part 2.
Outstanding find! I just came across your great blog through Delicious.
I have been having a really hard time figuring out how positioning works in CSS. Your tutorial really helped me. Thanks a lot.
Looking forward to read the next parts..
Thank you guys. More posts coming soon
Nacho is not a CSS Ninja but the commander and chief of all Ninjas.!
Great post and very well explained. With articles like this I might possible make this my No.1 place for CSS resources.
sad thing is by the time we all get used to divs and such a new, “better” way to layout stuff on html will be out there.
does any1 of you guys would talk to the w3 folks?
I just recently grasped this a few days ago, especially on how I view Relative positioning now. I used to think that when you set an element to Relative, it changes how that element works with each other…but it actually is more useful to use it to change how OTHER elements work with IT. Definitely a “css A-HA! moment”. Thanks for the affirmation and additional explanation to really help drive it home for me.
Great tutorial. I am a very visual person that yet CSS has always been scary to me. I want to see the stuff position themselves as I move them around but it doesn’t work that way in CSS. You have a gift for writing and explaining. Looking forward to part 2!
Thank you Eloine
After reading for 20 min, the headache starts. Its not your fault. It’s just the bloody nature of css.
It’s not difficult. Seriously. When you ‘get it’ you’ll wonder why you found it so confusing in the first place.
Actually this is not the best, in my opinion, tutorial on this subject.
The best one is this:
http://www.barelyfitz.com/screencast/html-training/css/positioning/
So, is “relative” positioning only used to make “absolute” elements have a different reference point? Why is it called “relative”. Is there any reason to apply a “relative” style to an element that doesn’t have a child?
Ok, that was kind of a dumb question. I guess I’m just confused because you say under absolute positioning:
“absolute positioned element’s don’t care even about their parent element.”
then, right below that you say,
“So they DO take their parent element into consideration.”
… and that confuses me a bit.
William you should read more carefully.
You can use relative positioning alone, or you can use it to set reference points for the absolute positioned children inside it.
They are two completely different uses.
Great article, just the kind of clear explanation that is a real help
Great piece. Spelling is distracting, though — beatle ≠ beetle, it’s ≠ its
Thank you for the correction. I’m sorry.
And your grammar is a distraction.
Nacho – Very nice article. Well thought out, many hours spend “photoshoping”, writing, editing, etc. Great job.
Thanks! I am waiting for a second part. I’m sure it will help me a lot.
Many thanks!!! I have been reading a lot of tutorials on CSS recently – I’m finally trying to make the switch from tables – and this has helped clarify matters, especially the part about the parent having to be relative for absolute to work. I’ll revisit my code now and see if I can fix things!
You have really unpacked it nicely. A note on the z index pictures – if the z-index code and in fact , all the css was in the text of the div the relations pictures would be even easier to grasp without going back and forth from source view to graphic view.
That said. thank you for raising my understanding another bar.
Very good explaination of positioning for beginners. Bookmarking for the next time students don’t understand the concept.
thanks for the tips there! CSS positioning can be a pain sometimes, and when the going gets tough, use Tables! LOL
Really great, thanks! stumbled…
I find it very frustrating that there are a lot of blogger templates where people that makes them dont think about positioning that much. Maybe they should stop by here before publishing it.
I’m glad I stumbled to your blog, very thorough coverage of CSS positioning. I agree 100% with ‘web design hastings’
In early stage of my web development career… it very difficult to Handel position… Very good n clear explanation about positioning. It help new comer in designing field.
thanks
Thanks a lot for the info, it´s great! I´m learning css and this really helps, i hope you can do some other posts
(excuse my english, is not my first language).
Thanks again!!
Can’t thank you enough for a well written and very helpful blog!
Good article, amazing looking weblog, added it to my favorites!!
This post can “positionate” to many users in the “margin-top: -300px: level of happiness” at due the clear explanations, it is very well explained!!!!
Thanks
thanks a lot for the article… honestly, i was really getting frustrated with the positioning where it works in one browser & the other one displays it in a very weird way…
but anyways.. now that i have seen your article… i’ll take reference from this for every positioning problem that i face in future…
thanks again,
Twitter got me here…thanks to microblogging!
You made everything sound so simple for a beginner like me! Thanks.
Fantastic!!!
Awesome!
Thanks a Ton!!!
Great blog, thanks a lot for the awesome posts!
Your article was help me a lot, thank you
You explained very nicely. A new guy can also learn from here very easily…well thanks a lot and Post more stuff again as i waiting to learn a lot……
Great tutorial indeed! thanks Nacho
)
Amazing article! It’s so funny that you used Twitter to see what frustrations everyone is experiencing with Web Design. I do the same thing!
Amazing!! you are master, simple and clear explanation.
more than amazing man
Thanks to everyone. I’ll try to keep on writing as soon as I can.
I’m sorry for the poor updates
This article does not address various issues with cross-browser compatibility which is what really drives people crazy. CSS itself is not hard, but achieving cross-browser compatibility is VERY hard.
Use this one simple rule, though, and you will be well on your way: do not use padding. Always use margins instead. The various IEs all render the box model differently and, if you use padding, you are just setting yourself up for trouble.
The article is meant for CSS begginners. It focuses on explaining the different properties and their features. Crossbrowsing was never a part of the article.
And sorry to disagree, but padding is not a problem, and with experience, crossbrowsing is a piece of cake 9 out of 10 times
Awesome post. Well done.