The !important rule in CSS is used to give a CSS declaration higher priority than normal declarations. When a property is marked with !important, it takes precedence over other declarations of the same property, regardless of the normal CSS cascade.
In this chapter, you will learn what the !important rule is, its syntax, how it works, and how to use it with examples.
The !important rule is a special keyword that increases the priority of a CSS declaration. It is added after the value of a CSS property.
Normally, when multiple CSS rules target the same element, the browser determines which rule to apply based on the CSS cascade, selector specificity, and source order. However, if a declaration is marked with !important, it overrides normal declarations for the same property.
Although !important is useful in certain situations, it should be used only when necessary. Excessive use of !important makes CSS difficult to understand, maintain, and debug.
The following syntax is used to apply the !important rule.
selector {
property: value !important;
}
In this syntax:
When two or more CSS rules define different values for the same property, the browser normally follows the CSS cascade to determine which value to apply.
If one of those declarations is marked with !important, that declaration is given higher priority than normal declarations.
If multiple declarations use !important, the browser again follows the normal cascade rules such as specificity and source order to determine the final value.
The following example demonstrates how the !important rule overrides normal CSS declarations.
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: white;
}
h1 {
color: blue !important;
}
body {
background-color: lightblue !important;
text-align: center;
background-color: yellow;
}
</style>
</head>
<body>
<h1>Hello World.</h1>
<h1>Welcome to the TpointTech.com. This is an example of <i>!important</i> property.</h1>
<p></p>
</body>
</html>
Explanation
In this example:
The !important rule can be applied to almost any CSS property, such as color, background-color, border, margin, padding, and font-size.
p {
color: red !important;
font-size: 18px !important;
}
Explanation
In this example, both the color and font-size declarations have higher priority than normal declarations for the same properties.
The !important rule should be used only when a declaration must override other CSS rules.
Some common situations include:
In most cases, using more specific selectors is preferred over relying on !important.
The following example demonstrates the use of the !important rule with multiple CSS properties.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body{
text-align: center;
}
h1{
border-color: red !important;
border: 5px green solid;
border-color: black;
}
h2{
color: green !important;
color: red;
border-color: violet !important;
border: 5px green solid;
}
</style>
</head>
<body>
<h1>Hello World :) :)</h1>
<h2>Welcome to the javaTpoint.com</h2>
</body>
</html>
Explanation
In this example:
We request you to subscribe our newsletter for upcoming updates.