views:

444

answers:

2

Should this work?

<!--[if IE]>
<link rel="stylesheet" type="text/css" href="/minify/css?f=ie8.css<!--[if lte IE 7]>,ie7.css<![endif]--><!--[if lte IE 6]>,ie6.css<![endif]-->" />
<![endif]-->

Apparently nested comments don't work, so what about this?

<link rel="stylesheet" type="text/css" href="/minify/css?f=someotherfile.css<!--[if IE]>,ie8.css<![endif]--><!--[if lte IE 7]>,ie7.css<![endif]--><!--[if lte IE 6]>,ie6.css<![endif]-->" />
A: 

It will not work, because you're nesting conditional comments.

A non-IE browser will read it the way this sites syntax highlighting reads it.

To fix it, remove the closing -- from the nested comments to hide it from normal browsers.
It should then work fine.

For example:

<!--[if IE]>
<link rel="stylesheet" type="text/css" href="/minify/css?f=ie8.css<![if lte IE 7]>,ie7.css<![endif]><![if lte IE 6]>,ie6.css<![endif]>" />
<![endif]-->
SLaks
What about if the containing <!--[if IE]> part was removed, would it work with the conditional stuff being in an attribute like that?
Stephen Belanger
I don't know; try it.
SLaks
+2  A: 

No, conditional comments are not macro-style processing above HTML; they can only go where normal HTML comments can. Comments can't go inside tags.

Therefore:

<!--[if lt IE 7]><link rel="stylesheet" type="text/css" href="/minify/css?f=ie8.css,ie7.css,ie6.css"><![endif]-->
<!--[if (gte IE 7)&(lt IE 8)]><link rel="stylesheet" type="text/css" href="/minify/css?f=ie8.css,ie7.css"><![endif]-->
<!--[if (gte IE 8)&(lt IE 9)]><link rel="stylesheet" type="text/css" href="/minify/css?f=ie8.css"><![endif]-->

(Do you have enough IE-hack rules that a separate stylesheet is warranted, even for IE8? That browser is generally pretty well behaved, as long as it's not in a compatibility mode. If you only have a few rules, this tip might be of use.)

bobince
Sadly, It's not up to me how many stylesheets are needed--we are trying to allow user submitted stylesheets on a community site and there is many complaints of IE issues, so we need a catch-all solution like this. If it was up to me we wouldn't be using these at all. ;)
Stephen Belanger