Javatpoint Logo
Javatpoint Logo

CSS Second Child

We will comprehend the CSS second child in this article. Let us understand what the second child is.

Second Child

The second child in CSS is the child related to the second number. Let us comprehend it with the assistance of a demonstration.

Demonstration

The code given below contains a "div" element that is a parent. The "div" element consists of many children. The "p" element written directly after the parent is considered the first child, and the next "p" element is considered the second child.

Code:

Output:

Here is the output in which we can clearly witness the second child.

CSS Second Child

We can pick the second child of the parent with the help of :nth child() selector.

:nth Child() Selector

It is a pseudo-class selector used to match the nth-child element. It allows us to select any child of the parent and give any style to the element.

Syntax:

The name of the child element should be written at the start of the syntax. "n" is the number, odd keyword, even keyword, or formula.

Demonstrations of :nth-child() Selector

We will comprehend the :nth-child() with the help of demonstrations.

Demonstration 1:

In this demonstration, we will choose all the children one by one and apply style to them. We will create a list of 10 electronic appliances and select each item using nth-child() to give it different colors.

Code:

Output:

Here is the result in which we can witness each item with different colors applied to them.

CSS Second Child

Demonstration 2:

In this demonstration, we are going to choose odd and even numbers of children and give them style utilizing nth-child().

Code:

Output:

Here in the output we can witness that the odd number child is in light green and the even number child is in light blue.

CSS Second Child

Demonstration 3:

We are going to select every third child using the formula "3n + 1" and give style to them in this demonstration utilizing nth-child().

Code:

Output:

Here is the output in which we can witness that every 3rd, 4th, 5th, and 6th child is in red with a yellow background.

CSS Second Child

Demonstration to Apply Style on Second Child Using the nth-child()

We will use nth-child() to select the second child and apply style to it.

Code:

Output:

Here, in the output, we can witness that the second child is given the style.

CSS Second Child

Browser Support

Following are the browsers that support the :nth-child() selector:

  • Google Chrome
  • Safari
  • Microsoft Edge
  • Firefox
  • Opera

Conclusion

We have understood the CSS second child in this article. Some points to remember are given below:

  1. The child that comes in connection with the second number is called the second child.
  2. The nth-child () selector is used to select the child.
  3. We can select the child in three ways:
    • Using number, i.e., nth-child(2)
    • Using even and odd keywords, i.e., nth-child(even)
    • Using the formula, i.e., nth-child(2n)

Next TopicCSS Spacing





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA