Languages
[Edit]
EN

CSS - !important rule

0 points
Created by:
user4838
668

In this article, we would like to show you how to !important rule in CSS.

Quick solution:

#id {
  background: green;
}

.class {
  background: red;
}

div {
  background: yellow !important;  /* overrides #id and .class background for all div elements */
}

 

Practical example

In this section, we present a practical example of !important rule that overrides all previous styling rules for the specific property (background) on that element (div).

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>
    #my-id {
      background: green;
    }

    .my-class {
      background: red;
    }

    div {
      margin: 5px;
      background: yellow !important;  /* overrides #id and .class background */
    }

  </style>
</head>
<body>
  <div id="my-id">div element with #my-id</div>
  <div class="my-class">div element with .my-class</div>
  <div>div element</div>
</body>
</html>

Note:

Don't use !important if it's not necessary because it makes the CSS code confusing and hard to debug.

References

Alternative titles

  1. CSS - important property (!important)
  2. CSS - !important exception
  3. CSS - how to apply !important rule
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join