When two rules set the same property on one element, the browser picks the winner by specificity.
Specificity order (low to high)
- Element selectors (
p,h1) - Class, attribute and pseudo-class selectors (
.btn,[type],:hover) - ID selectors (
#main) - Inline styles (
style="...")
If specificity is equal, the rule written later wins.
!important
color: red !important; beats normal rules. Avoid it — it makes CSS hard to change later.
Inheritance
Some properties (like color and font-family) pass from parent to child automatically. Others (like border and margin) do not.
Example
Example — Which colour wins?
HTML
<style>
p { color: gray; }
.text { color: blue; }
#special { color: green; }
</style>
<p>Gray (element)</p>
<p class="text">Blue (class beats element)</p>
<p class="text" id="special">Green (ID beats class)</p>