Skip to main content
Frontend is a free bonus on CodeMentor AI — the main product is Python. Register to save your progress and unlock all 290 Frontend lessons + 1,000+ Python lessons.Sign up — free
🎨 CSS as a design system·Module B1 · Lesson 4
TaskThe .alert text should be red. There's a more-specific rule trying to make it blue. Use !important on .alert to force red. (The blue rule stays — we're demonstrating !important wins.)

!important: when it's a smell, when it's right

75 XP6 minFREE
Theory

What !important does

Tacks an !important flag on the declaration. !important declarations cross-cut all specificity rules — they outrank everything except other !important declarations (in which case specificity decides between them).

.alert { color: red !important; }

After this, NO normal selector can override the color. Not #main .alert, not inline style="color: blue". Only another !important rule of equal or higher specificity.

When !important is a smell

90% of !important usage is a sign the codebase has lost the specificity fight. Junior dev can't override something → adds !important → later someone needs to override THAT → adds !important higher up → arms race.

The fix is almost always: increase your selector's specificity by one class level, or rewrite the conflicting rule to be less specific.

When !important is actually correct

Three legitimate uses:

  1. Utility classes (Tailwind-style). A class like .text-center { text-align: center !important; } is meant to ALWAYS win. The rule "if you applied this utility, you wanted it" is honest.
  2. User stylesheets / accessibility overrides. A user setting * { font-size: 18px !important } to enforce a minimum font size — beats anything the site author tried.
  3. Print stylesheets. @media print { * { background: none !important } } to ensure printers don't waste ink on dark themes.

Outside these three, treat !important as a code smell that needs investigation.

How to UN-important

You can't lower a declaration's importance. The only options:

  1. Remove the !important from the source.
  2. Outrank it with another !important of higher specificity.

The second is the arms race we're trying to avoid. The first is the right answer.

2 tabs
Live preview
PreviousNext lesson →

Get one Python or web tip a day — by email

Short, hand-written, no spam. Unsubscribe in one click.