CSS hack

This is an old revision of this page, as edited by 202.20.0.68 (talk) at 03:32, 16 January 2007 (Criticisms of Hacks). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A CSS filter is a coding technique that aims to effectively hide or show parts of the CSS to different browsers, either by exploiting CSS-handling quirks or bugs in the browser or by taking advantage of lack of support for parts of the CSS specifications. Using CSS filters, some designers have gone as far as delivering entirely different CSS to certain browsers in order to ensure that designs are rendered as expected. Because very early web browsers either were completely incapable of handling CSS or rendered CSS very poorly, designers today often routinely use CSS filters that completely prevent these ancient browsers from 'seeing' any of the CSS. For example, CSS filters are very often put in place to exclude Netscape 4.xx and Internet Explorer 4, ensuring that only more recent 'v5' browsers 'see' the CSS.

Conditional Comments

Conditional Comments are supported by Microsoft Internet Explorer (version 5 onwards[1]) they allow web developers to show or hide html code based on the version of the viewer's browser.

There are two types of conditional comment, downlevel revealed which is a proprietary Microsoft HTML tag and downlevel hidden which are ignored by non Microsoft browsers because they are treated as a standard comment.

Conditional comments can be used to hide certain styles from a specific browser, or even to supply a tailored style sheets for individual browser versions.

This is an example of downlevel hidden conditional comment:

<!--[if IE 7]>
<style type="text/css">
p {text-decoration:underline;  /* Underlines paragraph text in Internet Explorer 7, but no other browser  */}
</style>
<![endif]-->

This is an example of downlevel revealed conditional comment:

<![if !IE]>
<style type="text/css">
p {text-decoration:line-through;  /* line-through paragraph text in browsers other than Internet Explorer */}
</style>
<![endif]>



"Hacks"

"Hacks" are style definitions that exploit known peculiarities and bugs to control whether style rules are seen by a specific browser version. Care should be taken when using hacks, website authors should check that hacks still work as intended when new version of browsers are released.

Selector Hacks

Star HTML hack

The html element is the root element of the W3C standard DOM, but Internet explorer versions 5.5 and 6 include a mysterious parent element. Fully-compliant browsers will ignore the * html selector, while IE 5.5 and 6 will process it normally. This enables rules to be specified for these versions of Internet Explorer which will be ignored by all other browsers. For Example:

* html p {font-size: 5em;  /* Specifies text size in Internet Explorer 5.5 and 6, but not in any other browsers */ }
html>body hack

Versions of Internet Explorer before version 7 do not support the "child selector" (>), allowing rules to be specified for all browsers except Internet Explorer. For example:

html>body p {color: blue;  /* Turns paragraph text blue in Firefox, but not in IE    */}

Criticisms of Hacks

Hiding code using hacks often leads to pages being incorrectly displayed when browsers are updated. Many Hacks that used to hide CSS from Internet Explorer 6 and lower will not work in Internet Explorer 7 due to Internet Explorer 7 improved support for CSS standards. The Microsoft Internet Explorer development team have asked that people use conditional comments instead[2] of hacks.

  • CSS Filters - A fairly complete table of CSS "hacks" which show and hide rules from specific browsers