The Importance of CSS !important

Published by Subhas Fagu on 26/04/09 14:22:25
Last edited on 26/04/09 14:23:23

This article is to bring light to the !important rule within CSS. CSS by default uses the last specified rule to style a document. However with the use of the !important rule, you can quickly override all subsequent modifications to the same property.

p{
color:grey;
}
a{
color:black;
}
p{
color:pink;
}

Using the above style in your stylesheet will render any paragraph the colour pink! How about the code below?

p{
color:grey !important;
}
a{
color:black;
}
p{
color:pink;
}

Now any paragraph will be rendered the colour grey.

I'm sure if this is W3 complaint but you can also override previous !important rules as follows.

p{
color:grey !important;
}
a{
color:black;
}
p.aclass{
color:pink !important;
}

This would allow the aclass to over rule the Inherited attribute.

About the Author

Subhas Fagu is an energetic person who you will never catch doing the same thing twice…unless it’s working. He’s always engaged in new concepts, frameworks, operating systems, applications, you name it and you’ve got a conversation. Being a founder of Techlicity Ventures, he's been a key in the business and technical development of all previous and upcoming projects. Subhas contributes a wide variety of perspectives to all projects to ensure risk coverage and smooth deployment.

Bookmark and Share
Blog Widget by LinkWithin
blog comments powered by Disqus

Valid XHTML 1.0!