Initially, what is the distinction?
According to MDN:
:first-child()
The :first-child
CSS pseudo-class represents any element that is the first child element of its parent.
:first-of-type()
The :first-of-type CSS pseudo-class represents the first sibling of its type in the list of children of its parent element.
In summary, :first-child()
is a more lenient pseudo selector compared to :first-of-type()
Regrettably, :first-child
or :first-of-type
do not pay attention to classes or ids, they only focus on the DOM elements. So if you attempt something like this, it will not work
#kasticketproducts div.aProduct:first-of-type {
color: red;
}
In this situation, the best CSS solution would be using :nth-of-type()
with a value of 2
. However, it will fail if your element lacks a class
of aProduct
#kasticketproducts div:nth-of-type(2) {
color: red;
}
Demo
OR
You can utilize adjacent selectors with :first-of-type()
#kasticketproducts div:first-of-type + div {
color: red;
}
Demo
The second solution is MORE COMPATIBLE especially when considering IE compatibility