CSS Second ChildWe will comprehend the CSS second child in this article. Let us understand what the second child is. Second ChildThe second child in CSS is the child related to the second number. Let us comprehend it with the assistance of a demonstration. DemonstrationThe 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. We can pick the second child of the parent with the help of :nth child() selector. :nth Child() SelectorIt 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() SelectorWe 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. 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. 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. 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. Browser SupportFollowing are the browsers that support the :nth-child() selector:
ConclusionWe have understood the CSS second child in this article. Some points to remember are given below:
Next TopicCss flex align content |