我发现绝对定位的父元素会将自身高度设为覆盖子元素,而相对定位的父元素则不会。我创建了两个jsfiddles来模拟这个情况:
绝对定位: https://jsfiddle.net/kc1g7v9s/ 相对定位: https://jsfiddle.net/smy5q8Ld/ 当查看渲染结果的元素时,绝对定位容器div的尺寸为220x60,而相对定位容器div的尺寸为689x0。
为什么会这样?我并没有特别想要实现什么,只是对这种行为感到好奇。
附上代码: 绝对定位:
绝对定位: https://jsfiddle.net/kc1g7v9s/ 相对定位: https://jsfiddle.net/smy5q8Ld/ 当查看渲染结果的元素时,绝对定位容器div的尺寸为220x60,而相对定位容器div的尺寸为689x0。
为什么会这样?我并没有特别想要实现什么,只是对这种行为感到好奇。
附上代码: 绝对定位:
<div class="absolute-container">
<div class="child1"></div>
<div class="child2"></div>
</div>
.child1, .child2 {
width: 100px;
height: 60px;
background-color: grey;
margin-right: 10px;
}
.child1 {
float: left;
}
.child2 {
float: right;
}
.absolute-container {
position: absolute;
clear: both;
}
相关:
<div class="relative-container">
<div class="child1"></div>
<div class="child2"></div>
</div>
.child1, .child2 {
width: 100px;
height: 60px;
background-color: grey;
margin-right: 10px;
}
.child1 {
float: left;
}
.child2 {
float: right;
}
.relative-container {
position: relative;
clear: both;
}