Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML How to Make a Website Responsive Web Design and Testing Build a Three Column Layout

Vanessa Elliott
Vanessa Elliott
1,742 Points

nth-child definition confusion

I thought I understood what the nth-child was while watching the video, but reading the definition given in the notes makes me feel really confused about it now.

It says ":nth-child(An+B) - The nth-child pseudo-class matches A(n)+B-1 siblings in the document tree. The A value that precedes n is required. By itself, this will match every "nth" element in a group. Both A and B must be integers."

But why does it say B-1? I understand what A(n) means, and what A(n)+B would mean, but why the -1? In the code suggestion it says you can put 3n+1 for when you have more than 5 items, but wouldn't that just mean it's now 3n+1-1? So it's just 3n?

Sorry if this seems nitpicky, it just really confuses me why there'd be a random -1, therefore making the +1 pointless. I'm probably just not understanding it.

1 Answer

Steven Parker
Steven Parker
230,230 Points

I like to think of it this way: The number before the "n" is the repeat interval. So "3n" would mean "every 3rd item". The (optional) number after the "+" is the starting item. So "3n+1" means "every 3rd item starting with item 1".

So some examples of the sequence of items affected:

  • 3n -- 3, 6, 9, 12, etc.
  • 3n+1 -- 1, 4, 7, 10, etc.
  • 2n+3 -- 3, 5, 7, 9, etc.

Does that help?

Vanessa Elliott
Vanessa Elliott
1,742 Points

Ohhhhh yes that makes sense now. Thank you very much!