Difference between "margin-left", and "left" (or "margin-right", and "right")

Css

Css Problem Overview


What is the difference between margin-left: 10px; and position: relative; left: 10px;?

It seems to produce the same result

Css Solutions


Solution 1 - Css

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.

Solution 2 - Css

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 = "testbox">
  <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.

Solution 3 - Css

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

margin-left: 5px;
position: inherited; left: 10px;

Solution 4 - Css

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.

Solution 5 - Css

margin-left, margin-right, ... => these add the pixels after the padding of their parent BUT top, left,... => these add the pixels from the edge(border) of their parent ignoring the padding Assuming that the positioning of their parent is relative

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoelView Question on Stackoverflow
Solution 1 - CssDavid ThomasView Answer on Stackoverflow
Solution 2 - CssAustin FitzpatrickView Answer on Stackoverflow
Solution 3 - CssLazloView Answer on Stackoverflow
Solution 4 - CssTeo MaragakisView Answer on Stackoverflow
Solution 5 - CssBekalu TilahunView Answer on Stackoverflow