Skip to content

Instantly share code, notes, and snippets.

@travhimself
Created June 12, 2015 03:50
Show Gist options
  • Select an option

  • Save travhimself/133fe954ab73236f4fe3 to your computer and use it in GitHub Desktop.

Select an option

Save travhimself/133fe954ab73236f4fe3 to your computer and use it in GitHub Desktop.
Explanation of :nth-child
div:nth-child(xn+y) {
/*
this evaluates like "x*n+y"
x is cycle size
n represents a counter, starting at 0
y is an offset
*/
}
div:nth-child(3) {
/* third child only */
}
div:nth-child(3n) {
/*
every third child
3*0 (0th child... doesn't exist)
3*1 (3rd child)
3*2 (6th child)
3*3 (9th child)
*/
}
div:nth-child(3n+2) {
/*
every third child, offset by 2
3*0+2 (2nd child)
3*1+2 (5th child)
3*2+2 (8th child)
3*3+2 (11th child)
*/
}
div:nth-child(-n+3) {
/*
first three children
-0+3 (3rd child)
-1+3 (2nd child)
-2+3 (1st child)
-3+3 (0th child... doesn't exist)
-4+1 (-1st child... doesn't exist)
...and so
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment