是否可以设置背景图像的不透明度而不影响子元素的不透明度?
页脚中的所有链接都需要自定义项目符号(背景图像),并且自定义项目符号的不透明度应为 50%。
<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>
#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%;
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您可以将 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; }