EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

CSS Selector nth Child

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » CSS Tutorial » CSS Selector nth Child

CSS Selector nth Child

Introduction to CSS Selector nth Child

The Nth-child selector in CSS is allowing you to select one or more than one elements based on their given source order. This Nth-child selector is used to style the HTML elements in particular way by applying a formula to the Nth-child in CSS.

Real Time Example: Let suppose we have a calendar in that we have 12 months, in each months 4 to 5 weeks are there. If all rows or columns have same color it is not easy to identify the all dates accurately, if we have each row or column have different color alternatively then we can easily make differentiate even and odd weeks easily. For this, we can color even rows gray in color and odd rows in white in color. This you can observe below.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

tr:nth-child(even)
{
background: lightgray;
}
tr:nth-child(odd)
{
background:white;
}

Output:

Real Time Example

How Does nth Child Selector Work in CSS?

Nth-child selector in CSS worked based on the formula given in selector. The Nth-child selector will take single argument that is an integer. This integer can be in even number or odd number or any formula. Based on the formula selector will check all the all child elements for apply the CSS styles to that matching element.

Syntax:

<section class="parent">
<article class="child">One</article>
<article class=" child ">Two</article>
<article class=" child ">Three</article>
<article class=" child ">Four</article>
<article class=" child ">Five</article>
<article class=" child ">Six</article>
<article class=" child ">Seven</article>
<article class=" child ">Eight</article>
</section>
.child:nth-child(4n) {
Color:green;
}

Explanation: The above child:nth-child selector apply the color to all the 4n elements (4,8,12,16……) means <article class=” child “>Four</article>and<article class=” child “>Eight</article>.

Note: Internet Explorer and below version not support this concept.

Examples to Implement CSS Selector nth Child

Below are the examples of CSS Selector nth Child:

Example#1 – nth-child Selector with Even Positions

Code:

<!DOCTYPE html>
<html>
<head>
<!--CSS Styles-->
<style>
p:nth-child(even) /*apply the styles to only even paragraphs content*/
{
background: red;
color:white;
font-size: 20px;
}
h1
{
color:blue;
text-align: center;
}
.div
{
border: solid 4px green;
}
</style>
</head>
<body>
<div class="div">
<h1>Apply the CSS styles to Nth selector Even Places</h1>
<p>The Nth-child selector in CSS is allowing you to select one or more than one elements based on their given source order. This Nth-child selector is used to style the HTML elements in particular way by applying a formula to the Nth-child in CSS.</p>
<p>Real Time Example: Let suppose we have a calendar in that we have 12 months, in each months 4 to 5 weeks are there. If all rows or columns have same color it is not easy to identify the all dates accurately, if we have each row or column have different color alternatively then we can easily make differentiate even and odd weeks easily. For this we can color even rows gray in color and odd rows in white in color. This you can observe below.</p>
<p>Nth-child selector in CSS worked based on formula given in selector. </p>
<p>The Nth-child selector will take single argument that is an integer. This integer can be in even number or odd number or any formula. Based on formula selector will check all the all child elements for apply the CSS styles to that matching element.</p>
</div>
</body>
</html>

Popular Course in this category
CSS Training (9 Courses, 9+ Projects)9 Online Courses | 9 Hands-on Projects | 61+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,429 ratings)
Course Price

View Course

Related Courses
Bootstrap Training (2 Courses, 6+ Projects)jQuery Training (8 Courses, 5 Projects)

Output:

CSS Selector nth Child Example 2

Example #2 – nth-child Selector with Odd Positions

Code:

<!DOCTYPE html>
<html>
<head>
<!--CSS Styles-->
<style>
p:nth-child(odd) /*apply the styles to only odd paragraphs content*/
{
font-size: 20px;
background: green;
color: white;
}
h1
{
color: maroon;
text-align: center;
}
.div
{
border: double 4px brown;
}
</style>
</head>
<body>
<div class="div">
<h1>Apply the CSS styles to Nth selector Odd Places</h1>
<p>The Nth-child selector in CSS is allowing you to select one or more than one elements based on their given source order. This Nth-child selector is used to style the HTML elements in particular way by applying a formula to the Nth-child in CSS.</p>
<p>Real Time Example: Let suppose we have a calendar in that we have 12 months, in each months 4 to 5 weeks are there. If all rows or columns have same color it is not easy to identify the all dates accurately, if we have each row or column have different color alternatively then we can easily make differentiate even and odd weeks easily. For this we can color even rows gray in color and odd rows in white in color. This you can observe below.</p>
<p>Nth-child selector in CSS worked based on formula given in selector. </p>
<p>The Nth-child selector will take single argument that is an integer. This integer can be in even number or odd number or any formula. Based on formula selector will check all the all child elements for apply the CSS styles to that matching element.</p>
</div>
</body>
</html>

Output:

CSS Selector nth Child Example 3

Example #3 – nth-child Selector with Formula

Code:

<!DOCTYPE html>
<html>
<head>
<!--CSS Styles-->
<style>
p:nth-child(2n+1)/*apply the styles to only 2n+1 places paragraphs content like 2*1+1=3, 2*2+1=5, 2*3+1=7 etc.*/
{
font-size: 20px;
background: orange;
color: navy;
}
h1
{
color: navy;
text-align: center;
}
.div
{
border: double 4px fuchsia;
}
</style>
</head>
<body>
<div class="div">
<h1>Apply the CSS styles to Nth selector based on Formula</h1>
<p>First Paragraph</p>
<p>Second Paragraph</p>
<p>Third Paragraph</p>
<p>Fourth Paragraph</p>
<p>Fifth Paragraph</p>
<p>Sixth Paragraph</p>
<p>Seventh Paragraph</p>
<p>Eigth Paragraph</p>
<p>Ninth Paragraph</p>
<p>Tenth Paragraph</p>
</div>
</body>
</html>

Output:

Example 4

Conclusion

Nth child selector in CSS is used to style the elements at that position specified. We can apply this nth child selector with any HTML elements like paragraphs( <p>), headers(<h1>,<h2>,…), tables( <tr>,<th>,..), footers (<footer>) etc. We can apply this nth child selector with odd, even, formula related numbers.

Recommended Articles

This is a guide to CSS Selector nth Child. Here we discuss a brief overview on CSS Selector nth Child and its different examples along with its code implementation. You can also go through our other suggested articles to learn more –

  1. Uses of CSS in Technology
  2. Introduction to Top 6 Advantages of CSS
  3. Guide to Top 10 CSS Interview Questions
  4. Some Property of Button in CSS

CSS Training (9 Courses, 9+ Projects)

9 Online Courses

9 Hands-on Projects

61+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
CSS Tutorial
  • CSS
    • Introduction To CSS
    • What is CSS?
    • Uses Of CSS
    • Advantages of CSS
    • Career In CSS
    • CSS Commands
    • Is Css Case Sensitive
    • CSS object-fit
    • Types of CSS Selectors
    • CSS Radio Button
    • CSS Attribute Selector
    • CSS first child of class
    • CSS Selector nth Child
    • CSS Parent Selector
    • CSS Child Selector
    • CSS Not Selector
    • CSS Descendant Selector
    • CSS Inline Style
    • Checkbox CSS
    • CSS Appearance
    • CSS Font Properties
    • CSS font-variant
    • CSS Pagination
    • CSS Table Styling
    • CSS Table Cell Padding
    • CSS Padding Color
    • CSS Text Formatting
    • CSS for Text-Shadow
    • CSS text-stroke
    • CSS text-indent
    • CSS Rotate Text
    • CSS Text Color
    • CSS Center Div
    • CSS Arrow
    • CSS Arrow Down
    • CSS offset
    • CSS Cursor
    • CSS Layout
    • CSS Grid Layout
    • Button in CSS
    • CSS Button Border
    • text-align in CSS
    • CSS Horizontal Align
    • CSS Position
    • CSS Box Sizing
    • CSS box-shadow
    • CSS Text Underline
    • CSS Text Outline
    • CSS Blinking Text
    • Text Decoration CSS
    • CSS Vertical Align
    • CSS Word Wrap
    • CSS Padding
    • CSS Font Color
    • CSS Color Generator
    • CSS Margin Right
    • CSS Margin Color
    • CSS Color Codes
    • CSS Color Transparent
    • CSS Color Chart
    • CSS Link Color
    • CSS z-index
    • CSS Curved Border
    • CSS Border Left
    • CSS left
    • CSS Gradient Generator
    • Radial Gradient in CSS
    • CSS Shape Generator
    • CSS Triangle Generator
    • CSS background-color
    • CSS Background Image
    • CSS background-clip
    • CSS background-blend-mode
    • CSS Drop Shadow
    • CSS line height
    • CSS line break
    • Sticky Footer CSS
    • CSS Header Design
    • CSS Border Style
    • CSS Border Generator
    • Sticky Sidebar CSS
    • CSS Transparent Border
    • CSS Border Radius
    • CSS translate
    • CSS transform
    • CSS 3D Transforms
    • CSS Text Transform
    • CSS Transition Effects
    • CSS Transition Property
    • CSS Animation Transition
    • Negative Margin CSS
    • CSS Navigation Bar
    • CSS Overflow
    • CSS overflow-wrap
    • CSS Lists
    • CSS list-style
    • CSS Order
    • CSS Box Model
    • CSS Inner Border
    • CSS Icon
    • Menu Icon CSS
    • CSS Multiple Borders
    • Opacity in CSS
    • CSS Float Right
    • CSS Clear Float
    • CSS clip
    • CSS disabled
    • CSS Border Padding
    • Border Images in CSS
    • CSS Visibility
    • CSS Validator
    • CSS Clearfix
    • CSS Counter
    • CSS Letter Spacing
    • CSS root
    • CSS zoom
    • CSS calc()
    • CSS.supports()
    • CSS Loader
    • Media Query CSS
    • CSS @keyframes
    • CSS @bottom
    • CSS page-break-after Property
    • CSS page-break
    • CSS Position Fixed
    • CSS skew()
    • CSS Row
    • CSS Masking
    • CSS Scrollbar
    • CSS Overlay
    • CSS Important
    • CSS Cursor Hand
    • CSS Inherit
    • CSS Position Relative
    • CSS Compressor
    • CSS tricks
    • CSS Outline Property
    • CSS Flexbox Properties
    • CSS flex-direction
    • CSS content property
    • CSS Typography
    • CSS Formatter
    • CSS nowrap
    • CSS Column
    • GridView CSS
    • CSS Viewport
    • CSS Minify
    • CSS Combinators
    • CSS in React
    • CSS Matrix 
    • CSS Pseudo Elements
    • CSS Pseudo Classes
    • CSS Pointer Events
    • CSS Resize
    • CSS Inheritance
    • CSS Interview Questions
    • Cheat Sheet CSS
  • CSS3
    • What is CSS3?
    • CSS3 Interview Questions
    • Cheat sheet CSS3
  • sass
    • How to Install SASS
    • SASS Interview Questions
    • What is Sass
    • SASS Comments
    • Sass Variables
    • SASS Import
    • SASS if else
    • SASS Nesting
    • SASS @each
    • SASS @at-root
    • SASS @extend
    • SASS @media
    • SASS @for
    • SASS Map
    • SASS Selectors
    • SASS Color Functions
    • SASS Mixins

Related Courses

CSS Training Course

Bootstrap Training Course

JQuery Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - CSS Training (9 Courses, 9+ Projects) Learn More