The :nth-child() selector in CSS is used to match the elements based on their position in a group of siblings. It matches every element that is the nth child. The :even and :odd pseudo-class is used with the list of items such as paragraph, article items which is basically a list content.
-
odd: The use of odd pseudo-class in any list item that will effect only the odd index number list.
Syntax:li:nth-child( odd ) { // CSS Property }Example:
<!DOCTYPE html><html><head><title>CSS :nth-child(odd) Selector</title><!-- Style to uses :nth-child(odd)Selector --><style>li:nth-child(odd) {background: green;font-size: 24px;color:white;}</style></head><body><li>GeeksforGeeks</li><li>A Computer Science portal</li><li>Welcome to GeeksforGeeks</li></body></html>chevron_rightfilter_noneOutput:
- even: The use of even pseudo-class in any list item that will effect only the even index number list.
Syntax:li:nth-child( even ) { // CSS Property }Example:
<!DOCTYPE html><html><head><title>CSS :nth-child(even) Selector</title><!-- Style to uses :nth-child(odd)Selector --><style>li:nth-child(even) {background: green;font-size: 24px;color:white;}</style></head><body><li>GeeksforGeeks</li><li>A Computer Science portal</li><li>Welcome to GeeksforGeeks</li></body></html>chevron_rightfilter_noneOutput:
- Example: This example uses both :even and :odd pseudo-class selector.
<!DOCTYPE html><html><head><style>li:nth-child(odd) {background: green;font-size: 36px;color:white;}li:nth-child(even) {background: Blue;font-size: 36px;color:yellow;}</style></head><body><h1>GeeksforGeeks</h1><h2>:even and :odd pseudo-class</h2><li>Data Structure</li><li>Operating System</li><li>Compuyer Networks</li><li>C Programming</li></body></html>chevron_rightfilter_noneOutput:

Recommended Posts:
- How to prevent line breaks in the list of items using CSS?
- How to set vertical space between the list of items using CSS ?
- CSS Grid and place-items together
- CSS | align-items Property
- CSS | Ordering Flex Items
- CSS place-items Property
- Python | Count number of items in a dictionary value that is a list
- How to Add Image into Dropdown List for each items ?
- How to use a not:first-child selector in CSS?
- What is the best way to include CSS file? Why use @import?
- What is the use of star-preceded property in CSS ?
- What is the use of asterisk (*) selector in CSS ?
- How to use text as background using CSS ?
- PHP | Separate odd and even elements from array without using loop
- PHP | check if a number is Even or Odd
- Difference between align-content and align-items
- How to fix number of visible items in HTML dropdown and allow multiple selections ?
- Difference between bootstrap.css and bootstrap-theme.css
- Count ways to distribute m items among n people
- Minimum number of items to be delivered
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

