Search code examples
htmlcssinternet-explorerconditional-comments

Valid conditional comments to target non-IE browsers and specific IE versions


How do you use conditional comments to target

  1. Internet Explorer only

  2. Certain IE versions

  3. Certain IE versions and all other browsers

  4. No IE, just other browsers

in a way that validates? (validator.w3.org/)


Solution

  • I tested these and they're valid HTML.

    IMPORTANT: Only IE 5 through 9 support conditional comments.

    <!-- 1. IE 5-9 only: -->
    
    <!--<![if IE]>
        <link rel="stylesheet" type="text/css" href="ie-5-through-9.css">
    <![endif]>-->
    
    
    <!-- 2. Some IE versions -->
    
    <!--<![if gte IE 8]>
        <link rel="stylesheet" type="text/css" href="ie-8-and-9.css">
    <![endif]>-->
    
    
    <!-- 3. Some IE versions, plus all non-IE browsers -->
    
    <!--[if gte IE 8]>-->
        <link rel="stylesheet" type="text/css" href="ie-gte8-and-non-ie.css">
    <!--<![endif]-->
    
    
    <!-- 4. All browsers except IE 5-9 -->
    
    <!--[if !IE]>-->
        <link rel="stylesheet" type="text/css" href="modern-ie-and-non-ie.css">
    <!--<![endif]-->