Grow your CSS skills. Land your dream job.

Absolute, Relative, Fixed Positioning: How Do They Differ?

Published by Chris Coyier

Finsta left a question in the forums:

I am fairly new to web design and I could never master the differences in positioning of elements. I know there are absolute, fixed, and relative. Is there any others? Also do they majorly differ? And when should you use which?

Short answer: Yes, there is one more, "static", which is the default. Yes, they majorly differ. Each of them is incredibily useful and which you should use of course depends on the desired result. Also, don't forget about float, which is an important part of positioning.

Medium answer: I'm not going to do a "long answer" because there is no need to rewrite what has been written many times before, but I will do a "medium answer" here were we can quickly run through each type. I also sometime refer people to this 10 step tutorial, which does a pretty good job of explaining it.

An important concept to understand first is that every single element on a web page is a block. Literally a rectangle of pixels. This is easy to understand when when you set the element to display: block; or if that element is by default display: block; This means you can set a width and a height and that element will respect that. But elements that are display: inline, like a span by default, are also rectangles, they just flow onto the page different, lining up horizontally as they can.

Now that you are picturing every single page element as a block of pixels, we can talk about how positioning is used to get the blocks of pixels exactly where you want them to go. We're going to leave off any discussion of the box model, but that factors into this as well...

  • Static. This is the default for every single page element. Different elements don't have different default values for positioning, they all start out as static. Static doesn't mean much, it just means that the element will flow into the page as it normally would. The only reason you would ever set an element to position: static is to forcefully-remove some positioning that got applied to an element outside of your control. This is fairly rare, as positioning doesn't cascade.
  • Relative. This type of positioning is probably the most confusing and misused. What it really means is "relative to itself". If you set position: relative; on an element but no other positioning attributes (top, left, bottom or right), it will no effect on it's positioning at all, it will be exactly as it would be if you left it as position: static; But if you DO give it some other positioning attribute, say, top: 10px;, it will shift it's position 10 pixels DOWN from where it would NORMALLY be. I'm sure you can imagine, the ability to shift an element around based on it's regular position is pretty useful. I find myself using this to line up form elements many times that have a tendency to not want to line up how I want them to.

    There are two other things that happen when you set position: relative; on an element that you should be aware of. One is that it introduces the ability to use z-index on that element, which doesn't really work with statically positioned elements. Even if you don't set a z-index value, this element will now appear on top of any other statically positioned element. You can't fight it by setting a higher z-index value on a statically positioned element. The other thing that happens is it limits the scope of absolutely positioned child elements. Any element that is a child of the relatively positioned element can be absolutely positioned within that block. This brings up some powerful opportunities which I talk about here.

  • Absolute. This is a very powerful type of positioning that allows you to literally place any page element exactly where you want it. You use the positioning attributes top, left bottom and right to set the location. Remember that these values will be relative to the next parent element with relative (or absolute) positioning. If there is no such parent, it will default all the way back up to the <html> element itself meaning it will be placed relatively to the page itself.

    The trade-off, and most important thing to remember, about absolute positioning is that these elements are removed from the flow of elements on the page. An element with this type of positioning is not affected by other elements and it doesn't affect other elements. This is a serious thing to consider every time you use absolute positioning. It's overuse or improper use can limit the flexibility of your site.

  • Fixed. This type of positioning is fairly rare but certainly has its uses. A fixed position element is positioned relative to the viewport, or the browser window itself. The viewport doesn't change when the window is scrolled, so a fixed positioned element will stay right where it is when the page is scrolled, creating an effect a bit like the old school "frames" days. Take a look at this site (update: dead link, sorry), where the left sidebar is fixed. This site is a perfect example for since it exhibits both good and bad traits of fixed positioning. The good is that it keeps the navigation present at all times on the page and it creates and interested effect on the page. The bad is that there are some usability concerns. On my smallish laptop, the content in the sidebar is cut off and there is no way from me to scroll down to see the rest of that content. Also if I scroll all the way down to the footer, it overlaps the footer content not allowing me to see all of that. Cool effect, can be useful, but needs to be thoroughly tested.

Comments

  1. Permalink to comment#

    What really helped me in understanding all of these options was when I first started using CSSEdit by MacRabbit, being able to see a live preview of the changes you are making really helps to get a full grasp on what each line of your CSS code is actually doing and how it affects the rest of the layout.

    PS – No, I’m not affiliated with MacRabbit, just use the product on a daily basis for work.

  2. This is a great resource for anyone entering the web design field.

  3. Permalink to comment#

    Dear Chris, please learn the difference between “it’s” and “its”. Thank you.

  4. Permalink to comment#

    To be honest, I have always tried to avoid using positioning unless absolutely necessary. Thanks, Chris, my cover has been blown and you give me no excuse not to fully understand it anymore.

    Great article!

  5. @Schwallex: Sorry about that, I really try to watch that but I have developed bad habits and it obviously sneaks through sometimes.

    As long as we are pointing out each others bad habits, I see on your site that you have each of your h2 elements wrapped in a div that does the positioning. This is extra unnecessary code as the h2 is already a block level element which can be positioned just the same.

  6. Great post! I appreciate its brevity and detail. Thanks!

  7. Permalink to comment#

    Nice post Chris!
    I wrote something about this too some time ago:

    http://www.onyx-design.net/weblog2/css/css-positioning/

  8. Ramiro Ruiz
    Permalink to comment#

    i was waiting for something like this.

    Thanks a lot!

  9. ericb
    Permalink to comment#

    Thanks Chris! I am new to web designing and I really get confused on the differences of the three. This post makes it more clearer for my understanding.

  10. Well explained, Chris! This is definitely a place I’ll point my CSS understudies at!

  11. I remember the positioning gave me a hard time when I began CSS, so I’m sure this article will be enlighting more than one !

  12. fallsemo
    Permalink to comment#

    Also, don’t forget about float, which
    is an important part of positioning.

    When is it best practice to use a float? Don’t you achieve the same effect a float provides by setting absolute positions to elements inside relative ones? And by positioning elements as absolute you avoid having to clear floats?

    I hope I’m explaining my question correctly! Thanks for the post

  13. Permalink to comment#

    As what I call a “traditional” coder (essentially that I work in mostly compiled code instead of interpreted code), this was easily the most complicated aspect of CSS for me to learn. This is due to the differences with respect to context.

    Thanks for a great read – it’s a great feeling to continue to learn!

    To answer fallsemo’s question with some advice: Don’t mix floats and positions if you can help it.

    This is just my opinion, and I’m not dismissing the neat tricks that can expose, I’m just advising the suppression of all the problems it can cause.

  14. @fallsemo

    The important difference is that floated elements still remain a part of the “flow” of the page where absolutely positioned elements do that. As such, they can be the more “flexible” choice.

    • Ajit
      Permalink to comment#

      hi chirs,

      when i am increasing a windows size , the div container is not fixed in a particular position. I also used a absolute position, but it is not working . help me

  15. Frederico Gonzalez
    Permalink to comment#

    Hi this is Finsta, thanks for your help! I now understand the scary positioning xD You helped me and many others out. Also the guy in the forum I forgot his name

  16. mangesh jog
    Permalink to comment#

    hey thank you for article relay useful and like brush up what you learn before
    thank ones again

  17. Permalink to comment#

    Nice post.Thanks..

  18. Permalink to comment#

    Thanks for this post Chris. When I started developping for the web I was struggling with it big time. To get the understanding of what is doing what I did some research myself. Now I think I know the difference and I developed a shoppingmall using a fixed header and sidebar.

  19. Permalink to comment#

    Great article as usual Chris. Thank you for the information. I usually use floats but will give some of these a go to see who fits best when (if that makes sense).

  20. Permalink to comment#

    Hi Chris, as a developer for Bluelounge website that you use for your example above I hope I may share a bit of my experience.

    When we started redesigning the site there were less menu items on the left navigation so everything was visible even on 800×600 screen. But as the site grows, the navigation also becomes longer and may not be the most friendly for browsers with smaller screen. I am very aware of this matter and trying to come out with a way to solve this without needing to cut down the menu items. Suggestions are welcomed :-)

  21. Awesome article. You definitely covered everything I have been trying to learn about positioning and it’s uses. Thanks for making it so simple and concise.

  22. Permalink to comment#

    Chris, a really excellent summary of positioning – definitely bookmarked for me. Its always useful to read up on the basics, as they are often teh things that I forget and struggle with the most!

  23. Permalink to comment#

    Good overview, thanks!

  24. David
    Permalink to comment#

    I thought position: fixed; set something fixed relative to the closest parent with position relative (and defaulted to the viewport (<html>)). Any chance of a confirmation?

  25. Permalink to comment#

    Thanks for this Chris. I had been wondering why navigation elements aren’t fixed more often. Now I know what to avoid…I think :)

  26. Raviraj
    Permalink to comment#

    Hi Chris,
    Thanks for this post Chris.
    I have same problem with fixed position as David asked above.
    I think position fixed is relative to and not to view point (Screen).
    I have created one page with frame in it.

    Frame contain page with one loading image whose CSS are as below.

    position:fixed; left:50%; right:50%; top:50%; bottom:50%; height:1px; width:1px;

    This image is visible to center when I brows the inner page separately, but it get center to iframe and not to screen when I brows the parent page which contain iframe with this page.

    I want the image to center of screen instated of iframe and I tried it but didn’t get desired result.
    Is there any way to do this?

  27. Raviraj
    Permalink to comment#

    I missed <HTML> in one statement.
    Correction*
    I think position fixed is relative to and not to view point (Screen) >> I think position fixed is relative to <HTML> and not to view point (Screen).

  28. Ziba
    Permalink to comment#

    Thank you so much Chris. I really appreciate your “medium answer”. It’s really helpful!

  29. Permalink to comment#

    I will add that Fixed elements, when scrolled to their boundaries, do not continue to scroll their parent elements. They just stop scrolling at their boundaries as you’d expect. They can be a good way to get multiple, nested scroll areas that do not interfere with each other, causing unexpected scrolling of the body element.

Leave a Comment

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".