We’ve been a bit light on web development tips on the blog of late, but I had some thoughts on the !important CSS declaration that I thought I’d share.
The declaration’s been valid since CSS1 but it seems to have acquired a bad reputation over the years. But, it can be a useful and powerful command if used properly. The declaration is a keyword that can be added at the end of any CSS pair of property/value. For example:
p {margin-left: 5px !important}
It assigns a weight to each rule depending on the specificity of its selector and its position in the source. This determines which style is applied to an HTML element. When the !important declaration is used on a pair of property/value, that value becomes the most important for that property and overrides any others.
The !important declaration can be used for other things such as overriding inline styles and print stylesheets. It has its downsides but, if used carefully, it can save time and effort.
If this is of interest then look out for a much more in-depth article on this command, with examples, which will be published on our website in January 2010.
indicates required fields