<!DOCTYPE html>
<html lang="pl">
<head>
<title>Test z-index</title>
<style>
.div1 {position:relative; z-index:1; margin:10px; background:#eee; }
.div2 {position:absolute; top:14px; z-index:999; background:blue; }
</style>
</head>
<body>
<div class="div1" >Lorem ipsum ... <div class="div2">This element I want to be on top!</div> Phasellus fermentum in, dolor.</div>
<div class="div1" >Suspendisse a pellentesque dui, non felis.</div>
</body>
</html>
我有两个相对定位的元素div1
。
在第一个元素中,我放置了一个绝对定位的元素div2
。我希望将div2
元素放在两个div1
元素(所有网站上的元素)的顶部。
问题在于div2
在第一个div1
元素的顶部,但它并不在第二个div1
元素的顶部。
如何解决?如何编辑我的CSS代码?