Things You Might Not Know About Conditional Comments

Things You Might Not Know About Conditional CommentsUse of conditional comments to target certain versions of Internet Explorer is pretty commonplace nowadays, and is generally seen as the best-practice method for including separate styles for IE.

Of course, I’ve argued in a previous article that if your IE-only styles are minimal, then you should just include them in your main stylesheet, a notion that others have echoed.

But conditional comments have some unique possibilities and quirks that maybe you haven’t considered before, or have simply forgotten. Here is an overview of some things you may not know about conditional comments.

You Can Target Every Browser Except IE

I think this is the most useful item on this list. While it would be nice to be able to target individual non-IE browsers by version, that simply isn’t possible through conditional comments. But it is possible to tell your document to display styles that will apply to every browser except Internet Explorer (all versions). Here is the syntax:

<!--[if !IE]>-->
<link href="non-ie.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="non-ie.js"></script>

In the example syntax, I’ve included a stylesheet and a script file. I think in some cases it would be quite feasible to exclude a script file from Internet Explorer because of IE’s poor JavaScript performance. I’m actually doing that on this website. I have a script that will change the layout of the website based on resolution, and I found that IE was becoming very slow and would throw JavaScript errors for no apparent reason — so I exclude that file from running when my site is viewed with IE.

It should be noted that the Microsoft-recommended version of the above code will not validate. This is actually an altered version of what Microsoft originally recommended.

You Can Target Non-IE Plus Some Versions of IE

This is another possibility that could be practical, and is a variation on the above example of targeting everything but IE. Here’s the syntax:

<!--[if IE 7]><!-->
<link href="alternate.css" rel="stylesheet" type="text/css">

It’s hard to notice at first, because the above comment at first glance looks very similar to a standard conditional. But the above stylesheet will only display to IE7 along with every other non-IE browser. You’ll notice that the opening and closing if statement sections are enclosed in comments (which hides them from the other browsers) but they also still qualify as a conditional visible to IE, so the conditional will still apply to the targeted version(s) of IE.

You can also accomplish something similar by nesting conditionals, like this:

<!--[if IE]><![if IE 7]><![endif]-->
<link href="alternate.css" rel="stylesheet" type="text/css">
<!--[if IE]><![endif]><![endif]-->

You Can Use Conditionals to Customize Markup

This is something that I have to admit, I was not familiar with until quite recently (probably because I rarely use IE-only CSS). It was pointed out in the comments in a previous article but evidently was first proposed by Paul Irish.

Here’s the basic syntax:

<!--[if lt IE 7 ]> <body class="ie6"> <![endif]-->
<!--[if IE 7 ]> <body class="ie7"> <![endif]-->
<!--[if IE 8 ]> <body class="ie8"> <![endif]-->
<!--[if !IE]><!--> <body> <!--<![endif]-->

So, in this example, the <body> tag gets a different class name depending on the version of IE being used. Non-IE browsers get a naked <body> tag (the last line). Targeting IE in your stylesheet then becomes just a matter of adding .ie6 in front of your selectors.

This option works as an alternative to conditional comments, and allows you to keep all your styles in the same file in order to save HTTP requests (which would grow in number if you use extra stylesheets targeted by conditionals). I think this is great advice in light of IE’s performance issues. IE is slow enough as it is; if you can do anything to speed it up (or more accurately, prevent it from slowing down), then go for it.

You Can Target IE Versions by Decimal Points

I really don’t see the use in doing this, but technically speaking it is possible, and good to know should something like this be necessary. This targeting is referred to as targeting by “version vectors”. But apparently, you have to know the exact version vector, otherwise the conditional will fail.

Here’s an example that correctly targets IE5.0 (not that you would ever have to do this!):

<!--[if IE 5.0000]>
<link href="ie5.css" rel="stylesheet" type="text/css">

It should be noted that the version vector can change according to the Windows machine being used, so the above syntax would not necessarily target all versions of IE 5.0, but only IE5.0 on certain machines. More info on this can be found here on MSDN. And there’s a very detailed explanation at the bottom of this article describing how to target specific versions of IE by version vectors.

You Can Use Other Operators in Your Expressions

Normally, the expressions we use to target IE include the use of the NOT operator (!), or one of the greater than/less than operators (gt, gte, lt, lte), but you can also use the subexpression operator (), the OR operator (|), as well as the AND operator (&).

For example:

<!--[if (IE 6)|(IE 7)]>
<link href="ie6-7.css" rel="stylesheet" type="text/css">

The expression above uses parentheses combined with the OR operator to target IE6 and IE7, and nothing else. Since IE5 is out of general use, then this method has become somewhat obsolete, since you can accomplish the same thing by using “lte IE7” instead.

And here’s how you would use the AND operator (this example targets IE5 and IE6):

<!--[if (gt IE 4)&(lt IE 7)]>
<link href="ie5-6.css" rel="stylesheet" type="text/css">

What Else?

I’m not sure if any of the above suggestions will be used too often, other than maybe the ability to target non-IE browsers. Conditionals have certainly saved developers many headaches, and like CSS hacks, continue to be used to create a consistent cross-browser experience.

Are there any other quirks and facts about conditionals that I haven’t mentioned here?

45 Responses

  1. Helen says:

    No, does not work.

    • Helen says:

      !IE does not work.

      • keith says:

        Actually, it does. Maybe you could add some clarifying information like “I tested it in browser X on operating system Y and it didn’t work.”

      • It does work, you just have to make sure you’re using the right syntax. I can see many people doing the following:

        <!--[if !IE]>
        <link href="non-ie.css" rel="stylesheet" type="text/css">

        But that’s not correct. You have to use this syntax:

        <!--[if !IE]>-->
        <link href="non-ie.css" rel="stylesheet" type="text/css">

        Notice the fact that the opening and closing expressions are wrapped in comments (in the second example which hides the expressions from the non-ie browsers, but allows the link tag to be viewed by them), instead of just a single comment wrapping the whole thing, which hides it from all, except for IE, which evaluates the expression.

  2. I did not knew about subexpression operators, Thanks for shearing : )

  3. Khandla says:

    Great article, Louis. You have 10 / 10 points.

  4. Mary says:

    Huh. I never thought about doing the body classes that way. Sweet! A new toy to play with!

  5. Jayman Pandya says:

    Amazing Article. This goes in my bookmark for future reference. :D

  6. Pusparaj says:

    Thanks a lot for sharing.

  7. s.y. Lin says:

    Awesome article! Really helpful for dealing with ie browsers. Thx for sharing!

  8. Ted Goas says:

    If you’re already ID’ing the BODY tag (say body id=’about’), you can ID the HTML tag using conditional comments.

    <!DOCTYPE html>
    <!--[if lt IE 7 ]> <html id="ie6"> <![endif]--> 
    <!--[if IE 7 ]>    <html id="ie7"> <![endif]--> 
    <!--[if IE 8 ]>    <html id="ie8"> <![endif]--> 
    <!--[if IE 9 ]>    <html id="ie9"> <![endif]--> 
    <!--[if gt IE 9]>  <html> <![endif]-->
    <!--[if !IE]><!--> <html> <!--<![endif]-->
    • Joel says:

      I’m trying to use the conditional technique to address different css coding by adding a class TO the BODY or HTML as in the example. BUT it seems that you can ONLY use conditional statements inside the BODY tag. When I run this code, all that gets output are the last items in the conditionals.

      <!--[if lt IE 7 ]>  <![endif]-->
      <!--[if IE 7 ]>     <![endif]-->
      <!--[if IE 8 ]>     <![endif]-->
      <!--[if IE 9 ]>     <![endif]-->
      <!--[if gt IE 9]>   <![endif]-->
      <!--[if !IE]><!-->  <!--
      Untitled Document
      <!--[if lt IE 7 ]>  <![endif]-->
      <!--[if IE 7 ]>     <![endif]-->
      <!--[if IE 8 ]>     <![endif]-->
      <!--[if IE 9 ]>     <![endif]-->
      <!--[if gt IE 9]>   <![endif]-->
      <!--[if !IE]><!-->  <!--
      <!--[if lt IE 7 ]> I am ie6 <![endif]-->
      <!--[if IE 7 ]>    I am ie7 <![endif]-->
      <!--[if IE 8 ]>    I am ie8 <![endif]-->
      <!--[if IE 9 ]>    I am ie9 <![endif]-->
      <!--[if gt IE 9]>  I am more than ie 9 <![endif]--> <!--[if !IE]><!--> I am Firefox baby! Winning!!!<!--

      This is the View Source HTML output from IE8:

      <!--[if lt IE 7 ]>  <![endif]-->
      <!--[if IE 7 ]>     <![endif]-->
      <!--[if IE 8 ]>     <![endif]-->
      <!--[if IE 9 ]>     <![endif]-->
      <!--[if gt IE 9]>   <![endif]-->
      <!--[if !IE]><!-->  <!--
      Untitled Document
      <!--[if lt IE 7 ]>  <![endif]-->
      <!--[if IE 7 ]>     <![endif]-->
      <!--[if IE 8 ]>     <![endif]-->
      <!--[if IE 9 ]>     <![endif]-->
      <!--[if gt IE 9]>   <![endif]-->
      <!--[if !IE]><!-->  <!--
      <!--[if lt IE 7 ]> I am ie6 <![endif]-->
      <!--[if IE 7 ]>    I am ie7 <![endif]-->
      <!--[if IE 8 ]>    I am ie8 <![endif]-->
      <!--[if IE 9 ]>    I am ie9 <![endif]-->
      <!--[if gt IE 9]>  I am more than ie 9 <![endif]--> <!--[if !IE]><!--> I am Firefox baby! Winning!!!<!--

      This is the View Source from Firefox 4.x:

      <!--[if lt IE 7 ]>  <![endif]-->
      <!--[if IE 7 ]>     <![endif]-->
      <!--[if IE 8 ]>     <![endif]-->
      <!--[if IE 9 ]>     <![endif]-->
      <!--[if gt IE 9]>   <![endif]-->
      <!--[if !IE]><!-->  <!--
      Untitled Document
      <!--[if lt IE 7 ]>  <![endif]-->
      <!--[if IE 7 ]>     <![endif]-->
      <!--[if IE 8 ]>     <![endif]-->
      <!--[if IE 9 ]>     <![endif]-->
      <!--[if gt IE 9]>   <![endif]-->
      <!--[if !IE]><!-->  <!--
      <!--[if lt IE 7 ]> I am ie6 <![endif]-->
      <!--[if IE 7 ]>    I am ie7 <![endif]-->
      <!--[if IE 8 ]>    I am ie8 <![endif]-->
      <!--[if IE 9 ]>    I am ie9 <![endif]-->
      <!--[if gt IE 9]>  I am more than ie 9 <![endif]--> <!--[if !IE]><!--> I am Firefox baby! Winning!!!<!--

      As you can see.. the conditionals that are trying to control the HTML and Body tags do not work. Is there an error in the code or am I correct?

      • Joel, no matter what browser you use to view the source, the conditional comments will still look exactly the same. It’s not like a PHP variable that doesn’t get seen when you view source.

        Conditional comments are always present in the source, and always look the same, no matter what browser is used to view the source.

        The changes will only be evident in how the classes are used. For example, if you say “If NOT IE, add a class of “otherbrowsers” to the body tag”, then you’ll have to use that body tag’s class to actually see the results. So if you do something like:

        body.otherbrowsers {
        	color: blue;

        Then the blue color text will appear in Firefox, but it will not appear for IE, because that class is only visible to non-IE browsers.

        But it will *still* appear in the source code, so don’t get mixed up about that. What appears in the source for conditional comments is not necessarily what will actually affect the browser.

  9. Daniel S says:

    Great compilation, Thanks! Actually, i really dont know this CC techniques. Bookmarked.

  10. Bravewe says:

    If anyhow we have to have use of separate IE css this is “(Conditionals to Customize Markup)” something really good to go with – thanks

  11. Paul Morgan says:

    If you were desperate you could also use it to populate some JavaScript booleans rather that browser sniff…

    var isIE6 = false;
    var isIE7 = false;
    var isIE8 = false;
    <!--[if IE 6 ]>isIE6=true;<![endif]-->
    <!--[if IE 7 ]>isIE7=true;<![endif]-->
    <!--[if IE 8 ]>isIE8=true;<![endif]-->
    if(isIE7){alert('That a broswer lives is because it is straight. A crooked browser survives because it is spared.');}
    if(isIE8){alert('You're a funny broswer. I\'ll kill you last.');}

    I've used this in a couple of occasions where I've not had control over the whole page and was asked not to include a 50k JS library just to implement some minor features cross-broswer but was able to inject code fragments into a HTML snippet. Obviously I didn't just use it to insult IE users :-)

  12. I use this PHP function to set a body-class depending on which OS and browser (+version) a user is running:

    With this you can skip conditional tags and other hacks altogether!

    • Leandro López (inkel) says:

      Your function is not completely failsafe, because the User Agent header is customizable by the user, and some people even completely disallow it, so you cannot rely on it to do this.

  13. Henry says:

    Using the same technic like Simon in my CMS:

    It’s way better than CC and CSS hacks…

  14. Sunny Singh says:

    I use a similar technique to your “body class” one, except I use a separate div for IE instead. And then I close it up with another conditional comment at the bottom. It just seems really messy to have to copy over the body tag like that.

    The operators are cool though, thanks for the tips. I’ll bookmark this.

    • Jeremy says:

      that’s exactly what I was thinking as I read it… I too thought it’d be less clean-up later and having the multiple body tags sitting in comments. great call.

  15. Tony Oravet says:

    Great article on using conditionals. Thankfully IE8 does a much better job of adhering to common standards accepted by other major browsers. However, until large corporation see that IE6 and IE7 are NOT the coolest thing ever, I guess we will all have to keep writing conditionals or using some of these great tips that you have provided here!


  16. Katie says:

    This is great! I almost never use IE-only stylesheets or hacks but it’s nice to know when IE is so frustrating you just don’t feel like moving your code around to fix it anymore.

  17. Very insightful resource. Have bookmarked it for future reference!

  18. Mike Birch says:

    You can exclude IE6 and older by:

    <!-­‐[if gte IE 7]><!­‐>
    	<link rel="stylesheet"
    	href="styles.css" /> 


  19. Excellent article, lots of bits there i didn’t know.

  20. Abe Torres says:

    Thanks for making the time to write these articles!! I wish I could’ve run across your site a year and a half ago when I literally spent countless hours/days looking for ways to make IE6 and IE7 behave!!!!

    Please, please continue to invest your time and effort on your articles and I hope there are more to come. If it weren’t for articles like these, we newbies would be in trouble!

  21. Steve L says:

    nice article, I’ve been using these for years for IE specific script, css, and whatever.
    much easier to work with than any other form of detection based on useragent strings.

    Given that the only question you generally have to deal with, “is it IE or is it the rest of the world?”, and that IE in general is the cause of so many headaches, its nice of MS to provide an easy way out with CCs.

    On a recent project I was slinging xsl for client side transforms, CCs came in really handy… if anyone is interested in how to write one of these in XSL… here you go.

    of course you can combine with the above techniques.

    html, css, js, whatever

  22. Steve L says:

    ok … I believe you, my xmp tag was devoured, intruding on my lazyness

    <xsl:comment><![CDATA[[if IE 8]><div class="IE8"><![endif]]]></xsl:comment>
    <xsl:comment><![CDATA[[if IE 7]><div class="lteIE7 ie7"><![endif]]]></xsl:comment>
    <xsl:comment><![CDATA[[if IE 6]><div class="lteIE7 ie6"><![endif]]]></xsl:comment>
    <xsl:comment><![CDATA[[if (IE 8)|(IE 6)|(IE 7)]></div><![endif]]]></xsl:comment>

  23. alarm says:

    I like the method of assigning classes to the body element to work around the need to send extra HTTP requests to fetch the IE specific code stored in separate external CSS files. But isn’t this method prone to specificity problem?

    since the code might introduce new values for existing selectors and given that it’s imperative to use selectors in CSS stylesheet to target the IE offending element, this would make the new declaration more specific ( has more weight ) and thus override the existing rule found on the same CSS file.

    Your thoughts would be much appreciated

    • Maybe you’re not understanding how those work.

      The IE-only classes will only appear to IE users. So, when they are overridden, the override is desired, because you want to target *only* IE and override whatever selector is in the normal stylesheet.

      You would never do this with a selector that is not declared in the main stylesheet, because the purpose of it is to help you override. Of course, even the IE classes would be used in the main stylesheet, so you could put it right below the other one, so it’s easy to find and easy to maintain.

  24. Martijn says:

    Doesn’t work. For “You Can Target Non-IE Plus Some Versions of IE”, IE shows this at the top of the page:

    <!–[if IE 7]>


    • I just tested it and it seems to work fine for me. I don’t have that problem. I’m using IETester to test IE. What version of IE are you using? Can you show a test page where you see this happening?

  25. paladyn says:

    Lifesaver! Well, OK, a little dramatic, but this was exactly what I needed today to resolve a thorny issue. I was creating a video lightbox using the prettyPhoto jQuery plugin, but IE (of course) insisted on displaying it differently than every other browser. The problem is that there was no param that could be passed in, nor easy access to the plugin’s applicable CSS. The method given above for targeting everything <strong>except </strong> IE worked a treat. Here’s an example from the source

    <!-- Account for the bloody IE box model -->
    <!--[if IE]>
    <a href="testimonials/yolanda-h-project/yolanda-h.html?iframe=true&width=350&height=295" rel="prettyPhoto[iframes]">Testimonial Video</a>
    <!--Give all other browsers the correct size -->												  
    <!--[if !IE]>-->
    <a href="testimonials/yolanda-h-project/yolanda-h.html?iframe=true&width=365&height=305" rel="prettyPhoto[iframes]">Testimonial Video</a>

    Note the difference in dimensions passed in IE versus all others. This resulted in the lightbox having the same appearance in all browsers, and resolved a knotty problem easily. Thanks!

  26. anonymous says:

    Sub Expression Operators are news to me, Thanks for the useful post!

  27. Al Lemieux says:

    Now that IE10 is out, conditional comments are gone. What are the alternatives?

  28. a user says:

    this no longer works

  29. Maria Seleva says:

    I just ran into this: I am a bit surprised that *no one* points out problems with Conditional Comments? Like that they are a maintenance tax? All I can find that quick is Jens Meiert’s work on this – see – but that is totally missing for a balanced view. I am not a hardliner on this but I learned to stay away from Conditional Comments.

Leave a Reply

Comment Rules: Please use a real name or alias. Keywords are not allowed in the "name" field and deep URLs are not allowed in the "Website" field. If you use keywords or deep URLs, your comment or URL will be removed. No foul language, please. Thank you for cooperating.

Markdown in use! Use `backticks` for inline code snippets and triple backticks at start and end for code blocks. You can also indent a code block four spaces. And no need to escape HTML, just type it correctly but make sure it's inside code delimeters (backticks or triple backticks).