本質的に階層データではないものを、非階層的なHTML構造で表現しようとしています。 nth-child
は、親クラス内の子の順番を指し、共有クラスを持つすべての子クラス内ではありません。基本的に、CSSではこれを行うことはできません。
は、可能な場合は、このようなあなたのHTMLを構築してみてください。あなたはイベントと同じ色でコメントを開始したいように見えるので
<div>
<div class="event">
<div class="comment"></div>
</div>
<div class="event">
<div class="comment"></div>
<div class="comment"></div>
<div class="comment"></div>
</div>
<div class="event">
</div>
</div>
今、あなたは
.event:nth-child(odd) { color: white; }
.event:nth-child(even) { color: black; }
.comment:nth-child(odd) { color: white; }
.comment:nth-child(even) { color: black; }
でこれをスタイルすることができますそれはあなたがする必要があるだろうの一部で、次のとおりです。
.event:nth-child(odd) { color: white; }
.event:nth-child(odd) .comment:nth-child(odd) { color: white; }
.event:nth-child(odd) .comment:nth-child(even) { color: black; }
.event:nth-child(even) { color: black; }
.event:nth-child(even) .comment:nth-child(odd) { color: black; }
.event:nth-child(even) .comment:nth-child(even) { color: white; }
上記の使用div
要素。そして、書き込み
<table>
<thead><tr><td>Event</td></tr></thead>
<tbody>
<tr><td>Comment</td></tr>
</tbody>
<thead <tr><td>Event</td></tr></thead>
<tbody>
<tr><td>Comment</td></tr>
<tr><td>Comment</td></tr>
<tr><td>Comment</td></tr>
</tbody>
<thead><tr><td>Event</td></tr></thead>
</table>
:あなたは本当にテーブルでこれを行うにしたい場合しかし、あなたは以下のHTMLを使用してみてください可能性があり、その後、上記のように、論理の同じ種類を使用し
thead:nth-of-type(odd) { color: white; }
tbody:nth-of-type(even) tr:nth-child(odd) { color: white; }
tbody:nth-of-type(odd) tr:nth-child(event) { color: black; }
thead:nth-of-type(even) { color: black; }
tbody:nth-of-type(even) tr:nth-child(odd) { color: black; }
tbody:nth-of-type(odd) tr:nth-child(event) { color: white; }
'n番目の-child'はなく、いくつかの共有クラスを持つすべての子供たちの中に、親内の子の順序を指します。基本的に、CSSではこれを行うことはできません。 –