Use conditional comments to determine the IE browser version

Source: Internet
Author: User
Tags html comment

IE condition annotation is a special HTML annotation, which can be understood only by IE5.0 and later versions. For example, the common HTML comment is:

<!--This is a comment-->

Only IE-readable IE condition comments are:

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

"Non-IE condition comments ":

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

"Non-specific version IE condition comments" (rarely used ):

<!--[if ! lt IE 7]><![IGNORE[--><![IGNORE[]]>Code for browsers that match the if condition<!--<![endif]-->

In short, all browsers except "Internet Explorer on Windows" will think that conditional comments are just plain HTML comments. You cannot use conditional comments in CSS code. IE condition annotation is a useful method for hiding or displaying specific code on IE. Compared with how to create a bug in CSS, using IE condition annotations to write CSS "hacks" is a more reasonable method. In layman's terms, condition comments are some if judgments, but these judgments are not executed in scripts, but directly executed in html code.

In terms of syntax, this is a quite legal common HTML comment. Any browser will think <! The part between-and-> is a comment to ignore it. However, IE will also see [if IE]>, so as to begin to explain the following code until it encounters <! [Endif]. Therefore, the following code is not displayed in any other browser.

The comparison operator can be used to control the IE version more flexibly. It is used to add a comparison operator before IE ". The valid operators are as follows:

  • Lte: Short for Less than or equal to, that is, Less than or equal.
  • Lt: Short for Less than, that is, Less.
  • Gte: Short for Greater than or equal to, that is, Greater than or equal.
  • Gt: Short for Greater than, that is, Greater.
  • ! : It means not equal to, which is the same as the non-equal identifier in javascript.

Example:

  • <! -[If gt IE 5.5]>/if the IE version is later than 5.5/
  • <! -[If lte IE 6]>/if the IE version is less than or equal to 6/
  • <! -[If! IE]>/If the browser is not IE/

Although it seems that it will be more time-consuming when you use conditional comments for the first time, it will be very convenient when you debug your CSS later. You only need to write HTML comments once for conditional comments, and you need to write long and ugly code for every rule with bugs, in addition, hacks is often written to change other hacks. In addition, conditional comments are legal HTML comments for any browsers that do not support it. The following are some differences between conditional comments and CSS hacks:

Link: http://www.cnblogs.com/oooweb/p/ie-conditional-comments.html

Related Article

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.