Specificity and the Cascade
When rules conflict, the browser picks a winner. Inline styles beat ids, ids beat classes (and attributes and pseudo-classes), and classes beat element selectors. If specificity is equal, the rule written later wins.
!important overrides normal specificity, but it makes code hard to maintain, so use it rarely. A better fix is usually to write a more specific selector or reorder your rules.
Example
<style>
p { color: gray; }
.note { color: royalblue; }
#special { color: crimson; }
</style>
<p>Element rule only</p>
<p class="note">Class beats element</p>
<p class="note" id="special">Id beats class</p>- Compare selectors by counting ids first, then classes, then elements.
Exercise
Which selector below would win? Fill in the winning kind: a class selector starts with ___ .
note { color: red; }Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.