Suz*_*ioc 4 html css css-position css-float
为什么添加左规则更改行为如此剧烈?是否可以相对于默认位置定位?
http://jsfiddle.net/suzancioc/drDn3/6/
HTML:
<div class='level0'>
<div class='level1'>
Hello
</div>
<div class='level1'>
Hello
<div id='inner2'>inner2</div>
</div>
<div class='level1'>
Hello
<div id='inner3'>inner3</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
.level0 {
height:40px;
width: 500px;
background:red;
}
.level1 {
float:left;
margin:2px;
border-style: solid;
background: cyan;
}
#inner1 {
position: absolute;
background: green;
}
#inner2 {
position: absolute;
background: green;
left:0px;
}
#inner3 {
position: absolute;
background: green;
}
Run Code Online (Sandbox Code Playgroud)
boy*_*oyd 11
为了定位绝对的东西你需要将div(在你的情况下)分配给相对定位的父级
.level1 {
float:left;
margin:2px;
border-style: solid;
background: cyan;
position:relative;
}
Run Code Online (Sandbox Code Playgroud)
添加position:relative使得.level1一种坐标系为它内部的所有元素.