gpt4 book ai didi

html - 如何在 div 之间设置不同的 z-Index?

转载 作者:太空宇宙 更新时间:2023-11-04 01:06:45 25 4
gpt4 key购买 nike

我在同一级别有不同的div,但我想设置不同的z-index

这是我的 jsfiddle: https://jsfiddle.net/k85t9zgq/8/

这是屏幕:

enter image description here

如何让红色 div 只在白色 div 中可见?

#page {
margin-top: 50px;
width: 300px;
height: 300px;
background-color: #000;
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
z-index: 4;
}

#box-first,
#box-second {
width: 200px;
height: 100px;
background-color: #fff;
border-radius: 200px 200px 0 0;
margin-top: 10px;
margin-bottom: 10px;
position: relative;
display: flex;
justify-content: flex-end;
align-items: flex-start;
z-index: 3;
}

#first,
#second {
border-radius: 200px 200px 0 0;
margin: 0;
background: red;
width: 200px;
height: 100px;
transform: rotate(230deg);
transform-origin: 50% 100%;
position: absolute;
top: 0px;
right: 0px;
border: 0;
z-index: 1;
}

#n1,
#n2 {
font-size: 20px;
color: #000;
font-weight: bold;
position: absolute;
left: 50px;
right: 0;
text-align: center;
top: 50px;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
width: 100px;
height: 50px;
background: yellow;
border-radius: 100px 100px 0 0;
}
<div id="page">
<div id="box-first">
<div id="first" class="first-pause">
</div>
<div id="n1">
1500
</div>
</div>
<div id="box-second">
<div id="second" class="second-pause">
</div>
<div id="n2">
270
</div>
</div>
</div>

最佳答案

我会考虑另一种用更少的代码实现这一点的方法:

.box {
width:300px;
height:300px;
box-sizing:border-box;
border:70px solid transparent;
border-radius:50%;
background:
linear-gradient(to top,#000 50%,transparent 50%) border-box,
linear-gradient(yellow,yellow) padding-box,
/*adjust the degree to control the red part from 0deg to 180deg*/
linear-gradient(var(--p,60deg), red 49.8%,transparent 50%) border-box,
#fff;
margin-bottom:-140px;
text-align:center;
font-size:40px;
font-weight:bold;
}

body {
background:#000;
}
<div class="box">
1500
</div>
<div class="box" style="--p:120deg">
1500
</div>

但对于您的初始代码,只需考虑 overflow:hidden 并使黄色 div 的 z-index 更大

#page {
margin-top: 50px;
width: 300px;
height: 300px;
background-color: #000;
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
z-index: 4;
}

#box-first, #box-second {
width: 200px;
height: 100px;
background-color: #fff;
border-radius: 200px 200px 0 0;
margin-top: 10px;
margin-bottom: 10px;
position: relative;
display: flex;
justify-content: flex-end;
align-items: flex-start;
z-index: 3;
overflow:hidden;
}


#first, #second {
border-radius: 200px 200px 0 0;
margin: 0;
background: red;
width: 200px;
height: 100px;
transform: rotate(230deg);
transform-origin: 50% 100%;
position: absolute;
top: 0px;
right: 0px;
border: 0;
z-index: 1;
}


#n1, #n2 {
font-size: 20px;
color: #000;
font-weight: bold;
position: absolute;
z-index:3;
left: 50px;
right: 0;
text-align: center;
top: 50px;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
width: 100px;
height: 50px;
background: yellow;
border-radius: 100px 100px 0 0;
}
<div id="page">
<div id="box-first">
<div id="first" class="first-pause">

</div> <div id="n1">
1500
</div>
</div>
<div id="box-second">
<div id="second" class="second-pause">

</div> <div id="n2">
270
</div>
</div>
</div>

关于html - 如何在 div 之间设置不同的 z-Index?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52016990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com