How to apply !important in CSS? - GeeksforGeeks

10 May, 2022
The !important property in CSS is used to provide more weight (importance) than normal property. In CSS, the !important means that “this is important”, ignore all the subsequent rules, and apply !important rule and the !important keyword must be placed at the end of the line, immediately before the semicolon.
In other words, it adds importance to all the sub-properties that the shorthand property represents.
In normal use, a rule defined in an external style sheet which is overruled by a style defined in the head of the document, which in turn, is overruled by an inline style within the element itself (assuming equal specificity of the selectors).
Defining a rule with the !important attribute that discards the normal concerns as regards the later rule overriding the earlier ones.
So, it is used for overriding the styles that are previously declared in other style sources, in order to achieve a certain design.
Syntax:
element {
    color: blue  !important;
    font-size: 14px !important; 
    ...
}
Example 1: 
HTML
<!DOCTYPE html><html>    <head>        <title>Document</title>        <style>            h1 {                color: blue ;            }            h1 {                color:white !important;            }            body {                background-color:green !important;                text-align:center;                background-color:yellow;            }        </style>    </head>    <body>        <h1>GeeksforGeeks</h1>        <h2>!important property</h2>        <p></p>    </body></html>                   
Output:
  
In the above example, the background color of the body is green instead of yellow because “!important” is kept after the green background color inside the body tag. 
Example 2: 
HTML
<!DOCTYPE html><html>    <head>        <title>!important property</title>        <style>            .geeks {                color: green !important;                size: 10ex !important;                background-color: lightgray !important;            }            .geeks {                color: red;                size: 100ex;                text-align:justify;                background-color: purple;            }            h1, h2 {                text-align:center;            }            h1 {                color:green;            }            body {                width:65%;                margin-left:15%;            }                         #gfg {                color: lightgreen !important;                size: 10ex !important;                text-align:justify !important;                background-color: darkgreen !important;            }            #gfg {                color: orange;                size: 1000ex;                background-color: magenta;            }        </style>    </head>    <body>        <h1>GeeksforGeeks</h1>        <h2>!important property</h2>        <div class = geeks> A Computer Science portal for geeks.        It contains well written, well thought and well explained        computer science and programming articles and quizzes.        </div>        <div id = gfg>            <p>Computer programming is the process of writing            instructions that get executed by computers. The            instructions, also known as code, are written in            a programming language which the computer can            understand and use to perform a task or solve a            problem.</p>        </div>    </body></html>                   
Output:
 
CSS is the foundation of webpages, is used for webpage development by styling websites and web apps. You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
hardikkoriintern
CSS-Misc
Picked
CSS
HTML
Web Technologies
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
How to update Node.js and NPM to next version ?
How to create footer to stay at the bottom of a Web page?
How to apply style to parent if it has child with CSS?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
How to update Node.js and NPM to next version ?
How to set the default value for an HTML <select> element ?
Hide or show elements in HTML using display property