Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In beginner CSS, you likely encountered :nth-child(odd) and :nth-child(even) to color alternating zebra stripes on a student marks table. But the true power of structural pseudo-classes goes far beyond simple alternating stripes.
Think of a school morning assembly line:
With the mathematical An + B syntax, :nth-last-child(), and the modern :nth-child(An+B of .selector) syntax, you can execute surgical, programmatic selections of DOM elements without touching a single line of JavaScript!
An + BEvery :nth-child() formula evaluates using the linear mathematical equation:
$$\text{Index} = (A \times n) + B$$
Where:
A: The cycle step multiplier (how many elements to jump).n: An internal integer counter that always starts at 0 and counts upwards: 0, 1, 2, 3, 4, 5...B: The starting offset (using 1-based indexing, where 1 is the very first child).How data and code flow step-by-step
By using negative multipliers (-n), you can create reverse countdowns and bounded ranges:
N Elements (-n + N)Want to highlight only the top 4 rank holders in an exam merit list?
N (n + N)Want to dim or style all students following the 5th item?
What if you want to select students between position 4 and position 8 (inclusive)? Simply chain the two formulas together!
:nth-last-child()What if you don't know how many students are in the database, but you need to style the last 3 items at the bottom of the list?
:nth-last-child() counts backwards from the end of the parent:
:nth-child vs :nth-of-typeOne of the most frequent bugs in CSS layout occurs when developers mix headings or dividers into a container:
:nth-child(N): Evaluates position strictly by index among all sibling elements, regardless of tag name.:nth-of-type(N): Filters strictly by HTML tag name (p, div, li), counting only elements of that specific type.:nth-child(An+B of .selector)In the modern CSS Selectors Level 4 specification (now supported in all modern browsers), you can target the nth occurrence of elements matching a specific class filter, ignoring all other classes:
Before this syntax, developers had to write complex JavaScript loops or add manual classes (class="even-verified") to accomplish this!
In a 3-column product or student dashboard grid:
:nth-child(3n + 1):nth-child(3n + 2):nth-child(3n) (or 3n + 3)| Practice | Bad Approach | Good Approach | Why It Matters |
|---|---|---|---|
| Selecting Top N | Creating manual classes like .item-1, .item-2, .item-3 | Using :nth-child(-n + 3) | CSS formulas adapt dynamically if content order or items change. |
| Mixed Elements | Expecting p:nth-child(1) to work when preceded by <h1> | Using p:nth-of-type(1) | :nth-child(1) fails if the very first element in the container is not a <p>. |
| Class Filtering | Adding .even-active classes via JavaScript | Using :nth-child(even of .active) | Eliminates unnecessary DOM manipulation and keeps styling in pure CSS. |
| Formula Spacing | Writing :nth-child(2 n + 1) with spaces around n | Writing :nth-child(2n + 1) | An must be a contiguous token without whitespace. |
An + B Formula: A is the frequency jump; B is the starting offset; n starts at 0.:nth-child(-n + N) (e.g. -n + 5 for first 5).:nth-child(n + N) (e.g. n + 6 for 6th onwards).:nth-child(n + 3):nth-child(-n + 7).:nth-last-child(-n + 3) for the last 3 elements in the container.:nth-child(An + B of .target-class).div:nth-child(1) and div:nth-of-type(1) inside a container where the first child is an <h1>?:nth-last-child(1) target?:nth-child(even of .featured) syntax?Build an interactive school examination merit board where different An + B formulas highlight top-tier rankers and alternate zebra rows.
:nth-child(-n + 3) to give the top 3 medal winners a gold gradient background and a medal badge.:nth-child(n + 4):nth-child(-n + 6) to give students ranked 4th to 6th a silver distinction badge.:nth-last-child(-n + 2) to style the final two rows with a dashed encouragement boundary.Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.