CSS Positioning 101

by Noah Stokes

66 Reader Comments

Back to the Article
  1. Very good overview of this topic, as one in the crowd “technical developer that occasionally has to do some css work” this was perfect!

    I fond one other difference between absolute and relative. I made a form, using width: on the labels to position the form fields correctly. Worked fine until I came to the first set of radio buttons (separated with a br/) Then the upper one was correctly placed, whereas the lower one lined up with the label. Wrapping those into a div with position:relative made no difference, whereas position:absolute made the radiobuttons align just as I wanted them to, whereas the relative box was allowed to be “squeezed between the lines”.

    Copy & paste the code below to embed this comment.
  2. I never fully understood positioning elements, that is until now. This was a very well written article and I thank you for saving me much of the frustration I would have gone through had I not found and read this piece. Keep up the great work.

    Copy & paste the code below to embed this comment.
  3. Brilliant post and elegantly explained. Your post is as elegant as the CSS which is laid out. Thanks for the example pages as well. It helps draw a better picture and makes things easier to understand. I’ll be giving this post the social media treatment.

    Thank you

    Copy & paste the code below to embed this comment.
  4. I really enjoyed this article. It was a nice refresher to some CSS of which I’m a little rusty with. The examples were really helpful.

    Copy & paste the code below to embed this comment.
  5. I needed this article! Thank you.
    On the last example, the footer does not stay fixed on my iPhone 4.

    Copy & paste the code below to embed this comment.
  6. @brennanag -Speaking without too much knowledge on the subject, I do know that fixed position does not work the same on the iPhone out of the box. This article explains why an even offers a solution:  http://doctyper.com/#/post/archives/200808/fixed-positioning-on-mobile-safari/

    Copy & paste the code below to embed this comment.
  7. 1. There is an accessibility issue with the use of absolute or fixed positioning for text blocks.  This often results in text being cut off or text overprinting other text if the site visitor has set a larger font size than the site designer expects.

    2. There is a usability issue with the use of fixed positioning.  If the site visitor scrolls by clicking just above the down arrow on the scroll bar, at least Firefox 3.6 Windows, and possibly other user agents, does not take the height of fixed elements into account when deciding how far to scroll.  This often results in one or two lines of text being missed, that is, not displayed to the site visitor before or after scrolling.  So the site visitor has to train themselves not to use that scrolling method on such a site.

    Copy & paste the code below to embed this comment.
  8. 2. Actually, two scrolling methods may be taken away by a fixed block.

    For the example http://www.alistapart.com/d/css-positioning-101/example_j.html in a browser sized at approximately 1124 × 563 and the default font size:

    The last line of text shown is in the second paragraph:

    nec dolor augue, sit amet blandit urna. Curabitur vitae elit id sem blandit tincidunt. Fusce aliquam sodales mauris

    Scrolling by clicking just above the arrow makes the new top line:

    libero. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Lorem ipsum dolor

    with a sliver cut off from the top, missing the line:

    vitae molestie. Aenean sit amet ligula sapien. Donec luctus ultrices condimentum. Fusce ac velit odio, sed luctus

    Scrolling by pressing the space bar did not lose any text at the default font size, but at 18-point minimum font size, the last line before scrolling is in the first paragraph:

    tristique condimentum, odio justo faucibus diam, sed imperdiet

    Pressing the space bar makes the new top line as:

    rutrum ut sem. Maecenas pulvinar bibendum lectus vel malesuada.

    meaning the line:

    metus dui non augue. Ut felis enim, accumsan id tempor vitae

    was skipped.  Usability and accessibily issue.

    Copy & paste the code below to embed this comment.
  9. It was a pleasure to read this article about necessary basics for css-layouts and remembering the old days building creative constructions with wooden blocks. And you are right: With css bulding is even cooler! I’ve translated this article into German: http://kleines-universum.de/html-css/css-position-einmaleins-aus-alistapart/
    I’ve a question also for future translations: Is it okay to deeplink the given examples or should I copy them to my webspace? Thank you very much!

    Copy & paste the code below to embed this comment.
  10. I find positioning extremely frustrating especially if im working on a new design, when you test in the browser, Firefox, Chrome and I.E can be quite different especially if you want pixel perfect positioning. Usually I make a div container and place everything in it get it to work in as many of the browsers as I can and then use a hack for the one I cant. Which ultimately isn’t the best solution but works for the minute.

    Copy & paste the code below to embed this comment.
  11. “we apply different position values to a given selector until we get one that works” – well, actually we do this because there are browsers out there that don’t respect standards. Ten years ago we’ve learned that all CSS is try and error – and we still need to come over it.
    As long as browsers don’t implement CSS like it’s written in the specs, theres less reason to study the specs, exactly?

    Copy & paste the code below to embed this comment.
  12. Another article on this subject: the “position property”:http://www.yuiblog.com/blog/2010/12/14/the-css-position-property/ on YUIBlog.

    @DrLangbhani, I’ve plugged your analogy in there :)

    Copy & paste the code below to embed this comment.
  13. Just a “thank you” from this self-taught artist-designer. I’ve learned this material a number of times when things didn’t fall together as I expected on the page. I think that now, thanks to your articulate explanation, I’ve finally got a handle on it for good. It’s not rocket science, but until you understand the underlying logic, it makes no intuitive sense. Now I think I do. Thanks for taking the time to lay it out clearly. Numerous books have essentially the same information, but have somehow still left me a little lost. Shows how important good writing is. It’s a lot more than efficient “content delivery.”

    Copy & paste the code below to embed this comment.
  14. @Eric – thanks for the kind words, I’m so glad things have clicked for you! Carry on.

    Copy & paste the code below to embed this comment.
  15. indeed with css we will save more energy when the page loads, I like this trick and if it had been added image maybe we can add background: # color url(image link) to add texture, and there will be more fantastic

    Copy & paste the code below to embed this comment.
  16. This is very well written article, good to have basics revised again. But this code could have been more optimised. That is what I think..

    Copy & paste the code below to embed this comment.
  17. Thanks for the clear instructions.  I am fairly new to css and this explained a concept that has had me tearing my hair out.  Straight away I was able to use what I learned to solve a problem on my site, in 5 minutes, that had, had me going around in circles for about 2 hours.  Thanks.

    Copy & paste the code below to embed this comment.
  18. @ UKdynamo -  Thanks for the feedback! Indeed the code could have been optimized, but for the sake of explanation and simplicity in learning it is presented as is.

    Copy & paste the code below to embed this comment.
  19. Thanks so much for the clearly written and illustrated article. This is exactly what I’ve been searching for in a positioning tutorial.

    Copy & paste the code below to embed this comment.
  20. another gold from Noah Stokes =)  thank you so much for all ideas & informations…

    Copy & paste the code below to embed this comment.
  21. Great article, well written and very informative.

    Thanks loads

    Copy & paste the code below to embed this comment.
  22. I’d been looking for a source to explain the basics of CSS Positioning. I am new to web design and really appreciated this well-worded article with great examples. Thanks!

    Copy & paste the code below to embed this comment.
  23. The article doesn’t really establish how contexts work for relative positioning. Example D nests the “blue” box* inside the red one, and that appears to be how you make the blue box display to the right of the red box and at the same level.

    What you don’t establish is how to make two boxes appear on the same horizontal level—have the same “top” offset—without one being nested inside the other.

    If that’s not possible, it would be a good thing to tell us so, so we don’t waste hours trying to figure out how to make this fairly obvious-seeming thing work. If it is possible, you would do well to construct your example to illustrate as much—since it’s clearly a preferable use-case to nesting a box inside another box to give the illusion of them having the same “top” offset.


    *It would be helpful if you used more obvious hex triplets to create your “red”, “blue”, and “green” examples—using the ones you do is basically a way of saying to users “you’re not advanced enough to play in our club, come back when you learn to read hex.”

    Copy & paste the code below to embed this comment.
  24. … the relative positioning use-case in this article is completely unrealistic.

    Copy & paste the code below to embed this comment.
  25. For example H2, “This allows the #box_2 element to maintain its top edge and still shift 100 pixels to the left.” should be “This allows the #box_2 element to maintain its top edge and still shift 100 pixels to the right.”

    Copy & paste the code below to embed this comment.
  26. Thank you so much for writing this article.  It has definitely taken the “mystery” and guess work out of positioning.

    Copy & paste the code below to embed this comment.