Search code examples
htmlcssz-index

How set different z-Index between divs?


I have differents div at same level but i want to set different z-index.

This is my jsfiddle: https://jsfiddle.net/k85t9zgq/8/

This is the screen:

enter image description here

How can I make the red div visible only in the white 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>


Solution

  • I would consider another way to achieve this with less of code:

    .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>

    But for you initial code simply consider overflow:hidden and make the z-index of the yellow div bigger

    #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>