Is it possible to target first li
having specific class inside ul
? for example :
<ul>
<li class="a"></li> <!-- I want to target this li -->
<li class="a"></li>
<li class="a"></li>
<li class="b"></li> <!-- and this li -->
<li class="b"></li>
<li class="b"></li>
</ul>
Any possibility?
The :first-of-type selector in CSS allows you to target the first occurence of an element within its container. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.
The CSS selector div:first-of-type only selects the very first element of its type and styles it. The div span:first-of-type selects the first span in each div since the div is the parent element.
The child combinator ( > ) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the direct children of elements matched by the first. Elements matched by the second selector must be the immediate children of the elements matched by the first selector.
Use the :first-child
pseudo-class and the adjacent sibling selector +
.
.a:first-child, /* Select the first child element */
.a + .b { /* Select the first element with 'b' class */
background-color: dodgerblue;
}
<ul>
<li class="a"></li>
<li class="a"></li>
<li class="a"></li>
<li class="b"></li>
<li class="b"></li>
<li class="b"></li>
</ul>
try selector :nth-child() and :first-child
ul li:first-child {
//some css
}
and
ul li:nth-child(4) {
//some css
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With