如何在CSS中覆盖父级的样式?

huangapple go评论50阅读模式
英文:

How to override parent's styles in css?

问题

在这个示例中,我想要将紫色矩形的不透明度更改为100%,而不考虑父级的值。我尝试使用all: unset/initial和!important,但似乎不起作用。

.rect {
    width: 500px;
    height: 600px;
    margin-top: 200px;
    margin-left: 300px;
    background-color: black;
    /* 这里 */
    opacity: 37%;
    z-index: -1;
}

.rect1 {
    all: unset;
    position: absolute;
    z-index: 10;
    width: 259px;
    height: 300px;
    margin-top: 500px;
    margin-left: 50px;
    background-color: purple;
    /* 到这里 */
    opacity: 100% !important;
}
<div class="rect">
  <div class="rect1"></div>
</div>

这段代码将紫色矩形的不透明度更改为100%,而不考虑父级的值。

英文:

In this example, I want the purple rectangle to change its opacity to 100% regardless of the value of the parent. I tried using all: unset/initial and !important but it doesn't seem to work.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.rect {
    width: 500px;
    height: 600px;
    margin-top: 200px;
    margin-left: 300px;
    background-color: black;
    /* this V */
    opacity: 37%;
    z-index: -1;
}

.rect1 {
    all: unset;
    position: absolute;
    z-index: 10;
    width: 259px;
    height: 300px;
    margin-top: 500px;
    margin-left: 50px;
    background-color: purple;
    /* to this V */
    opacity: 100% !important;
}

<!-- language: lang-html -->

&lt;div class=&quot;rect&quot;&gt;
  &lt;div class=&quot;rect1&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

所以正如Haworth指出的那样,在元素本身上使用opacity会使所有子元素受到用于创建opacity效果的像素着色的影响。

如果您想在保留HTML结构的同时获得相同的效果,我建议使用RGBA或带有alpha通道的十六进制值直接在background-color属性上。请参见下面的示例。

body {
  height: 100%;
  width: 100%;
  background: url(https://picsum.photos/800) no-repeat;
  background-size: cover;
}

.rect {
  width: 500px;
  height: 600px;
  margin-top: 200px;
  margin-left: 300px;
  background-color: rgba(0,0,0,.37);
  /* this V
  opacity: 37%;*/
  z-index: -1;
}

.rect1 {
  position: absolute;
  z-index: 10;
  width: 259px;
  height: 300px;
  margin-top: 500px;
  margin-left: 50px;
  background-color: purple;
  /* to this V */
  opacity: 100% !important;
}
<div class="rect">
  <div class="rect1"></div>
</div>
英文:

So like Haworth pointed out, using opacity on the element itself brings all children under the influence of the pixelshading used to make the opacity effect.

If you want to get the same effect while retaining your html structure I'd recommend a different approach for the same result using RGBA or hex with an alpha channel on the background-color property directly. See example below.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  height: 100%;
  width: 100%;
  background: url(https://picsum.photos/800) no-repeat;
  background-size: cover;
}

.rect {
    width: 500px;
    height: 600px;
    margin-top: 200px;
    margin-left: 300px;
    background-color: rgba(0,0,0,.37);
    /* this V
    opacity: 37%;*/
    z-index: -1;
}

.rect1 {
    position: absolute;
    z-index: 10;
    width: 259px;
    height: 300px;
    margin-top: 500px;
    margin-left: 50px;
    background-color: purple;
    /* to this V */
    opacity: 100% !important;
}

<!-- language: lang-html -->

&lt;div class=&quot;rect&quot;&gt;
  &lt;div class=&quot;rect1&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月2日 01:17:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定