我希望我的类“非绝对”元素正常定位,因为它的 sibling 元素将定位为静态。现在,由于它的 sibling 是绝对定位的,非绝对元素与绝对定位元素重叠。
我该如何解决这个问题,以便该元素将占据其正常位置,所有它的 sibling 都将定位为静态?
<html>
<head>
<style type="text/css">
.world{width:1000px;position:relative;border:1px solid black;height:200px;}
.world .child{position:absolute}
.animal{left:0px;}
.tree{left:200px;}
.water{left:500px;}
.not-absolute{position:relative;float:left}
</style>
</head>
<body>
<div class='world'>
<div class='child animal'> Absolute</div>
<div class='child tree'> Absolute</div>
<div class='child water'>Absolute</div>
<div class='not-absolute'>Not Absolute</div>
</div>
</body>
</html>
最佳答案
如果我做对了,恐怕你要求的是不可能的。绝对定位时,元素会从流中移除,因此非绝对元素的正常位置也会发生变化。
是否可以设置 position:relative 当前绝对定位的元素?这将允许在流中保持非绝对元素位置。
您也可以尝试定位您的非绝对元素,或将当前的 .child 绝对元素保留为 position:static 并使用它们的 :before/:after 伪元素来完成这项工作。
关于html - 我怎样才能使一个元素的 sibling 定位绝对,正常定位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13534596/