Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
HTML and CSS
Creating Web Sites
 About Conditional Comments
About Conditional Comments

One of the most common operations performed in a Web page is to detect the browser type and version. Browser detection is performed to ensure that the content presented to the browser is compatible and renders correctly. The browser type can be detected using many different techniques. Most methods of browser detection make use of script on the server or client.

This article introduces conditional comments, which offer certain advantages over scripted browser detection techniques. Conditional comments make it easy for developers to take advantage of the enhanced features offered by Microsoft Internet Explorer 5 and later versions, while writing pages that downgrade gracefully in less-capable browsers or display correctly in browsers other than Windows Internet Explorer. Conditional comments are the preferred means of differentiating Cascading Style Sheets (CSS) rules intended for specific versions of Internet Explorer.

The following topics are discussed in this document.

Terminology

The following terms are used in this article.

TermDescription
expressionA combination of operators, features, and/or values used to form a conditional statement.
downlevel browserAny browser except Internet Explorer 5 and later versions. For the purposes of this article, downlevel refers specifically to any browser or browser version that does not support conditional comments.
uplevel browserInternet Explorer 5 and later versions, which support conditional comments.
downlevel-hiddenA conditional comment block that is ignored by downlevel browsers. Internet Explorer 5 and later versions render the HTML content if the expression evaluates to true.
downlevel-revealedA conditional comment block that is parsed by downlevel browsers. Internet Explorer 5 and later versions also render the HTML content if the expression evaluates to true.

Benefits of Using Conditional Comments

Conditional comments have certain advantages over scripting methods of browser detection.

  • Low client-side impact.

    When a downlevel browser encounters a downlevel-hidden conditional comment, the browser skips over the HTML inside the comment, and the content elements are not parsed, downloaded, or rendered. This saves client machine resources.

  • No script required.

    Conditional comments do not require scripting and DHTML, and when no scripting is used in a Web page, no scripting engine needs to be loaded. Conditional comments are processed during the downloading and parsing phase, so only the content that is targeted for the browser is actually downloaded. Conditional comments can be combined freely with other browser detection techniques.

  • Separate code from detection logic.

    Using conditional comments, script logic can be separated into smaller and simpler segments of code, which are easier to maintain and understand. Plus, code segments are loaded only by the browser version for which they were intended.

  • Cross-browser.

    Conditional comments have been around since Internet Explorer 5, but their use is not restricted to Internet Explorer alone. Conditional comments can be used to customize content delivered to browsers that support conditional comments and those that do not.

Syntax of Conditional Comments

The basic syntax of each type of comment is shown in the following table. The first comment shown is the basic HTML Comment, which is included for the purpose of comparison and to illustrate the different syntax used by each type of conditional comment.

Comment typeSyntax or possible value
standard HTML comment<!-- Comment content  -->
downlevel-hidden<!--[if expression]> HTML <![endif]-->
downlevel-revealed<![if expression]> HTML <![endif]>

The HTML shown inside the syntax block in each of the conditional comments denotes any block of HTML content, including script. Both types of conditional comment use a conditional expression to indicate whether the content inside the comment block should be parsed or ignored.

The conditional expression is formed from a combination of feature, operator, and/or value, depending on the feature. The following table shows the supported features and describes the values each feature supports.

ItemExampleComment
IE[if IE]The string "IE" is a feature corresponding to the version of Internet Explorer used to view the Web page.
value[if IE 7]An integer or floating point numeral corresponding to the version of the browser. Returns a Boolean value of true if the version number matches the browser version. For more information, see Version Vectors.
WindowsEdition[if WindowsEdition]Internet Explorer 8 on Windows 7. The string "WindowsEdition" is a feature corresponding to the edition of Microsoft Windows used to view the Web page.
value[if WindowsEdition 1]An integer corresponding to the edition of Windows used to view the Web page. Returns a Boolean value of true if the value matches the edition being used. For information about supported values and the editions they describe, see the pdwReturnedProductType parameter of the GetProductInfo function.
true[if true]Always evaluates to true.
false[if false]Always evaluates to false.

The following table describes the operators that can be used to create conditional expressions.

ItemExampleComment
![if !IE]The NOT operator. This is placed immediately in front of the feature, operator, or subexpression to reverse the Boolean meaning of the expression.
lt[if lt IE 5.5]The less-than operator. Returns true if the first argument is less than the second argument.
lte[if lte IE 6]The less-than or equal operator. Returns true if the first argument is less than or equal to the second argument.
gt[if gt IE 5]The greater-than operator. Returns true if the first argument is greater than the second argument.
gte[if gte IE 7]The greater-than or equal operator. Returns true if the first argument is greater than or equal to the second argument.
( )[if !(IE 7)]Subexpression operators. Used in conjunction with boolean operators to create more complex expressions.
&[if (gt IE 5)&(lt IE 7)]The AND operator. Returns true if all subexpressions evaluate to true
|[if (IE 6)|(IE 7)]The OR operator. Returns true if any of the subexpressions evaluates to true.

Downlevel-hidden Conditional Comments

The following sample shows a downlevel-hidden conditional comment, which contains a short paragraph of text.

<!--[if IE 8]>
<p>Welcome to Internet Explorer 8.</p>
<![endif]-->

The downlevel-hidden conditional comment contains hyphens ("--") in the opening and closing tag, similar to the basic HTML Comment. The condition appears in the opening portion of the tag, and [endif] is placed prior to the closing portion of the tag. The content is placed inside the comment tags.

Because the first four characters and the last three characters of the comment are identical to a basic HTML Comment element, downlevel browsers ignore the HTML content inside the comment block. Since content is effectively hidden from browsers that do not support conditional comments, this type of conditional comment is called downlevel-hidden.

If the result of the conditional expression is true, the content inside the comment block is parsed and rendered by Internet Explorer 5 and later versions. This behavior makes the downlevel-hidden conditional comment particularly useful for content that has been specifically designed for Internet Explorer.

The following sample illustrates how a client-side script block can be placed inside a conditional comment; in this case, a message is displayed in Internet Explorer 5 and later.

<!--[if gte IE 7]>
<SCRIPT LANGUAGE="Javascript">
alert("Congratulations! You are running Internet Explorer 7 or greater.");
</SCRIPT>
<P>Thank you for closing the message box.</P>
<![endif]-->
This feature requires Microsoft Internet Explorer 5 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

In the preceding example, only the major digit of the browser version is compared because it is the only digit specified in the conditional expression. To compare both major and minor version numbers, specify both digits. For further explanation and examples on specifying the browser's version number, see Version Vectors.

Downlevel-revealed Conditional Comments

The downlevel-revealed conditional comment enables you to include content in browsers that do not recognize conditional comments. Although the conditional comment itself is ignored, the HTML content inside it is not. Internet Explorer 5 and later versions also parse and render the content if the conditional expression evaluates to true. The downlevel-revealed conditional comment complements the downlevel-hidden conditional comment.

The following snippet shows a typical downlevel-revealed conditional comment.

<![if lt IE 8]>
<p>Please upgrade to Internet Explorer version 8.</p>
<![endif]>

When comparing this type of comment to the basic HTML Comment, notice that there are no hyphens ("--") immediately after the opening "<!" or immediately before the closing ">" of the comment block; therefore, the comment delimiters are treated as unrecognized HTML. Because the browser does not recognize the downlevel-revealed conditional comment, it does nothing with it.

Version Vectors

Conditional expressions can be used to determine the version of the browser used to view a Web page or the edition of Windows used to run the browser. In both cases, the value of the expression is called a version vector and must be specified correctly to obtain the desired result.

When detecting the version of the browser, the major browser version is specified as integer. To check for a minor browser version, follow the version vector by a decimal point and four digits. For example, the version vector for the release build of Internet Explorer 5.5 is 5.5000.

In the following example, only the major version number is specified; therefore, the sample evaluates as true for both Internet Explorer 5 and Internet Explorer 5.5.

<!--[if IE 5]>
<p>Welcome to any incremental version of Internet Explorer 5!</p>
<![endif]-->

The following test correctly identifies Internet Explorer 5.

<!--[if IE 5.0000]>
<p>Welcome to Internet Explorer 5.0!</p>
<![endif]-->
Note  Internet Explorer 5, which shipped with Windows 2000, has a version vector equal to 5.0002. Therefore, the conditional expression [if lte IE 5.0000] returns false when evaluated in the release build of Internet Explorer 5.

Internet Explorer 8 on Windows 7. When detecting the edition of Windows, the version vector is an integer corresponding to one of the values supported by the pdwReturnedProductType parameter of the GetProductInfo function.

<!--[if WindowsVersion 1]>
<p>You are using Windows Ultimate Edition.</p>
<![endif]-->

Add-on developers can use custom version vectors to provide version information to Web pages. To define a custom version vector, add a REG_SZ value to the Version Vector Registry key. The name of the new key defines the feature value to use in a conditional comment, as shown in the following example.

HKEY_LOCAL_MACHINE
     Software
          Microsoft
               Internet Explorer
                    Version Vector
                         Contoso = 0.9

The previous example uses a custom version vector to indicate that a pre-release version (0.9) of the fictional Contoso control is installed on a user's computer. The following example shows how a conditional comment could use this information.

<!--[if lt Contoso 2]>
<p>Your version of the Contoso control is out of date; please update to the latest.</p>
<![endif]-->

Examples

Here are some more examples of conditional comments.
<!--[if IE]><p>You are using Internet Explorer.</p><![endif]-->
<![if !IE]><p>You are not using Internet Explorer.</p><![endif]>

<!--[if IE 7]><p>Welcome to Internet Explorer 7!</p><![endif]-->
<!--[if !(IE 7)]><p>You are not using version 7.</p><![endif]-->

<!--[if gte IE 7]><p>You are using IE 7 or greater.</p><![endif]-->
<!--[if (IE 5)]><p>You are using IE 5 (any version).</p><![endif]-->
<!--[if (gte IE 5.5)&(lt IE 7)]><p>You are using IE 5.5 or IE 6.</p><![endif]-->
<!--[if lt IE 5.5]><p>Please upgrade your version of Internet Explorer.</p><![endif]-->

<!--[if true]>You are using an <em>uplevel</em> browser.<![endif]-->
<![if false]>You are using a <em>downlevel</em> browser.<![endif]>

<!--[if true]><![if IE 7]><p>This nested comment is displayed in IE 7.</p><![endif]><![endif]-->
This feature requires Microsoft Internet Explorer 5 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Related Topics

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Strange IE 5.5      Lacrymoc&#233;phale   |   Edit   |   Show History

I have experimented some strange behaviour with IE 5.5

IE 5.5 seems to not react to float numbers with a floor value of 5 that have less than 4 digits after dot
<!--[if IE 5.5]> is not interpreted
<!--[if IE 5.500]> is not interpreted
<!--[if IE 5.5000]> is interpreted
<!--[if IE 5.50000]> is not interpreted

<!--[if lt IE 5.6]> is not interpreted
<!--[if lt IE 5.6000]> is interpreted

Is it a known bug?

So be careful, expecially with [if IE 5.5]

Tags What's this?: dhtml (x) ie5.5 (x) Add a tag
Flag as ContentBug
Proposal of CSS CC      Freyjkell   |   Edit   |   Show History
// Hidden syntax (C-style):
@cc lte IE 8
{
	/*...*/
}
  
// Revealed syntax (BASIC-style):
@cc !IE begin;
	/*...*/
@cc end;
  
// Non-IE browsers would simply ignore @cc (standard behavior with
// unknown @ blocks and instructions).
Flag as ContentBug
Detecting Firefox      Skyfish ... PaulAllsopp   |   Edit   |   Show History

If the following code fails to exclusively detect Firefox..

  
<!--[if !IE]>
...statements...
<![endif]-->
  

Use "Downlevel-revealed Conditional Comments" to get it working...

  
<![if !IE]>
...statements...
<![endif]>
  

Example to force Firefox to use an exclusive css..

  
  <![if !IE]>
    <link href="css/ff.css" rel="stylesheet" type="text/css" />
  <![endif]>
  
Tags What's this?: firefox (x) ie (x) Add a tag
Flag as ContentBug
Valid HTML for downlevel-revealed conditional comments      Benjamin Hawkes-Lewis ... seezee   |   Edit   |   Show History

This topic used to include a discussion of how to create downlevel-revealed conditional comments that are valid HTML 4.01. This discussion appears to have gone missing. Thanks to search engine caches, we can reproduce it below:

Fixing Validation Errors

The downlevel-revealed conditional comment syntax is flagged as invalid HTML by some validation services. Additional characters can be added to construct a valid HTML Comment before and after the HTML content to be revealed.

<!--[if !IE]><!--> HTML <!--<![endif]-->

In the example above, the negative conditional expression prevents Internet Explorer from displaying the HTML content within the downlevel-revealed conditional comment block. However, if the conditional expression evaluates to true, the closing "-->" of the first comment appears along with the HTML content in Internet Explorer. To hide these characters from Internet Explorer users, add "<!" as follows:

<!--[if IE 7]><!--> HTML <!--<![endif]-->
What about supportEmptyParas      bgailer ... sharathe   |   Edit   |   Show History

IE displays comments like <!--[if !supportEmptyParas]--> <!--[endif]-->. The whole comment shows up!

And how come there is no reference to supportEmptyParas in this discussion or anywhere else?


[[jsudds.MSFT]] The supportEmptyParas feature appears to be available only inside Microsoft Word documents, and is only used to determine whether to insert a non-breaking space inside empty paragraphs, as follows:

<p class=MsoNormal><![if !supportEmptyParas]>&nbsp;<![endif]><o:p></o:p></p>


HTH.



Even though it is a feature of Microsoft word , supportEmptyParas tags appear on IE browser for any content copy pasted Microsoft word. Internet Explorer should be intelligent enough to ignore these conditional comments??? .

Tags What's this?: Add a tag
Flag as ContentBug
Not in HTML applications      yecril   |   Edit   |   Show History

HTML applications are not IE but they process conditional comments. The following code is hidden from Internet Explorer, represents a comment for everybody else but will be visible in an HTML application:

<!--[if !IE]>I am an HTML application<![endif]-->



WARNING: Carefully consider gt(e)      John Sudds   |   Edit   |   Show History
Keep in mind that IE is embracing standards in the current version and will continue to do so in subsequent versions. Whenever you use a gt(e) [greater than (or equal)] in your comparision operator, ask yourself:

Are you doing this to force a quirky behavior, or to adopt a newly implemented standard behavior?

Approximately 18% of sites that use conditional comments have written rules like this:

[if ie]
[if gt(e) ie *]

These rules are guaranteed to *include* future versions of Internet Explorer. Is that what you intended to do? To be safe, use conditional comments to switch on a quirky CSS rule with a lt(e) [less than (or equal)] or an absolute version number, like this:

[if lt(e) ie *]
[if ie X]

These rules explicitly select only those IE versions that you have tested and know will work.

Remember: Any conditional comment that supposes that quirky behavior will continue indefinitely will very likely be wrong at some point in the future.
IE8 reports as IE7 in an IFRAME      AdamRLeggett   |   Edit   |   Show History
I've done numerous tests and as far as I can tell - IE reports as 7 when the condition is tested from within a page loaded in an IFRAME.

The following code works correctly outside of an IFRAME

<html>
<!--[if lte IE 7]>
<p>Internet Explorer 7 or older</p>
<![endif]-->
<!--[if gte IE 8]>
<p>Internet Explorer 8 or later</p>
<![endif]-->
</html>

But if it's loaded from an IFRAME in another page - it incorrectly shows that it's IE 7

Does anyone have any suggestions for an alternate approach?

[EDIT - I ended up using JavaScript - but even then had to jump through hoops because compatiblity mode still reports IE8 as IE7. The trick is to look for 'trident' in the user agent string - as per this blog http://blogs.msdn.com/giorgio/archive/2009/04/14/how-to-detect-ie8-using-javascript-client-side.aspx]
Tags What's this?: 8 (x) condtional (x) ie (x) iframe (x) Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker