我需要改变所有 child 的背景颜色,逐渐增加不透明度。 child 的数量是动态的,将异步插入或删除。是否有任何解决方案可以使用 css 或更少实现相同的效果。
ul li {
list-style: none;
font-size: 16px;
text-align: center;
font-weight: bold;
margin: 3px 0;
}
ul li:nth-child(1){
background-color: rgba(0,0,255, 0.1);
}
ul li:nth-child(2){
background-color: rgba(0,0,255, 0.2);
}
ul li:nth-child(3){
background-color: rgba(0,0,255, 0.3);
}
ul li:nth-child(4){
background-color: rgba(0,0,255, 0.4);
}
ul li:nth-child(5){
background-color: rgba(0,0,255, 0.5);
}
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ul>
最佳答案
添加了这些样式
ul li {
box-shadow: 2px 2px white, 0px 2px white;
}
ul {
padding: 0;
background-image: linear-gradient(rgba(0, 0, 255, 0.1), rgba(0, 0, 255, 0.8));
}
不是给每个 li 着色,而是给 ul 一个背景渐变,并用 box-shadow 创建一个盒子效果。
ul li {
list-style: none;
font-size: 16px;
text-align: center;
font-weight: bold;
margin: 3px 0;
box-shadow: 2px 2px white, 0px 2px white;
}
ul {
padding: 0;
background-image: linear-gradient(rgba(0, 0, 255, 0.1), rgba(0, 0, 255, 0.8));
}
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
<li>H</li>
</ul>
关于html - 不透明度增加的背景颜色使用更少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53256825/