margin-top和top的区别

top要配合position:relative和position:absolute使用。

[url]http://stackoverflow.com/questions/4036176/css-top-vs-margin-top[/url]
You'd use margin, if you wanted to move a (block) element away from other elements in the document flow. That means it'd push the following elements away / further down.

If you wanted the element to have no effect on the surrounding elements, you'd use positioning (abs., rel.) and the top, bottom, left, right settings.

上述说如果我们想移动一个元素, 但是却不影响其它元素的位置, 那我们就可以使用
position:relative|absolute + top:xx
如下图所示:
[img]http://dl2.iteye.com/upload/attachment/0095/4185/4075f49d-83fa-3623-8fe2-f4f9828692e3.jpg[/img]


原本div1是在div2的上面。
当加入了position和top之后, div1被移动到了下面, 而div2没有收到影响。

当我们使用margin时, 就会导致其它的元素被下移,或者移开。

如下图所示:
[img]http://dl2.iteye.com/upload/attachment/0095/4187/330f4e01-ec1d-3b46-9c44-f589890caad2.jpg[/img]

这里div2随着div1一起被移动到了下面。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值