dam*_*mon 8 css position absolute
1.position:absolute;left:200px;
2.position:absolute?margin-left:200px;
上述款式有什么不同吗?
有一个微妙的区别.
请考虑以下示例:
<div class="wrap">
<div class="ex1">Ex 1</div>
<div class="ex2">Ex 2</div>
<div class="ex3">Ex 3</div>
</div>
body {
margin: 0;
}
.wrap {
margin: 0 50px;
background-color: lightgray;
height: 200px;
width: 400px;
}
.ex1 {
position: absolute;
background-color: beige;
margin-left: 50px;
}
.ex2 {
position: absolute;
left: 50px;
background-color: beige;
margin-left: 0px;
}
.ex3 {
position: absolute;
top: 50px; /* so you can see what is happening */
left: 0px;
background-color: beige;
margin-left: 50px;
}
Run Code Online (Sandbox Code Playgroud)
并查看结果:http://jsfiddle.net/audetwebdesign/WQA6B/
在示例1(.ex1)中,边距是父容器(.wrap)左边缘的50px .
在示例2(.ex2)中,元素距视图端口的左边缘50px.
为了得到.ex2类似的行为.ex1,你需要设置position: relative
为.wrap两个div相对于相同的上下文.
还有另一个因素.在示例1中,我没有指定任何偏移量,因此如果使用过,则元素将保留在该位置position: static,这就是相对于包装器左边缘计算边距的原因.
如果我已设置left: 0(参见示例3),您将得到类似于示例2的结果.