tags:

views:

48

answers:

4

What is the difference between using "margin-left: 10px" to using "position:relative; left:10px" . It seems like it is producing the same result..

Thanks

Joel

A: 

I can only suppose it is there for other positions with margins. i.e.:

margin-left: 5px;
position: inherited; left: 10px;
Lazlo
+4  A: 

When you move something with position:relative you're not actually moving the space it takes up on the page, just where it is displayed.

An easy way to test this is to use a simple structure like this:

<div id = "test box">
  <img src = "http://www.dummyimage.com/300x200" id = "first">
  <img src = "http://www.dummyimage.com/300x200" id = "second">
</div>

with the following CSS:

img{ display:block; }
#first{ margin-top:50px; }

versus this CSS:

img{display:block;}
#first{position:relative; top:50px;}

You'll see that the first moves everything down 50px while the second only moves the first image down (which means it will overlap the second image).

Edit: you can check it out in action here: http://www.jsfiddle.net/PKqMT/5/

Comment out the position:relative; and top:100px; lines and uncomment the margin-top line and you'll see the difference.

Austin Fitzpatrick
Thanks!!! Very clear!
Joel
A: 

The simplest way I can explain it is that margin-left moves the element itself, whereas left (with position: relative) pushes other elements away. Although that's not, perhaps the clearest description.

With pictures, though:

            +---------------------------------------------------------------------------+--------------+
            |                                                                                          |
            |              +------------------------------------------------------------+              |
            |              |                                                            |              |
            |              |              +------------------------------+              |              |
            |              |              |                              |              |              |
  position  |              |              |                              |              |              |
<--Left---->|<---margin--->|<---padding-->|<------------width----------->|<---padding-->|<---margin--->|
            |              |              |                              |              |              |
            |              |              +------------------------------+              |              |
            |              |                                                            |              |
            |              +------------------------------------------------------------+              |
            +------------------------------------------------------------------------------------------+

With position: absolute left also serves to define the distance between the element itself and the left boundary of whatever object the element is positioned against.

David Thomas
A: 

Consider any given block element (a DIV for example) as a box. position:relative; makes the box's position on the page relative to the element it is nested inside (another DIV for example) and left:10px; moves the box 10 pixels to the right (AWAY from the left).

Now margin-left: 10px; has nothing to do with that and just creates a margin (an invisible power field if you prefer :P) on the left of the box, which makes it move if there's another fixed element stuck on its left.

Teo Maragakis