The :first-child
pseudo-class will only target the very first child node. If that first child does not match the criteria of being an element[bla="3"]
, then nothing will be selected.
There is no equivalent filter pseudo-class specifically for attributes. One workaround is to select all elements and then exclude those that come after the first one (as explained in more detail here and here):
element[bla="3"] {
}
element[bla="3"] ~ element[bla="3"] {
/* Revert the rule above */
}
This method works well for applying styles, but if you need to identify that specific element for reasons other than styling (especially if your markup is arbitrary XML rather than HTML), you may have to resort to using functions like document.querySelector()
:
var firstChildWithAttr = document.querySelector('element[bla="3"]');
Alternatively, you can use an XPath expression:
//element[@bla='3'][1]