There are several rules ( applied in this order ) :
!important
always takes precedence.In your case its rule 3 that applies.
Specificity for single selectors from highest to lowest:
#main
selects <div id="main">
).myclass
), attribute selectors (ex.: [href=^https:]
) and pseudo-classes (ex.: :hover
)div
) and pseudo-elements (ex.: ::before
)To compare the specificity of two combined selectors, compare the number of occurences of single selectors of each of the specificity groups above.
Example: compare #nav ul li a:hover
to #nav ul li.active a::after
#nav
):hover
and .active
)ul li a
) for the first and 4 for the second (ul li a ::after
), thus the second combined selector is more specific.