# Conditional comment

In Internet Explorer (IE) versions 5 through 9, a conditional comment is text formatted as a comment in HTML source code with special syntax that IE interprets as a conditional statement. A conditional comment specifies whether to include or exclude code based on the evaluation of a conditional expression and is generally used to support different versions of IE or a browser other than IE.
Support for conditional comments was introduced in IE version 5 and dropped in version 10. In IE 10, conditional comments are not supported when the page is in standards mode (document mode 10). There exists an adjacent technology in JScript (Microsoft's non-standard implementation of JavaScript) called conditional compilation, which uses @-prefixed codes in the style of preprocessor macros in C and C++. Conditional compilation was introduced in IE 4, and was supported up through IE 10, in both standards and compatibility modes.

## Syntax

By definition, a code comment is text that is ignored by the translator – the browser, IE, in this case. But, the conditional comment feature adds syntax for a conditional statement that is formatted as a comment. Therefore, some text that is formatted as a comment is actually not a comment. It is markup code. Note that other browsers – that do not support the conditional comment feature – ignore them since they are formatted as comments.
An HTML code comment is text that starts with <!-- and ends with -->. An IE conditional comment is delimited the same, but is like:
The conditional comment has two forms. The one above is called downlevel hidden. The other form, called downlevel revealed, is not formatted as a comment yet is called a conditional comment none-the-less. It is formatted like:
The code between the if and endif markup can be any HTML content that is included if the condition evaluates true or excluded otherwise.
An expression can contain the name of a feature, literal values and comparison operators. Feature names include:
* IE – version of IE that is parsing the HTML document
* WindowsEdition – edition of Windows hosting IE
Literal values are either numeric or Boolean (true/false).
Comparison operators are:
* lt – less than; evaluates as true if the left argument is less than the right argument
* lte – less than or equal; evaluates as true if the left argument is less than or equal to the right argument
* gt – greater than; evaluates as true if the left argument is greater than the right argument.
* gte – greater than or equal; evaluates as true if the left argument is greater than or equal to the right argument.
* (expression) – subexpression; used with logical operators to create a more complex expression
* ! – logical not; placed before a feature, operator or subexpression to invert its value
* & – logical and; evaluates as true if all subexpressions evaluate to true
* | – logical or; evaluates as true if any of the subexpressions evaluates to true

## Examples

A downlevel-hidden conditional comment that includes code for IE version 8:
A downlevel-hidden conditional comment that includes code for IE version 7 and less:
A downlevel-revealed conditional comment (which is not an HTML comment despite the name) that includes code if the browser is not IE.
Microsoft acknowledges this syntax is not standardized markup, intending these tags to be overlooked by other browsers and expose the content in the middle. In order to ensure compliance with W3C standards, some web developers use an alternative technique for downlevel-revealed conditional comments:
While possibly confusing, this syntax is valid (X)HTML and is useful for conditional sections intended specifically for non-IE browsers; if the condition evaluates to true (for example, if targeting non-IE browsers and on some versions of IE), IE displays the --> present before the HTML content. This problem is resolved by prepending <! to the initial --> as follows:
The extra <! is ignored by non-IE browsers, and also by IE regardless of the condition. If false, everything within the conditional comment is ignored. If true, the resulting tag <!--> is unrecognized and therefore ignored.

## Conditional compilation

IE 4 introduced a similar mechanism for JScript, called conditional compilation that was dropped in version 11 standards mode.
Example code: