CSS nth child Selectors
- We can use the nth-child selector to target specific element by their index number.
- We can target odd or even numbered elements
- We can target patterns of elements, like every 3rd paragraph.
main p:nth-child(2) {
/*targets a paragraph inside each <main> element.
if the 2nd child is a paragraph */
}
p:nth-child(odd) {
/* targets all the odd numbered paragraphs */
}
p:nth-child(3n) {
/* targets every 3rd paragraph */
}
p:first-child {
/* targets the first paragraph on the page */
}
p:last-child {
/* targets the last paragraph on the page */
}
p:nth-of-type(3) {
/* targets the third paragraph, regardless of
whether or not there are other siblings of the
paragraph which are not paragraphs */
}
- CSS Tricks has a great page that explains how to create nth-child patterns
- CSS-Tricks tutorial on nth-child selectors