Aug
04
2009

How to Use the Important Declaration in CSS

by   |  Posted in Tutorials  |  5 comments

Sometimes when you are messing around with CSS, you can’t figure out why certain styles won’t stick. Most of the time, it’s because you are obviously doing something wrong, but other times you might just be missing the fact that your CSS is being overwritten somewhere else down the line. Remember, CSS stands for Cascading Style Sheets, so styles you set further down will overwrite styles that are above.

You have probably come across the solution several times when looking over CSS and you just might not have know that it was there.

The !Important declaration

Placing the !Important declaration after a style is a way to force that style to be applied even if they may be contradicted further down in your CSS.

Examples:

h1 { color: #ff0000; }
h1 { color: #000000; }

In the above example, your H1 tags will be black.

h1 { color: #ff0000 <span style="color: #ff0000;">!Important</span>; }
h1 { color: #000000; }

By adding the !Important declaration, you force the first style to be applied even though the style below should overwrite it.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

Tags: , , , , , , , , ,

Short URL: http://bit.ly/div8bt

Discussion 5 Comments

  1. KT on August 6, 2009 at 9:27 am

    I have seen this in CSS but I never knew what it was for. Thanks for the explanation.

  2. Clair on August 6, 2009 at 10:08 pm

    Good to know. Thanks.

  3. Simanek on August 11, 2009 at 1:03 am

    You’ve correctly described this feature of CSS. However, I would recommend that it is a good practice to not be too reliant on it. Discovering a conflict in a CSS file is a good opportunity to ‘clean up’ your CSS by finding the conflict and improving the structure and specificity of your CSS rules.

  4. css examples on January 29, 2010 at 9:19 am

    Thanks for good articles.