负边距与相对定位 [英] Negative margins vs relative positioning

查看:154
本文介绍了负边距与相对定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过许多涉及负边距的布局技术,例如侧边栏定位的经典。似乎这些技术可以很容易地应用于相对定位。



而不是这样:

  .sidebar {
margin-left:-600px;
}

可以这样做:

  .sidebar {
position:relative;
left:-600px;
}

似乎相对定位甚至可能在垂直方向更干净,
作为顶部利润操纵可能会受到崩溃利润率问题等的影响。



一个超过另一个有什么优势, p>

感谢 -

解决方案

我想相对定位会将内容向左移但原始空间将被它占据,除非你使下一个元素相对。但是,如果使用负边距,则内容及其原始空间都会移动。


I've come across many layout techniques involving negative margins, such as this classic for sidebar positioning. It seems like these techniques could just as easily be applied with relative positioning.

So instead of this:

.sidebar {
    margin-left:-600px;
}

One would do this:

.sidebar {
    position:relative;
    left:-600px;
}

It seems like relative positioning might even be cleaner in the vertical direction, as top-margin manipulation may be affected by collapsing margin issues, etc.

Is there any advantage to one over the other, or are they practically equivalent?

Thanks-

解决方案

I guess relative positioning will shift the content to left but the original space will be occupied by it unless you make the next element relative too. However with negative margin the content and its original space both are shifted.

这篇关于负边距与相对定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆