我想要一个灰色的覆盖物,除了selected个以外的所有子元素.假设 struct 如下:

<div class="parent">

  <!-- I have this subparent which is absolute. I cannot remove it... -->
  <div class="subParent1">
    <div class="subParent2">

      <!-- This child I want to be above the OVERLAY, aka not greyed out -->
      <div class="child selected">child</div>

      <div class="child">child</div>
      <div class="child">child</div>
    </div>
  </div>

  
  <!--   This component is underneat subParent in the tree structure -->    
  <div class="grayOverlay"></div>
  
</div>

Here's an exact fiddle.也许,我可以使用伪元素来代替?

PS:我更新子代码,使其更加嵌套,与我的实际代码保持一致.

推荐答案

您可以从下面的代码中获取参考.我对css做了一些修改.我已经在任何需要的地方增加了z-index个,你可以优化它.此外,从subParent1中删除了position: absolute;,并在grayOverlay上增加了top: 0; left: 0;.您可以根据自己的喜好对其进行优化或更改.

.parent {
  width: 300px;
  height: 300px;
  position: relative;
  background-color: gray;
}

.grayOverlay {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-color: rgb(107 114 128 / 0.8);
  z-index: 11000;
}

.subParent1 {
  display: flex;
  flex-direction: column;
  width: 100%;
  z-index: 12000;
}

.child {
  color: black;
  width: 50px;
  height: 20px;
  background-color: white;
  margin: 10px;
  z-index: 10000;
}

.childIWantOverOverlay {
  background-color: red;
  z-index: 12000;
}
<div class="parent">

  <div class="subParent1">
    <div class="child childIWantOverOverlay">child</div>
    <div class="child">child</div>
    <div class="child">child</div>
  </div>

  <!--   This component is underneat subParent in the tree structure -->
  <div class="grayOverlay"></div>

</div>

Css相关问答推荐

与Chrome和Edge相比,背景位置动画在Firefox中运行并不流畅

基于子元素的子元素数量的样式元素

通过使溢出的子元素具有其宽度的100%来防止Flex元素溢出

当鼠标悬停在元素的::第一行时,如何应用样式?

如何使用css::Part()定位Web组件shadowDOM中的第N个元素

如何防止css边框缩小div?

如何从 WooCommerce 中的 Betheme 图像中删除产品永久链接图标

可以在 JavaFX CSS 中同时使用 dropshadow 和 insideshadow 吗?

Firefox未正确设置SVG的父div的宽度

如何使我的标语(h1元素)不居中对齐?

为什么我的 CSS 转换在 React 18 中不起作用

在变量中存储匹配 Select 器的子元素的计数

如何使用 ReactJS 使用 CSS 设置 map 容器的样式

用 svelte 设计 body 元素

CSS:将背景图像拉伸到屏幕的 100% 宽度和高度?

如何在不使用浮动的情况下垂直对齐 div?

带有 flexbox 的 css 正方形网格

虚拟键盘激活时的屏幕样式

使用 CSS go 除图像之间的空间

使图像具有响应性 - 最简单的方法