使用nth-child的5行颜色块

时间:2016-04-15 17:35:58

标签: css

我正在尝试使用nth-child为每组其他5行着色。 下面的代码有效,但我并不总是知道总行数。 它们总是5的倍数。

http://jsfiddle.net/gbgcxo2p/

p:nth-child(n+8):nth-child(-n+12),:nth-child(n+18):nth-child(-n+22) {
    background: #ff0000;
}
<h1>This is a heading</h1>
<p>The 1 paragraph.</p>
<p>The 2 paragraph.</p>
<p>The 3 paragraph.</p>
<p>The 4 paragraph.</p>
<p>The 5 paragraph.</p>
<p>The 6 paragraph.</p>
<p>The 7 paragraph.</p>
<p>The 8 paragraph.</p>
<p>The 9 paragraph.</p>
<p>The 10 paragraph.</p>
<p>The 11 paragraph.</p>
<p>The 12 paragraph.</p>
<p>The 13 paragraph.</p>
<p>The 14 paragraph.</p>
<p>The 15 paragraph.</p>
<p>The 16 paragraph.</p>
<p>The 17 paragraph.</p>
<p>The 18 paragraph.</p>
<p>The 19 paragraph.</p>
<p>The 20 paragraph.</p>
<p>The 21 paragraph.</p>
<p>The 22 paragraph.</p>
<p>The 23 paragraph.</p>
<p>The 24 paragraph.</p>
<p>The 25 paragraph.</p>

有没有办法使用nth-child来执行此操作,无论行数是多少?

斯科特

1 个答案:

答案 0 :(得分:3)

您可以尝试此操作(更好地使用nth-of-type)。

p:nth-of-type(10n + 6),
p:nth-of-type(10n + 7),
p:nth-of-type(10n + 8),
p:nth-of-type(10n + 9),
p:nth-of-type(10n + 10) {
  background: red;
}
<h1>This is a heading</h1><p>The 1 paragraph.</p><p>The 2 paragraph.</p><p>The 3 paragraph.</p><p>The 4 paragraph.</p><p>The 5 paragraph.</p><p>The 6 paragraph</p><p>The 7 paragraph.</p><p>The 8 paragraph.</p><p>The 9 paragraph.</p><p>The 10 paragraph.</p><p>The 11 paragraph.</p><p>The 12 paragraph.</p><p>The 13 paragraph.</p><p>The 14 paragraph.</p><p>The 15 paragraph.</p><p>The 16 paragraph.</p><p>The 17 paragraph.</p><p>The 18 paragraph.</p><p>The 19 paragraph.</p><p>The 20 paragraph.</p><p>The 21 paragraph.</p><p>The 22 paragraph.</p><p>The 23 paragraph.</p><p>The 24 paragraph.</p><p>The 25 paragraph.</p>