What is the reason for this?
It's simply a matter of specificity - the first selector has a type selector attached to the class name whereas the second selector only has a single class. The second selector is therefore more specific and takes precedence.
This is migrated from another answer of mine, it may help:
You can think of specificity as four numbers, starting with (0,0,0,0):
- !important rules always take precedence, only another !important rule
can override a previous one (its an accessibility feature of CSS,
designed to override the UA stylesheet)
- The universal selector (*) has a specificity of 0
- Combinators like
+
and ~
also have no specificity
- Inline styles have the highest specificity (other than !important)
and count as the first number (1,0,0,0)
- ID's (
#test
) count as the second number in the above set (0,1,0,0)
- Classes, pseudo-classes and attribute selectors are the third number
(0,0,1,0)
- Type selectors and psuedo-elements (e.g. -
<p>
& ::after
) take place of the
fourth number, and are the least specific
- Remember that if two rules have the same specificity and specify the
same property the latter in the stylesheet will win
Based on the above, the first selector has a specifictiy of (0,0,1,1) while the second only has (0,0,1,0)