Loading Please Wait...

Logo Lynxsia IT Solutions

CSS !important Rule

CSS !important Rule

The CSS !important rule in CSS is used to add more importance to a property/value than normal.

If we use the !important rule, it will override all previous styling rules for that specific property on that element!

					 
        
          #sectionID {
          background-color: blue;
          }

          .sectionClass {
            background-color: gray;
          }

          p {
            background-color: red !important;
          }
        
      
Explanation

In the above example ID selector has the highest specificity among all the selector. But !important rule overrides all the definition and apply style (background-color: red;) to all selections.

Issue With !important

The only way to override an !important rule is to include another !important rule on a declaration with the same (or higher) specificity in the source code - and here the problem starts! This makes the CSS code confusing and the debugging will be hard, especially if you have a large style sheet!

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

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

          p {
            background-color: red !important;
          }
        
      
!important With Shorthand Properties

Declaring a shorthand property with !important sets all of sub-properties as important.

					 
        
          p{
            margin: 10px !important;
          }

          /* It will mark the individual properties as !important too. */
          #p{
            margin-top: 10px !important;
            margin-right: 10px !important;
            margin-bottom: 10px !important;
            margin-left: 10px !important;
          }
        
      
Avoid using !important to override specificity unless you absolutely have to. One way to use !important is if you have to override a style that cannot be overridden in any other way.

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved