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

前端 未结 4 2059
盖世英雄少女心
盖世英雄少女心 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: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:

    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.

提交回复
热议问题