<p>可以将红色盒子的z-index设置为更高的值,比如100,让它覆盖在黄色部分上方。同时,需要将红色盒子的top值设置为负值,比如-30px,让它向上移动一定距离,遮住黄色部分。修改后的代码如下:</p>
<!DOCTYPE html>
<html>
  <head>
    <meta charset='utf-8' />
    <title></title>
    <style type='text/css'>
      * {
        margin: 0;
        padding: 0;
      }
      .box1 {
        width: 300px;
        height: 400px;
        background: red;
        position: relative;
        z-index: 100;
        top: -30px;
      }
      .box1 span {
        width: 100px;
        height: 30px;
        background: yellow;
        position: absolute;
        left: 0;
        top: 0;
        z-index: 10;
      }
      .box2 {
        width: 300px;
        height: 400px;
        background: blue;
        position: relative;
        top: 10px;
        z-index: 0;
      }
      .box2 span {
        width: 100px;
        height: 30px;
        background: green;
        position: absolute;
        left: 0;
        top: 0;
        z-index: 0;
      }
    </style>
  </head>
  <body>
    <div class='box1'>
      <span>1</span>
    </div>
    <div class='box2'>
      <span>2</span>
    </div>
  </body>
</html>
CSS技巧:如何让红色盒子遮住黄色部分

原文地址: https://www.cveoy.top/t/topic/njVH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录