CSS Advanced Features

CSS !important Rule

Master the !important declaration - learn when to use it responsibly and understand its impact on CSS specificity and maintainability.

What does !important do in CSS?

!important makes a declaration win over normal declarations regardless of specificity or order. It's a last resort: it makes CSS hard to override later. It's reasonable for utility classes and for overriding third-party inline styles, but fix specificity problems at the source instead.

.hidden { display: none !important; }   /* utility: must always win */

Interactive !important Demo

Code Editor

<p id="msg" class="note">Which colour wins?</p>
<button class="btn hidden">Utility class with !important</button>

Preview

Understanding CSS Specificity

Specificity Hierarchy

/* Specificity order (lowest to highest): */

/* 1. Universal selector (*) - 0,0,0,0 */
* {
  margin: 0;
}

/* 2. Element selector - 0,0,0,1 */
p {
  color: black;
}

/* 3. Class selector - 0,0,1,0 */
.text-blue {
  color: blue;
}

/* 4. ID selector - 0,1,0,0 */
#main-text {
  color: red;
}

/* 5. Inline styles - 1,0,0,0 */
/* style="color: green;" */

/* 6. !important - overrides everything */
.important-text {
  color: purple !important;
}

CSS follows a specific hierarchy for determining which styles apply.

How !important Works

/* Without !important - ID wins */
#sidebar {
  background: blue;
}

.dark-theme {
  background: black;
}

/* With !important - class wins */
#sidebar {
  background: blue;
}

.dark-theme {
  background: black !important;
}

/* !important vs !important - order matters */
.theme-dark {
  background: gray !important;
}

.theme-override {
  background: purple !important; /* This wins */
}

!important overrides normal specificity rules.

Valid Use Cases for !important

Utility Classes

/* Utility classes that should always work */
.hidden {
  display: none !important;
}

.sr-only {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  clip: rect(0, 0, 0, 0) !important;
}

.text-center {
  text-align: center !important;
}

.float-right {
  float: right !important;
}

Utility classes need to override any existing styles

Framework Overrides

/* Override third-party CSS frameworks */
.bootstrap-override {
  margin-bottom: 0 !important;
}

/* Override WordPress theme styles */
.wp-override {
  font-family: 'Inter', sans-serif !important;
}

/* Override inline styles from JS libraries */
.slider-custom {
  height: 400px !important;
  width: 100% !important;
}

/* Override email client styles */
.email-text {
  color: #333 !important;
  font-size: 16px !important;
}

Sometimes necessary to override framework styles

Accessibility

/* Respect user motion preferences */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Force high contrast for accessibility */
@media (prefers-contrast: high) {
  .low-contrast {
    color: black !important;
    background: white !important;
  }
}

/* Ensure focus visibility */
.focus-visible {
  outline: 3px solid #0066cc !important;
}

User accessibility preferences should override design

Print Styles

/* Print-specific overrides */
@media print {
  .no-print {
    display: none !important;
  }

  .print-only {
    display: block !important;
  }

  * {
    background: white !important;
    color: black !important;
  }

  a:after {
    content: " (" attr(href) ")" !important;
  }
}

Print styles often need to override screen styles

When NOT to Use !important

Anti-Patterns to Avoid

/* ❌ DON'T: Use for regular styling */
.header {
  background: blue !important;
  padding: 20px !important;
  margin: 10px !important;
}

/* ❌ DON'T: Use to fix specificity issues */
.sidebar .widget .title {
  color: red !important;
}

/* ❌ DON'T: Use on every property */
.card {
  width: 300px !important;
  height: 200px !important;
  border: 1px solid black !important;
  border-radius: 8px !important;
}

/* ❌ DON'T: Use when better solutions exist */
.component .deep .nested .element {
  display: flex !important;
}

These patterns make CSS harder to maintain and debug.

Better Alternatives to !important

Increase Specificity

/* Instead of !important, be more specific */

/* ❌ Avoid */
.text {
  color: blue !important;
}

/* ✅ Better */
.page-content .sidebar .text {
  color: blue;
}

/* ✅ Even better with class combination */
.sidebar.dark-theme .text {
  color: blue;
}

/* ✅ Best - use specific classes */
.sidebar-text-highlighted {
  color: blue;
}

Write more specific selectors instead of using !important

Restructure CSS

/* Organize CSS in proper order */

/* 1. Base styles */
button {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 4px;
}

/* 2. Component styles */
.btn {
  font-weight: 500;
  cursor: pointer;
}

/* 3. Modifier styles */
.btn--primary {
  background: #3b82f6;
  color: white;
}

/* 4. State styles */
.btn--primary:hover {
  background: #2563eb;
}

Organize CSS hierarchy to avoid specificity conflicts

Debugging !important Issues

Developer Tools Tips

Finding !important Rules

  • • Open DevTools and inspect element
  • • Look for crossed-out styles
  • • !important rules show as bold
  • • Check Computed tab for final values
  • • Use Console to test overrides

Common Solutions

  • • Remove unnecessary !important
  • • Increase selector specificity
  • • Reorder CSS rules
  • • Use CSS variables for overrides
  • • Restructure component hierarchy

CSS !important — step by step

1 / 4
1

What !important Does

!important overrides all other specificity rules — a declaration with !important beats any selector regardless of how specific it is.

1 of 4
⚠️!important is a Sign of Specificity Debt
Frequent use of !important means your CSS specificity is out of control. Every !important you add makes the next one harder to override. Use CSS cascade layers (@layer) to manage specificity without !important.

🎯 Quick Check

If two rules both use !important, which one wins?

CSS !important: summary

Key points

  • Between two !important declarations, normal specificity and order decide the winner again.
  • !important beats inline styles unless the inline style is also !important.
  • Cascade layers (@layer) and :where() are cleaner ways to manage which styles win.

Common mistakes

  • Adding !important to fix one bug, then needing another !important to override it later.

Frequently asked questions

›Is using !important bad practice?

Overusing it is. It's fine for utility classes that must always apply and for overriding styles you can't edit. Elsewhere, lower the competing selector's specificity instead.

›How do I override an !important rule?

Write another !important declaration with equal or higher specificity that comes later, or put it in a later cascade layer.