文本行的斑马条纹

周知,表格的斑马条纹只需简单的运用伪类 :nth-child()/:nth-of-type()来处理就好了:

tr:nth-child(even){

  background-color: rgba(0,0,0,.2);

}

但是把这种效果运用在文本行的时候就有点力不从心了,尽管js可以实现这种功能,但是这在理论上有违纯粹原则(javascript不应该掺合到样式层面来),

而且过多的DOM元素还会拖累整个页面的性能。

解决方案:

<div class="content">
原来你是这样的毛玻璃 <br>
原来你是这样的毛玻璃 <br>
原来你是这样的毛玻璃 <br>
原来你是这样的毛玻璃
</div>

.content{
text-align: center;
font-weight: 100;
padding:.5em;
line-height: 1.5;
background: beige;
background-size: auto 3em;
background-origin: content-box;
background-image: linear-gradient(rgba(0,0,0,.2) 50%, transparent 0);
}


效果:

         

 注意:改变 line-height 时要相应的调整 background-size 。


原文地址:https://www.cnblogs.com/Man-Dream-Necessary/p/6672013.html