HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcssTip

Zebra striped list or table

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
stripedzebratablecsslist

Problem

Zebra striped lists and tables are a great way to improve the readability of your content. By using the :nth-child(odd) or :nth-child(even) pseudo-class selectors, you can apply different background colors to elements based on their position in a group of siblings.
> [!TIP]
>
> You can use this trick to apply different styles to any other elements that are part of a group of siblings.
https://codepen.io/chalarangelo/pen/VwoLNMR

Solution

/* Zebra striped list */
li:nth-child(odd) {
  background-color: #ccc;
}

/* Zebra striped table */
tr:nth-child(even) {
  background-color: #ccc;
}


>
> You can use this trick to apply different styles to any other elements that are part of a group of siblings.
https://codepen.io/chalarangelo/pen/VwoLNMR

Code Snippets

/* Zebra striped list */
li:nth-child(odd) {
  background-color: #ccc;
}

/* Zebra striped table */
tr:nth-child(even) {
  background-color: #ccc;
}

Context

From 30-seconds-of-code: zebra-striped-list-or-table

Revisions (0)

No revisions yet.