代码之家  ›  专栏  ›  技术社区  ›  Navin Gelot

对div应用灰度,但其子元素之一除外[duplicate]

  •  3
  • Navin Gelot  · 技术社区  · 6 年前

        body :not(.exclude) {
        	-webkit-filter: grayscale(100%);
          -moz-filter: grayscale(100%);
        	filter: grayscale(100%);
        }
        
        .exclude {
        	background: yellow;
        	height: 100px;
        }
        
        .gray {
        	background: purple;
        	height: 300px;
        }
        <html>
        <head>
          <meta charset="utf-8">
          <title>JS Bin</title>
        </head>
        <body>
          <div class="gray">
            <div class="exclude"></div>    
          </div>
        </body>
        </html>

    我试过了,但它没有显示为黄色

    1 回复  |  直到 6 年前
        1
  •  0
  •   chintuyadavsara    6 年前

    应用过滤器时,不可能忽略子元素。 另一种解决方案是。

    .gray{
      position:absolute;
      top:0;
      left:0;
      right:0;
      bottom:0;
      background-color:purple;
      filter: grayscale(100%);
      z-index:-1;
    }
    .exclude{
      background-color:yellow;
      width:inherit;
      height:30px;
      top:10px;
      position:absolute;
      
    }
    <div id="parent_div" style="position:relative;height:100px;width:100px;">
      <div class="gray" ></div>
      <div class="exclude"></div>
    </div>