In this article I’ll introduce the declaration !important in CSSs and explain how to use it as a “workaround” to avoid adopting IE proprietary code to obtain the max-width effect in your web pages.
When a CSS propriety is specified twice, the browser will commonly use the last one. Let’s see an example:
In this example the browser will assign width 800 pixels to the #main element.
The declaration !important can be used in cascading style sheets to give priority to some parameters.
In the example above the browser will give priority to the first declaration and the #main element will be 600 pixels width.
So, what’s the point?
Internet Explorer 6 and previous versions simply ignore the declaration !important (while IE 7 supports them) therefore we can take advantage of this issue to design a “browser-based” CSS.
Let’s consider, for example, the CSS proprieties max-width and min-width: these parameters specify a maximum and minimum width of a web element (such as a table or a DIV container). This is a useful feature which allows to create fluid and dynamic layouts that can be displayed correctly both in low resolutions (e.g. 640×480) and in very high resolutions.
Unfortunately IE 6, among other things, doesn’t even support the max/min width declaration: in order to obtain the same effect under IE6 you can use a proprietary code:
width:expression(document.body.clientWidth > 900? "900px": "auto" ); /* IE proprietary code */
In this example the #main element will be rendered with a maximum width of 900px both in Firefox, Safari, Opera and Internet Explorer.
Please note that the proprietary code width:expression is not a valid CSS declaration, so if you decide to adopt it, your stylesheet won’t respect web standards and won’t be validated.
However, we can use the !important declaration to avoid non-standard CSS expressions. Here is a “workaround” which allows to use max/min width with smart browsers and specify a fixed width for IE 6 users:
margin: 0 auto 0;
In the above example (you can see it in action on this website) the declaration !important is used to specify dynamic width attributes to browsers that support them (e.g. Firefox, Netscape, Opera, Safari and IE 7) while the width is fixed to an average value (800 pixels) for IE 6.
This doesn’t solve completely the matter, but can be considered as a workaround to improve your fluid layout appearance avoiding non-standard code.