Difference between “margin-left”, and “left” (or “margin-right”, and “right”)

前端 未结 4 2057
盖世英雄少女心
盖世英雄少女心 2020-12-13 00:19

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

It seems to produce the same result

相关标签:
4条回答
  • 2020-12-13 00:31

    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.

    0 讨论(0)
  • 2020-12-13 00:37

    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.

    0 讨论(0)
  • 2020-12-13 00:47

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

    margin-left: 5px;
    position: inherited; left: 10px;
    
    0 讨论(0)
  • 2020-12-13 00:50

    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.

    0 讨论(0)
提交回复
热议问题