Maybe this question is ridiculous, but I want to know this fact. I have a li
element with two classes (see below), but it is not working as I expected. Does anybody know the reason?
.red:first-child {
color:#F00;
}
.blue:first-child {
color:#00F; /* not working */
}
.red:last-child {
color:#F00; /* not working */
}
.blue:last-child {
color:#00F;
}
<ul>
<li class="red">one</li> <!-- This is the first child of red -->
<li class="red">two</li>
<li class="red">three</li>
<li class="blue">one</li> <!-- and this first child of blue -->
<li class="blue">two</li>
<li class="blue">three</li>
</ul>
<li class="blue">one</li>
is not thefirst-child
of parentul
:first-child
or:last-child
selector. They will select any element only when an element is first or last child element of its parent respectively..blue:first-child
is not working because.blue
is not first child of parent. This is the thinking behind implementation of this selector and as for me its quite logical.