我正在尝试将一个绝对定位的元素放在一个相对定位的元素下面。由于站点开发的限制,蓝色框必须是相对定位的。
想象一下,一个漫画气泡是蓝色框,气泡的尾巴是:after伪元素。我需要绿色框在这两个元素后面,因为它要为这两个元素提供背景。
想象一下,一个漫画气泡是蓝色框,气泡的尾巴是:after伪元素。我需要绿色框在这两个元素后面,因为它要为这两个元素提供背景。
div.layer01 {
background-color: blue;
position: relative;
z-index: 10;
}
div.layer01:after { /* This is the tail of the bubble */
display: block;
background-color: red;
content: '\00a0';
width: 30px;
height: 30px;
position: absolute;
right: 20px;
top: 50px;
border: 3px #fff solid;
}
/* This should be behind the tail and the blue box (bubble) */
div.layer01 div.layer02 {
background-color: green;
width: 320px;
height: 125px;
display: block;
position: absolute;
z-index: 5;
}
<div class="layer01"> <!-- speech bubble -->
<div class="layer02">Why isn't this below the blue?</div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus non dolor quis leo malesuada pharetra at in massa. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div>
修复:
将我想要定位的元素从相对定位的元素中取出,然后将内容包装到一个新的 div 中。虽然增加了标记,但似乎这是唯一可行的方法。