Let's take these three selectors, sorted from the highest specificity to the lowest:
.special-section p { }
.weird-font { }
p { }
Many CSS gurus recommend against nesting like in the first selector .special-section p
, because its specificity is high enough that you can't override it with a simple class like .weird-font
. I would like to find a way to still achieve nesting like in .special-section p
, but without increasing specificity. Something like this:
.weird-font { }
.special-section p /* with hack to decrease specificity */ { }
p { }
Use case:
It's pretty safe to apply defaults for typography and such document-wide using simple selectors like p
. However, I would like to change those defaults for a particular section, similar to .special-section p
, without having to use hacks to increase the specificity of selectors like .weird-font
. I would rather use a hack to decrease the specificity of .special-section p
than use a hack to increase the specificity of .weird-font
. Is there a way to do this?
!important
is that there is only one level of override. If you make a more specific selector, you can override it again with a still even more specific selector. After using!important
, you're out of options. – Overspend