!important rule overrides any other style declaration made.
Definition 2: !important rule specified style gets highest priority irrespective of default style applicable order.
Followings are default CSS styles applicable hierarchy.
Inline level style has highest priority by default and applicable to the element first.
Example: Apply styles to P elements based on default style applicable hierarchy ((!important rule is not used).
<!DOCTYPE HTML>
<html>
<head>
<title> CSS !important</title>
<style>
p {
background-color:green;
color:white;
}
div p {
background-color:yellow;
color:black;
}
</style>
</head>
<body>
<p>Page level style applied</p>
<p style="background-color:red;">Element level style applied</p>
<p>Page level style applied</p>
<div>
<p>Page level style applied </p>
</div>
</body>
</html>
Result:
In the above example,
Example to show how “!important” rule overrides default style hierarchy.
<!DOCTYPE HTML>
<html>
<head>
<title> CSS !important</title>
<style>
p {
background-color:green !important;
color:white;
}
div p {
background-color:yellow;
color:black;
}
</style>
</head>
<body>
<p>Page level style applied</p>
<p style="background-color:red;">Element level style applied</p>
<p>Page level style applied</p>
<div>
<p>Page level style applied</p>
</div>
</body>
</html>
Result:
In the above example,
Note:
Since “!important” rule disturbing default/natural flow, it is to be used with care.