Search code examples
cssz-indexlayerdestroy

CSS: How to Annihilate / Clip element below?


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    #a {
      position: relative;
    }

    #a div {
      width: 100px;
      height: 100px;
      position: absolute;
    }

    #b {
      background-color: green;
      left: 0;
      top: 0;
    }

    #c {
      left: 20px;
      top: 20px;
      background-color: red;
    }
  </style>
</head>
<body>
    <div id="a">
      <div id="b"></div>
      <div id="c"></div>
    </div>
</body>
</html>

In this example there are two rectangles above each other. The red one would actually be transparent but should delete the pixels of the green one, where they intercept. Warning: Setting the second one to solid white would not get accepted as valid solution - it must actually delete those pixels.

Current

Target


Solution

  • Use mask

    #a {
      position: relative;
    }
    
    #a div {
      width: 100px;
      height: 100px;
      position: absolute;
    }
    
    #b {
      background-color: green;
      left: 0;
      top: 0;
      -webkit-mask: conic-gradient(from 90deg at 20px 20px,#0000 25%,#000 0)
    }
    <div id="a">
      <div id="b"></div>
      <div id="c"></div>
    </div>