我希望在div1上面div2。我尝试使用,z-index但不起作用。
div1
div2
z-index
我已经试过这段代码:
div { width: 100px; height: 100px; } .div1 { background: red; z-index: 1; } .div2 { background: blue; margin-top: -15vh; z-index: 2 } <div class="div1"></div> <div class="div2"></div>
您可以同时添加position: relative到div和创建stackingcontext
position: relative
stackingcontext
div { width:100px; height: 100px; } .div1 { background: red; z-index: 2; position: relative; } .div2 { background: blue; margin-top: -15vh; z-index: 1; position: relative; } <div class="div1"></div> <div class="div2"></div>
或者您可以使用transform-style: preserve-3d;,现在.div1应该将其放置在 3D空间中, 而不要在平面中展平。
transform-style: preserve-3d;
.div1
div { width:100px; height: 100px; } .div1 { background: red; z-index: 2; transform-style: preserve-3d; } .div2 { background: blue; margin-top: -15vh; z-index: 1; } <div class="div1"></div> <div class="div2"></div>
您也可以使用一些随机的transform像translate或rotate
transform
translate
rotate
div { width:100px; height: 100px; } .div1 { background: red; z-index: 2; transform: translate(1px); } .div2 { background: blue; transform: translate(1px, -15vh); z-index: 1; } <div class="div1"></div> <div class="div2"></div>
Filters也可以,但是它们不好 Support
Filters
Support
div { width:100px; height: 100px; } .div1 { background: red; filter: brightness(0.4); z-index: 2; } .div2 { background: blue; margin-top: -15vh; filter: brightness(0.4); z-index: 1; } <div class="div1"></div> <div class="div2"></div>