我在同一级别上有不同的div
,但是我想设置不同的z-index
。
这是我的jsfiddle: https://jsfiddle.net/k85t9zgq/8/
这是屏幕:
如何使红色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>
答案 0 :(得分:2)
我会考虑用更少的代码来实现这一目标的另一种方法:
.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>