Despite the fact that it’s widely used, applying CSS3’s !important declaration to your stylesheets isn’t considered to be a best practice. The !important declaration is typically used to override style rules that are defined elsewhere in the CSS. For example, if you want your links to be red, but there’s already a style rule within your CSS that indicates that all a tags should be yellow, you can add an !important declaration to your stylesheets and it will ensure that all a tags will in fact be red, EVEN IF the code defining the red color for the a tags comes BEFORE the code that defines them as yellow. See the example below:
- color: red !important;
- color: yellow;
Without the !important declaration, the rules of CSS indicate that the a tags would be yellow, because in CSS any rule that conflicts with a rule defined above it automatically overrides its predecessor. While the use of this declaration is definitely helpful when you’re in a pinch, it’s not considered a best practice because it makes for sloppy, unorganized code. If your entire stylesheet is littered with !important declarations, you could probably be doing better.
The best times to use the !important declaration is when you just can’t figure out what’s wrong with your CSS and why something isn’t working. If you’re in a time crunch, it’s okay to leave the !important declaration, but it’s always a good idea to find the root of the issue and go back and refactor your code later.