When is the !important rule used in CSS?



The !important rule provides a way to make your CSS cascade. It also includes the rules that are to be applied always. A rule having a !important property will always be applied, no matter where that rule appears in the CSS document.

Example

For example, in the following style sheet, the paragraph text will be black, even though the first style property applied is red:

<style>
   <!--
      p {
         color: #ff0000;
      }
      p {
         color: #000000;
      }
   -->
</style>

Example

If you want to make sure that a property always applied, you would add the !important property to the tag. Therefore, to make the paragraph text always red, you should write it as follows :

Live Demo

<html>
   <head>
      <style>
         p {
            color: #ff0000 !important;
         }
         p {
            color: #000000;
         }
      </style>

   </head>
   <body>
      <p>Tutorialspoint.com</p>
   </body>
</html>
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements