CSS :nth-child() Selector

You are Here:

CSS :nth-child() Selector

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> p:nth-child(3){ color: red; } </style> </head> <body> <h1>CSS :nth-child() Selector</h1> <h2>Heading 2</h2> <p>This is 3rd child (p) of its parent (body).</p> </body> </html>

Syntax

Using CSS

:nth-child(number) { color: red; }

More Examples

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> p:nth-child(odd){ color: red; } p:nth-child(even){ color: blue; } </style> </head> <body> <h1>CSS :nth-child() Selector</h1> <h2>Heading 2</h2> <p>This is 3rd(odd) child (p) of its parent (body).</p> <p>This is 4th(even) child (p) of its parent (body).</p> </body> </html>

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> p:nth-child(3n+0){ color: red; } </style> </head> <body> <h1>CSS :nth-child() Selector</h1> <h2>Heading 2</h2> <p>This is 3rd child (p) of its parent (body).</p> <p>This is 4th child (p) of its parent (body).</p> <p>This is 5th child (p) of its parent (body).</p> <p>This is 6th child (p) of its parent (body).</p> <p>This is 7th child (p) of its parent (body).</p> <p>This is 8th child (p) of its parent (body).</p> <p>This is 9th child (p) of its parent (body).</p> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author