AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
CSS Nth-Child |
The :nth-child pseudo-class is used to represent one or more of the sibling elements. The syntax for :nth-child is as follows:
where <expression> can be of the following forms:
Below we some examples. Assume there are 10 child elements.
Let's take a look at two examples below: Example 1: Use :nth-child to change the even paragraphs CSS Declaration
The following HTML code,
results in the following:
In Example 1, we used :nth-child to specify that the even number of paragraphs will be in italics. Example 2: Use :nth-child to change the first two items CSS Declaration
The following HTML code,
results in the following:
In Example 2, when n=0, -n+2=2; when n=1, -n+2=1. Therefore, the texts in the first two rows are bold. Example 3: Use :nth-child to change non-consecutive items CSS Declaration
The following HTML code,
results in the following:
When n=0, 2n+3=3; when n=1, 2n+3=5. Therefore, the third line and the fifth line are shown in red. Example 4: Use :nth-child to change background color for alternate rows CSS Declaration
The following HTML code,
results in the following:
In Example 4, we use the :nth-child pseudo class to give odd-number rows a background color of #ccc, while the background color for even-number rows remains white. |
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.