Saturday , February 22 2025

How to colorize every second or odd element in css

I used the most popular query for my practice in the post title, but there are more options in the table below.

Options for :nth-child

1 1 The first element, is a synonym for the :first-child pseudo-class
5 5 5th element
2n 2, 4, 6, 8, 10 All even elements, equivalent to even
2n+1 1, 3, 5, 7, 9 All odd elements, equivalent to odd
3n+2 2, 5, 8, 11, 14
-n+3 3, 2, 1
5n-2 3, 8, 13, 18, 23
even 2, 4, 6, 8, 10 All even elements
odd 1, 3, 5, 7, 9 All odd elements

How to use:

div.test p:nth-child(2n) {font-size: 2em;}
.block .intro:nth-child(2n+1) {background: #000;}

About iryna

I'm Iryna, a web developer from Ukraine with a decade of experience solving complex technical challenges in the world of freelance. Throughout my career, I've worked on everything from troubleshooting server-side issues and optimizing website performance to enhancing user interfaces. On this blog, I share detailed solutions to the technical problems I’ve encountered and methods that have worked best for me. In addition to my technical expertise, I’m also passionate about digital drawing. I hope the tutorials and insights I provide here will help both fellow developers and creatives alike in their own projects.

Check Also

Css styles for WordPress admin in functions.php

If you need to add individual styles, and they are only a couple of lines, …

Leave a Reply

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