How to add alternate background colors for list items with CSS?

Spread the love

Sometimes, we want to add alternate background colors for list items with CSS.

In this article, we’ll look at how to add alternate background colors for list items with CSS.

How to add alternate background colors for list items with CSS?

To add alternate background colors for list items with CSS, we use thr nth-child selector.

For instance, we write

li {
  background: green;
}

li:nth-child(odd) {
  background: red;
}

to set theg li background to green.

Then we override the color for the odd rows by setting the background to red.

Conclusion

To add alternate background colors for list items with CSS, we use thr nth-child selector.

Leave a Reply

Your email address will not be published. Required fields are marked *