HTML & CSSMedium
What is !important?
!important is a flag added to a declaration (color: red !important;) that makes it beat all normal declarations, including inline styles and more specific selectors. It is a blunt override that breaks the normal cascade, so it should be used rarely and deliberately.
How it works in the cascade
- Important declarations form their own, higher-priority group.
- Between two
!importantdeclarations, normal rules apply again: specificity, then source order. - Origins and layers are reversed for important rules: user
!importantbeats author!important(so accessibility user styles can win), and!importantin an earlier@layerbeats!importantin a later one. - An inline
style="... !important"beats stylesheet!importantrules.
Legitimate uses
- Utility classes that must always apply:
.hidden { display: none !important; },.sr-only. - Overriding third-party CSS or inline styles you cannot edit.
- User stylesheets and accessibility overrides.
@media (prefers-reduced-motion: reduce) { * { animation: none !important; } }.
Why it is usually a smell
Each !important invites another one to override it, an escalating "specificity war" that makes CSS hard to maintain and debug.
Better alternatives
- Fix the selector's specificity or the source order.
- Use
:where()for low-specificity defaults. - Use
@layerto order resets, libraries, components and utilities predictably.
Interview tip
Knowing that !important reverses layer order is an advanced detail that impresses interviewers.
Code example
html
1<style>2 body { font-family: system-ui; }3 #promo .title { color: blue; } /* very specific */4 .title { color: green !important; } /* still wins: important beats specificity */56 /* Utility that must always win */7 .hidden { display: none !important; }89 /* Respect users who prefer less motion */10 .spin { display: inline-block; animation: spin 1s linear infinite; }11 @keyframes spin { to { transform: rotate(360deg); } }12 @media (prefers-reduced-motion: reduce) {13 * { animation: none !important; transition: none !important; }14 }15</style>16<section id="promo"><h2 class="title">I am green</h2></section>17<!-- Even an inline style loses to !important in a stylesheet -->18<p class="title" style="color: red">Also green, despite the inline style</p>19<p class="hidden" style="display: block">You will never see me</p>20<span class="spin">⟳</span> (stops spinning with reduced motion)
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.