英文:
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 -->
<div class="rect">
<div class="rect1"></div>
</div>
<!-- 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 -->
<div class="rect">
<div class="rect1"></div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论