30秒学会 CSS 片段 · 2020年5月16日

30秒学会 CSS 片段 – Zebra striped list

Creates a striped list with alternating background colors, which is useful for differentiating siblings that have content spread across a wide row.

  • Use the :nth-child(odd) or :nth-child(even) pseudo-class to apply a different background color to elements that match based on their position in a group of siblings.
  • Note that you can use it to apply different styles to other HTML elements like div, tr, p, ol, etc.

预览

  • Item 01
  • Item 02
  • Item 03
  • Item 04
  • Item 05



HTML

<ul>
  <li>Item 01</li>
  <li>Item 02</li>
  <li>Item 03</li>
  <li>Item 04</li>
  <li>Item 05</li>
</ul>

CSS

li:nth-child(odd) {
  background-color: #ddd;
}

翻译自:https://www.30secondsofcode.org/css/s/zebra-striped-list