CSS Tutorial

!important

What is '!important'?

'!important' is a CSS declaration that overrides all other styles applied to an element. It is used as a last resort when other methods of overriding styles have failed.

Steps to Use '!important'

  • Add the '!important' declaration to the end of the CSS property you want to override.
  • Example: color: red !important;

Key Concepts

  • Overriding Styles: '!important' overrides all other styles, including inline, internal, and external stylesheets.
  • Last Resort: Use '!important' only when other methods of overriding styles have failed. It should not be used for general styling.

Example

/* Original style: color is blue */
p {
  color: blue;
}

/* Override with !important: color is red  */
p.important {
  color: red !important;
}

Accessibility and Ease of Use

Avoid using '!important' for accessibility reasons: '!important' can unintentionally override styles that are essential for accessibility, such as high-contrast modes.

Favor !important for emergencies: Use '!important' only when absolutely necessary, such as to fix a critical bug or resolve a conflict between two conflicting stylesheets.