! Important attribute in CSS


important_example

So many time in interview if we will ask this question with developers. They don’t know “What is Important attribute in CSS” . Here is the answer:

If we have to avoid the override functionality of parent css class, then we can declare as ! important in css. But it is not a good practice to use it in all the scenario . It will be very difficult in debugging process. If it is possible, we have to avoid this features while using in application.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>
        div {
            background-color: green !important ;
        }
    </style>
</head>
<body>

  <fieldset>
      <legend>
          <b>This is the sample code of Important in CSS</b>
      </legend>
 <div style="background-color:silver; height:100px; width:500px;">
<p>This is the text in Div</p>
</div>
</fieldset>
</body>
</html>


Note:

In the above code we have defined all div color should be green. But if we will not used important then developer can override that color with other color using inline css. So if we have to avoid the override the css class we can use important in global css class.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.