[英]CSS force div with absolute position be behind div with static position
所以可以说我有一些文件
#a{width: 10px; height: 10px; background: red; z-index: 10;} #b{width: 100%; height: 100%; background: black; z-index: 5; position: absolute;}
<body> <div id="a">foo</div> <div id="b">bar</div> <body>
#b div
覆盖#a
,因为#b
具有absolute
位置。
如何在不更改#a位置的情况下强制#b
落后#a
?
您可以添加position: relative
对于#a
元素的position: relative
。
您必须在第一个div上设置除static
以外的position
才能应用z-index
样式
#a { width: 100px; height: 100px; background: red; z-index: 10; position: relative; } #b { width: 100%; height: 100%; background: black; z-index: 5; position: absolute; left:0; top:0; }
<div id="a">foo</div> <div id="b">bar</div>
您应该在第一个div中添加相对位置:
<body>
<div id="a">foo</div>
<div id="b">bar</div>
<body>
<style>
#a{width: 10px; height: 10px; background: red; z-index: 10; position:relative}
#b{width: 100%; height: 100%; background: black; z-index: 5; position: absolute;}
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.