IE conditional statement

Source: Internet
Author: User
Tags comment tag html comment

Conditional comments work as follows: Copy codeThe Code is as follows: <! -- [If IE 6]> Special instructions for IE 6 here <! [Endif] -->
[Code]

Their basic structure is the same as an HTML comment (<! -->). Therefore all other browsers will see them as normal comments and will ignore them entirely.
IE, though, has been programmed to recognize the special <! -- [If IE]> syntax, resolves the ifand parses the content of the conditional comment as if it were normal page content.
Since conditional comments use the HTML comment structure, they can only be encoded in HTML files, and not in CSS files. I 'd have preferred to put the special styles in the CSS file, but that's impossible. you can also put an entire new <link> tag in the conditional comment referring to an extra style sheet.
Example
Below I added a lot of conditional comments that print out messages according to your IE version.

Note however, that if you use multiple login ers on one computer, the conditional comments will render as if all these Explorer versions are the highest Explorer version available on your machine (usually Explorer 6.0 ).

Test
Below are a few conditional comments that reveal the IE version you're using.

According to the conditional comment this is IE
According to the conditional comment this is IE 7
According to the conditional comment this is IE lower than 9
According to the conditional comment this is IE lower or equal to 7
According to the conditional comment this is IE greater than 6

Code
The syntax I use is:
[Code]
<P class = "accent">
<! -- [If IE]>
According to the conditional comment this is IE <br/>
<! [Endif] -->
<! -- [If IE 6]>
According to the conditional comment this is IE 6 <br/>
<! [Endif] -->
<! -- [If IE 7]>
According to the conditional comment this is IE 7 <br/>
<! [Endif] -->
<! -- [If IE 8]>
According to the conditional comment this is IE 8 <br/>
<! [Endif] -->
<! -- [If IE 9]>
According to the conditional comment this is IE 9 <br/>
<! [Endif] -->
<! -- [If gte IE 8]>
According to the conditional comment this is IE 8 or higher <br/>
<! [Endif] -->
<! -- [If lt IE 9]>
According to the conditional comment this is IE lower than 9 <br/>
<! [Endif] -->
<! -- [If lte IE 7]>
According to the conditional comment this is IE lower or equal to 7 <br/>
<! [Endif] -->
<! -- [If gt IE 6]>
According to the conditional comment this is IE greater than 6 <br/>
<! [Endif] -->
<! -- [If! IE]> -->
According to the conditional comment this is not IE <br/>
<! -- <! [Endif] -->
</P>

Note the special syntax:

Gt: greater
Lte: less than or equal
Also note the last one. It has a different syntax, and its contents are shown in all browsers that are not IE:Copy codeThe Code is as follows: <! -- [If! IE]> -->

CSS hack?
Are conditional comments CSS hacks? Strictly speaking, yes, since they can serve to give special style instructions to some browsers. however, they do not rely on one browser bug to solve another one, as all true CSS hacks do. besides, they can be used for more than CSS hacks only (though that rarely happens ).

Since conditional comments are not based on a browser hack but on a deliberate feature I believe they are safe to use. sure, other browsers cocould implement conditional comments, too (though as yet none have done so), but they're unlikely to react to the specific query <! -- [If IE]>.

I use conditional comments, though sparingly. first I see if I can find a real CSS solution to an Explorer Windows problem. if I can't, though, I don't hesitate to use them.

Comment tag
A reader told me IE also supports the (non-standard) <comment> tag.

<P> This is <comment> not </comment> IE. </p>

This isIE.

This tag might be a replacement for! IE conditional comment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.