设置opacity属性的时候,有时候我们只需要当前的元素透明度变化,而不需要子级标签变化,但是我们设置的时候,会发现一个问题,原本写在当前标签中的opacity属性,会让子级的透明度也随着变化,真是头疼,那么有什么好的解决方法吗?当然有,下面贴出代码:
问题重现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <style type="text/css">
#demo {
width: 100px;
height: 100px;
background: red;
opacity: .1;
}
.opacity {
width: 50px;
height: 50px;
background: red;
}
.red {
width: 100px;
height: 100px;
background: red;
}
</style>
<div id="demo">
<div class="opacity">
</div>
</div>
<div class="red">
</div> |
这时候,我们发现我们父级设置的属性影响到了子级标签,原因就是父级将opacity属性又传递了给了子级,下面我们来解决它吧。
解决方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <style type="text/css">
#demo {
width: 100px;
height: 100px;
background: #000;
background: rgba(0, 0, 0, .1);
}
.opacity {
width: 50px;
height: 50px;
background: #000;
}
.red {
width: 100px;
height: 100px;
background: #000;
}
</style>
<div id="demo">
<div class="opacity">
</div>
</div>
<div class="red">
</div> |
现在我们就可以看到,父级设置的opacity属性就没有传递给子级了,将opacity写法换为background: rgba(0, 0, 0, .1);写法就行了rgb表示的是颜色,a表示的就是透明度了。
「梦想一旦被付诸行动,就会变得神圣,如果觉得我的文章对您有用,请帮助本站成长」
共 0 条评论关于"css3设置opacity属性影响了子级标签的解决方法"
最新评论