Valid downlevel-revealed conditional comments

There's been a bit of talk about the use of CSS hacks vs. Conditional Comments for supplying Internet Explorer for Windows with alternative CSS rules to compensate for its buggy behaviour. I personally prefer conditional comments since they allow me to send the buggy CSS to IE/Win only, with no risk of anything affecting good browsers.

So far, so good. But if you want to use conditional comments to hide something from Internet Explorer while keeping it visible to other browsers you'll notice that your HTML is no longer valid if you use Microsoft's Downlevel-revealed Conditional Comments (which really should be renamed to Uplevel-revealed).

The syntax suggested by Microsoft is this:

<![if !IE]>
do something

Unfortunately that is not valid HTML. And who is surprised to see Microsoft suggest the use of invalid HTML? Not me.

After a bit of searching I found an alternative and valid way of writing the conditional comments in a message from Lachlan Hunt to the whatwg mailing list: [whatwg] [WF2] Fixing Repetition Template Degradation in IE without Scripting.

The valid syntax for conditional comments that hide something from Internet Explorer for Windows is this:

<!--[if !IE]>-->
do something

Keep it handy for the next time you want to do something that is too advanced for Internet Explorer to handle.

Posted on November 14, 2005 in (X)HTML, Browsers, Web Standards