When looking at the code of Lightbox.com I remarked that they are not using top and left in order to position their images but margin-top and margin-left. I've been wondering why for some time and finally found the reason.

It is a way to position absolutely elements in a container and preserving the padding property of the container.

Using traditional top and left

As you can see, the position is relative to the top left of the container, completely ignoring padding.

position: relative;
padding: 20px;

position: absolute;
top: 0;
left: 0;
position: absolute;
top: 0;
left: 200px;

Using margin-top and margin-left

And now, with this trick, the position is taking padding into account.

position: absolute;
margin-top: 0;
margin-left: 0;
position: absolute;
margin-top: 0;
margin-left: 200px;
position: relative;
padding: 20px;

Why it works?

When declaring an element position: absolute; and not setting any of top, right, bottom and left, the element is going to be rendered using the normal flow for its position. However, it is not going to be added to the flow.

So if you want it to work, do not insert anything non absolute before the elements and make sure you don't set any of top, right, bottom and left.

Conclusion

This is a neat CSS trick but there's a little side effect when debugging. Inspectors are going to highlight the top left area of the element in orange which may be strange ๐Ÿ™‚

If you liked this article, you might be interested in my Twitter feed as well.
 
  • Saad Malik

    This works well for absolute elements that are either top aligned or left aligned, but what about elements that need to be right/bottom aligned?

  • http://www.facebook.com/vjeux Christopher Chedeau

    For right aligned, you can do text-align: right; on the container and use margin-right. We actually do that for RTL at Facebook.

    For bottom aligned, I don't really have a solution ๐Ÿ˜ก

  • Michaรซl Picovschi

    I Have the same trouble here for bottom/top aligned elements

  • Alex

    You can propagate padding from the parent like so:

    .parent {
    position: relative;
    padding-top: 20px;
    }

    .parent .child {
    position: absolute;
    top: 0;
    padding-top: inherit;
    }

    This way you do not need to explicitly synchronise the values but will have padding none the less.

  • http://www.matheusbaumgart.com/ Matheus Baumgart

    Great idea using position: relative in the parent. Fixed my problem, thanks!

  • w3debugger

    Not working here ๐Ÿ™‚

    http://jsfiddle.net/w3debugger/g8gnzr9g/

 

Related Posts

  • July 4, 2012 CSS – Vertical Height of Smileys (1)
    There's one big issue when displaying images inline like smileys is to position them at the right height. Using vertical-align pre-sets Usually what you do is try to use vertical-align and test all the possibilities. Then you are frustrated because you can't find the one that is […]
  • September 14, 2011 CSS – One Line Justify (24)
    I came across a CSS problem, text-align: justify does not work with only one line. Justify behavior The reason is because it has been designed with paragraphs in mind. It justifies all the lines but the last one. Normal Justify Lorem ipsum dolor sit amet, consectetur […]
  • September 17, 2011 WoW Interface Anchor Positioning (5)
    I've always found CSS positioning with both float and position: absolute/relative hard to work with. I want to introduce to you an alternative way borrowed from the World of Warcraft Interface: Anchors. Anchor The concept is extremely simple. You can tell where you want the element […]
  • July 8, 2012 Image Layout Algorithm – Lightbox (1)
    Layout Algorithms: Facebook | Google Plus | Lightbox | Lightbox Android | 500px Lightbox.com has a really interesting image layout algorithm. We're going to see how it works and its best use case. How does it work? Column based The algorithm is column based. You pick a […]
  • July 7, 2012 CSS – Understanding Percentage Background Position (9)
    In this article, I'm going to guide you through a concrete problem I had to solve. Eventually, we'll see how to use percentage values in the background-position CSS property and how it solves a lot of tough issues. Usual way Positioning the image The usual way to position images […]