Curriculum
Course: CSS
Login

Curriculum

CSS

CSS INTRODUCTION

0/1

CSS Selectors

0/1

CSS Comments

0/1

CSS Padding

0/1

CSS Box Model

0/1

CSS Combinators

0/1

CSS Pseudo-classes

0/1

CSS Pseudo-elements

0/1

CSS Dropdowns

0/1

CSS Image Gallery

0/1

CSS Image Sprites

0/1

CSS Counters

0/1

CSS Website Layout

0/1

CSS Specificity

0/1

CSS Math Functions

0/1
Text lesson

What is !important?

In CSS, the !important rule is employed to assign higher importance to a property/value pair than usual.

Indeed, when you apply the !important rule, it takes precedence over all preceding styling rules for that particular property on the element.

Consider the following example:

Example 

#myid {
  background-color: blue;
}

.myclass {
  background-color: gray;
}

{
  background-color: red !important;
}

Example Explained 

In the provided example, despite the higher specificity of the ID selector and the class selector, all three paragraphs will have a red background color. This is because the !important rule takes precedence over the background-color property in both cases, overriding any previous styling.

Important About !important

The sole method to supersede an !important rule is by incorporating another !important rule with the same or greater specificity in the source code. However, this practice leads to confusion and makes CSS debugging challenging, particularly in extensive style sheets.

In the provided simple example, it’s unclear from the CSS source code which color is deemed the most crucial.

Example

#myid {
  background-color: blue !important;
}

.myclass {
  background-color: gray !important;
}

{
  background-color: red !important;
}

Advice: Familiarize yourself with the !important rule often found in CSS source code, but refrain from using it unless absolutely necessary.

Maybe One or Two Fair Uses of !important

One scenario where using !important is justified is when you need to override a style that is otherwise unchangeable, such as within a Content Management System (CMS) where direct CSS editing isn’t possible. In such cases, custom styles can be applied to override specific CMS styles.

Another instance where !important may be utilized is when aiming for a uniform appearance for all buttons on a webpage. For instance, if buttons are initially styled with a gray background color, white text, along with padding and borders, you might want to apply consistent custom styling across all buttons.

Example

.button {
  background-color: #8c8c8c;
  color: white;
  padding: 5px;
  border: 1px solid black;
}

Sometimes, the appearance of a button may undergo alterations when placed within another element with higher specificity, causing conflicts between their properties. Here’s an example to illustrate this situation.

Example

.button {
  background-color: #8c8c8c;
  color: white;
  padding: 5px;
  border: 1px solid black;
}

#myDiv a {
  color: red;
  background-color: yellow;
}

To ensure consistent styling for all buttons regardless of any conflicting specificity, we can apply the !important rule to the button properties, like so:

 

Example

.button {
  background-color: #8c8c8c !important;
  color: white !important;
  padding: 5px !important;
  border: 1px solid black !important;
}

#myDiv a {
  color: red;
  background-color: yellow;
}