- CSS3 provides “Nth Child” selector for element selection.
- In this Demo, “We will see the use of Nth selector in CSS3 and Jquery“.
- JavaScript/Jquery Array Index starts from ZERO(0), But CSS3 indexing starts from ONE(1). This cause the problem of selecting an element.For example, when you select 1st element in Jquery it will select the 2nd element while in CSS3 it will select the 1st element.
- The HTML markup used for this demo is as below,
<ol>
<li>sandeep</li>
<li>sandeep</li>
<li>sandeep</li>
<li>sandeep</li>
<li>sandeep</li>
<li>sandeep</li>
<li>sandeep</li>
<li>sandeep</li>
<li>sandeep</li>
<li>sandeep</li>
</ol>
$("ol li:eq(1)").css("backgroundColor", "#ff0");
ol li:nth-child(4) {
color:blue;
}
ol li:nth-last-child(2) {
color:orange;
}
ol li:nth-child(even) {
font-style:italic;
font-size:20px;
}
ol li:nth-child(odd) {
font-style:normal;
font-size:10px;
}
ol li:nth-child(3n+1) {
background:pink;
}