×

CSS: nth-selector

CSS nth selector can be used to match an element according to its position regardless of its parent’s type. Here, n can be any number, formula, or a keyword.

These selectors are applied to match an element according to its position inside a sibling’s group. It also matches all the elements, which are the nth-child.

Syntax:

:nth-child(n) 
{  
  //CSS Property  
}  

 The “n” in the above syntax, is an argument which is specified in the parentheses. It represents a pattern to match the elements. It may be odd, even, or functional notation.

Odd values depict various components that are located in an odd series like 1, 3, and 5, whereas even values depict multiple components that are locates in an even series like 2, 4, and 6.

 Functional notation (An+B)- It depicts those components whose position of siblings match to the pattern An+B, Where:

A: It is the size of integer step.

n: It is a positive integer, i.e., begins from 0.

B: It is an integer offset.

Let’s consider some of the essential illustrations as follows:

Example 1:

In the following example, we use 3n+4 functional notations that will depict the elements:

(3×0)+4 = 4, (3×1)+4 =7, and many others.

<!DOCTYPE html>
<html>
<head>
<title> :nth-child Selector </title>
<style>
p:nth-child(3n+4)
{
 background: lime;
 color: black;
 font-size: 30px;
}
</style>
</head>
<body style= "text-align: center">
<h1> Hello World </h1>
<h2> Welcome to this Page </h2>
<p> It will not influenced. </p>
<p> It will be influenced. </p>
<p> It will not influenced. </p>
<p> It will not influenced. </p>
<p> It will be influenced. </p>
</body>
</html>

Output:

CSS: nth-selector

Example 2:

In the following example, we use even and odd keywords that are matching with those components whose index is even or odd. It is noticed that any first child’s index is 1.

<!DOCTYPE html>
<html>
<head>
<title> :nth-child Selector </title>
<style>
p:nth-child(even)
{
 background: lime;
 color: black;
 font-size: 30px;
}
p:nth-child(odd)
{
 background: blue;
 color: white;
 font-size: 20px;
}
</style>
</head>
<body style= "text-align: center">
<h1> Hello World </h1>
<h2> Welcome to this Page </h2>
<p> Odd </p>
<p> Even </p>
<p> Odd </p>
<p> Even </p>
<p> Odd </p>
</body>
</html>

Output:

CSS: nth-selector

Related Topics

CSS Tutorial

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

9 minutes read.

CSS Superscript and Subscript

Superscript and Subscript To specify the superscript and the subscript text, HTML provides us two tags that are <sup> and <sub>. The superscript content looks in the shorter font and half...

2 minutes read.

Css Text Orientation

Introduction: The text-orientation property of CSS is used to specify the orientation of the text characters in the line of content. This property applies only with the vertical writing-mode and not...

4 minutes read.

CSS Page-break-before Attribute

Page-break-before Attribute As its name implies, the page-break-before property is applied to include page break before any element, at the time of printing any document. It adds the page break before...

5 minutes read.

CSS Box-shadow

CSS Box-shadow This property is referred to include effects as shadow-like around an element’s frame. Syntax box-shadow: h-offset v-offset blur spread color |inset|inherit|none; Property values Following are the various property values which can be used...

3 minutes read.

Internal CSS

What is CSS? CSS stands for Cascading Style Sheet.CSS is used to apply styling the HTML elements.With CSS user can add or change color, font-size and font-family. It also helps user...

4 minutes read.

CSS Pagination

CSS Pagination The pagination in CSS is an advantageous approach for indexing of a website’s distinct pages over the homepage. When our site contains multiple pages, we must insert a few...

13 minutes read.

CSS Form

You can create attractive HTML form by using CSS. Style input Fields The Input Field width properties are used to determine the width of the input field. Let us see an example of CSS style...

2 minutes read.

CSS Images

Images in CSS: Images are a critical part of a web application. It includes various types of images in the web application that cannot be recommended. However, it is essential...

4 minutes read.

CSS Masking

CSS Masking The CSS mask property is applied to hide a component with the use of the masking or clipping an image at any specific point. The masking depicts the use...

3 minutes read.

CSS Introduction

CSS stands for Cascading Style Sheet. It is a design language which is used to design the web pages. It was developed by Håkon Wium Lie on October 10, 1994. It provides...

1 minute read.

How to change background color in CSS

How to change background color in CSS? The background-color property in CSS would be used to specify the color of an element's background. As the element's background, it uses solid colors. The...

3 minutes read.

How to edit CSS in WordPress

Developing a new website or blog is a difficult undertaking. We want to create something that the consumers will adore, but we might not want the setting to take months and...

7 minutes read.

How to center a button in CSS

How to center a button in CSS? In the tutorial, we will learn how to center a button in CSS. CSS (Cascading Style Sheets) gives an HTML web page the greatest possible...

4 minutes read.

Untitled Reusable Block

  ...

1 minute read.

CSS Gradient

CSS Gradient: The CSS gradient property is applied to show the smooth transition inside two specified colors or more than two specified colors. Need of CSS Gradient The following are a few...

4 minutes read.

CSS Specificity

CSS Specificity: If one or more conflicting rules of CSS express a similar element, a browser will pursue a few rules to examine the specific one. Specificity can be specified...

3 minutes read.

CSS Background Color

It is one of the CSS properties to apply styling to html elements.This property is used to apply the background color to any HTML element.The background of an element includes...

2 minutes read.

CSS Justify Content

Justify Content: These properties are applied for the alignment of items of a flexible box container if any item does not cover every available space over the main-axis, i.e., horizontally....

4 minutes read.

CSS Units

CSS Units In CSS, there are so many units available to show the length’s measurement. A single unit of CSS is applied to examine the size of the property, which the...

5 minutes read.