×

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 Display

CSS Display: The CSS Display property is one of an essential property among all other properties of CSS. The display property specifies how the HTML elements should be displayed on...

4 minutes read.

CSS Loader

CSS Loader The loader is the animation that can alert a visitor regarding the loading of the page. We need to wait for some time. It will be helpful if any...

4 minutes read.

CSS Pseudo-class

Pseudo-class The pseudo-class may be depicted as the keyword which can be mixed to any selector that specifies the elected element’s unique state. It can be included to a selector to...

6 minutes read.

How to change font size in CSS

How to change font size in CSS? Font Size: - Users may attract attention to content on a website page in various ways. One element in a sentence can be highlighted. You...

5 minutes read.

CSS Hover

CSS Hover: CSS :hover is used to select the various elements whenever we point the mouse on these elements. We can also use this selector on all the HTML elements,...

3 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.

CSS Dropdowns

Dropdowns are one of the maximum crucial parts of an interactive internet site. CSS is a kind used to design the drop-down menus. A drop-down essentially is a group of...

5 minutes read.

Grid Vs Flexbox in CSS

Grid: - CSS Grid Layout is a 2D grid-based matrix-based layout method with columns and rows that simplifies web pages created by eliminating the need for floats and placement. A grid layout, like...

4 minutes read.

CSS White Space Property

White Space in CSS The white-space CSS property describes how to show the content inside an element. This property is applied to manage a white space within an element. Values of White-space...

2 minutes read.

CSS Margin

CSS Margin: The CSS margin property is employed to describe the space throughout the elements. It clears the range throughout the element. These properties do not have a background color...

2 minutes read.

CSS Pseudo-elements

Pseudo-elements The pseudo-classes in CSS can be represented as any keyword which is connected to the selector that represents any selected element’s unique state. On the other hand, pseudo-elements can be...

6 minutes read.

CSS Visibility

Visibility in CSS: This visibility property in CSS is employed to describe whether the components are visible or not. Note: An invisible component also grabs the space over the page. If...

2 minutes read.

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...

2 minutes read.

What is CSS

CSS stands for Cascading Style Sheet. It gives an additional style to the HTML document. A cascading style sheet is a language that is designed to define the document formatting...

3 minutes read.

CSS Text-Transform Property

Text-Transform in CSS The text-transform property permits us to alter the text case. It controls the capitalization of the text. This property is employed to design the text appearance in every...

3 minutes read.

CSS Overlay

CSS Overlay: It measures to cover-up something’s surface along with a coating. CSS overlay sets anything over the top area of other things. It creates a web-page more attractive. The...

8 minutes read.

CSS Flex-Box

Introduction The flex-box property of CSS stands for Flexible Box Layout Module. It is specifically used to design a flexible responsive layout. Flex-box is a one dimensional layout model, i.e. it works...

6 minutes read.

CSS Descendant Selector

Descendant Selector The Descendant selector in CSS is used to match the descendant components of a particular component. The term descendant depicts nested in whatever place, inside a DOM tree. This...

2 minutes read.

How to get rid/remove of bullet pioints in CSS

We include a list of items on the web pages that were sometimes numbered and sometimes bulleted in various styles. In HTML, there are two kinds of lists: ordered and unordered...

3 minutes read.

What is Bootstrap CSS?

Bootstrap: - Bootstrap is a CSS framework that is free to use and is used for the front-end development of the website in a faster and easier way. Bootstrap was released as...

3 minutes read.