[英]z-index not working with position relative
我寫的 css 編碼是以下幾行。 但不工作z-index
。
我想知道如何在將position
的值保持為relative
的同時使z-index
工作。
#foo {
position: relative;
z-index: -1;
width: 100%;
height: 30%;
background-color: lightblue;
}
#bar {
width: 50%;
height: 30%;
background-color: lightpink;
}
據我所知,如果您將position
的值設置為非靜態值,則z-index
應該可以工作。 還有其他影響z-index
的因素嗎?
此外,如果我將position
的值更改為absolute
,它工作正常。
position
是relative
的position
是absolute
的https://codesandbox.io/s/heuristic-bose-bfetmj?file=/index.html
margin-bottom:-20px;
到.foo
以便您可以確保.foo
實際上位於.bar
后面。z-index
的效果。 html, body { width: 100%; height: 100%; } #foo { position: relative; z-index: -1; width: 100%; height: 30%; background-color: lightblue; margin-bottom: -20px; } #bar { width: 50%; height: 30%; background-color: lightpink; }
<div id="foo">Foo</div> <div id="bar">Bar</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.