Home >Web Front-end >HTML Tutorial >Detailed explanation of conditional comments in IE browser
IE conditional comments are a non-standard logical statement provided by Microsoft since IE5. Its function is to flexibly import different html elements, such as style sheets, html tags, etc. into different IE versions of browsers. Obviously, the biggest advantage of this method is that it is a compatible solution officially given by Microsoft and it can also pass W3C validation.
Let’s take a look at a few examples:
<!--[if IE]> <link type="text/css" rel="stylesheet" href="my.css" /> <![endif]-->
Because only IE5 and above versions begin to support IE conditional comments, all "only IE" can recognize it means "only IE5 and above versions" can recognize it.
<!--[if IE 8]> <link type="text/css" rel="stylesheet" href="my.css" /> <![endif]-->
Identify a specific IE version, whether it is higher or lower. The above example can only be recognized by IE8.
<!--[if !IE 7]> <link type="text/css" rel="stylesheet" href="my.css" /> <![endif]-->
In the above example, the specific version of IE7 cannot be recognized, but other versions can be recognized, of course, it must be IE5 or above.
<!--[if gt IE 7]> <link type="text/css" rel="stylesheet" href="my.css" /> <![endif]-->
In the above example, only versions higher than IE7 can be recognized. IE7 is not recognized.
<!--[if gte IE 7]> <link type="text/css" rel="stylesheet" href="my.css" /> <![endif]-->
In the above example, IE7 and higher versions can be recognized.
<!--[if lt IE 7]> <link type="text/css" rel="stylesheet" href="my.css" /> <![endif]-->
In the above example, only versions lower than IE7 can be recognized, and IE7 cannot.
<!--[if lte IE 7]> <link type="text/css" rel="stylesheet" href="my.css" /> <![endif]-->
In the above example, IE7 and lower versions can be recognized.
The above codes may seem difficult to remember, but in fact, they are easy to remember as long as you explain the keywords a little.
lt: It is the abbreviation of Less than, which means less than.
lte: It is the abbreviation of Less than or equal to, which means less than or equal to.
gt: It is the abbreviation of Greater than, which means greater than.
gte: It is the abbreviation of Greater than or equal to, which means greater than or equal to.
!: It means not equal to, which is the same as the inequality judger in JavaScript.
If you explain it this way, it will be much easier to remember.
1. Some people will try to use dbf5feeb0d676154b00e7fac3915442f to define the situation under non-IE browsers, but note: conditional comments can only be executed under IE browsers, and this code is blocked under non-IE browsers. Ignore it as a comment.
2. We usually use IE conditional comments to load different css according to different browsers, thereby solving style compatibility issues. In fact, it can do more. It can protect any code block - HTML code block, JavaScript code block, server-side code... Take a look at the code below.
<!--[if IE]> <script type="text/javascript"> alert("你使用的是IE浏览器!"); </script> <![endif]-->
The above is the detailed content of Detailed explanation of conditional comments in IE browser. For more information, please follow other related articles on the PHP Chinese website!