是否可以在不影响子元素不透明度的情况下设置背景图像的不透明度?
例子
页脚中的所有链接都需要一个自定义元素符号(背景图像)并且自定义元素符号的不透明度应为 50%。
HTML
<div id="footer">
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
<li><a href="#">Link 4</a></li>
<li><a href="#">Link 5</a></li>
</ul>
</div>
CSS
#footer ul li {
background: url(/images/arrow.png) no-repeat 0 50%;
}
我尝试过的
我尝试将列表项的不透明度设置为 50%,但是链接文本的不透明度也是 50% - 并且似乎没有办法重置子元素的不透明度:
#footer ul li {
background: url(/images/arrow.png) no-repeat 0 50%;
/* will also set the opacity of the link text */
opacity: 0.5;
}
我也试过使用 rgba,但这对背景图像没有任何影响:
#footer ul li {
/* rgba doesn't apply to the background image */
background: rgba(255, 255, 255, 0.5) url(/images/arrow.png) no-repeat 0 50%;
}
最佳答案
您可以将 CSS linear-gradient()
与 rgba()
一起使用。
div {
width: 300px;
height: 200px;
background: linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,.5)), url("https://i.imgur.com/xnh5x47.jpg");
}
span {
background: black;
color: white;
}
<div><span>Hello world.</span></div>
关于css - 在不影响子元素的情况下设置背景图片的不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4997493/