Conditional comment style writing method and sample code

Conditional comment style writing method and sample code

As front-end engineers, IE must be familiar to us. When writing code to implement a design draft, various IE compatibility issues often arise. There are many ways to deal with compatibility. The easiest way is to write different styles for different browsers.
Each browser in IE has its own set of comments. Annotations belonging to your own browser will be recognized specifically in your own browser and not by his browser. Next we will show you how to write conditional styles.

It is a comment in HTML language. It is a note added to the code to let yourself or your team members know what you wrote, when you wrote it, etc. This comment will not be displayed in the browser, but you can see this part when viewing the source code of the page, such as the following simple example:

Copy code
The code is as follows:

<!-- HTML comment here -->
<div>HTML Comment</div>

The content in the above code is the comment part. Then if we add a conditional statement based on the above, we can get the conditional comment we want. IE has its own set of conditional comments. There are many benefits of conditional comments, the main one is to make your website style neat and refreshing, and the second is to easily be compatible with all eligible browsers. These benefits give us an instant interest in conditional comments. So how should conditional comments be written? Let me take a look at it below.

How to write conditional styles <br />The method is very simple. We just need to import the external style sheet in the normal way, and then nest conditional comments outside. Before using conditional comment statements, there are several conditional comment attributes that we must understand and use:
1. gt (great than): select versions greater than the conditional version, excluding the conditional version itself;
2. lt (less than): This is the opposite of gt, which means selecting the version below the conditional version, excluding the conditional version itself;
3. gte (great than or equal): select versions greater than the conditional version and including the conditional version itself;
4. lte (less than or equal): select the version below the conditional version, including the conditional version itself;
5. !: Select all versions except the conditional version, regardless of high or low.

Conditional Style Examples
1. Support all IE browsers

Copy code
The code is as follows:

<!--[if IE]>
<link rel="stylesheet" href="IE.css" type="text/css"/>
<![endif]-->

2. Support all browsers except IE

Copy code
The code is as follows:

<!--[if !IE]>
<link rel="stylesheet" href="noIE.css" type="text/css"/>
<![endif]-->

or

Copy code
The code is as follows:

<!--[if !IE]><!-->
<link rel="stylesheet" href="noIE.css" type="text/css" />
<!--<![endif]-->

3. Only supports IE10

Copy code
The code is as follows:

<!--[if IE 10]>
<link rel="stylesheet" type="text/css" href="IE10.css">
<![endif]-->

4. Only supports IE9

Copy code
The code is as follows:

<!--[if IE 9]>
<link rel="stylesheet" type="text/css" href="IE9.css">
<![endif]-->

5. Only supports IE8

Copy code
The code is as follows:

<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="IE8.css">
<![endif]-->

6. Only supports IE7

Copy code
The code is as follows:

<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="IE7.css">
<![endif]-->

7. Only supports IE6

Copy code
The code is as follows:

<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="IE6.css">
<![endif]-->

8. Support IE10 and below (IE9 and below)

Copy code
The code is as follows:

<!--[if lt IE 10]>
<link rel="stylesheet" type="text/css" href="ie9Down.css">
<![endif]-->

or

Copy code
The code is as follows:

<!--[if lte IE 9]>
<link rel="stylesheet" type="text/css" href="ie9Down.css">
<![endif]-->

9. Support IE9 and below versions (IE8 and below versions)

Copy code
The code is as follows:

<!--[if lt IE 9]>
<link rel="stylesheet" type="text/css" href="ie8Down.css">
<![endif]-->

or

Copy code
The code is as follows:

<!--[if lte IE 8]>
<link rel="stylesheet" type="text/css" href="ie8Down.css">
<![endif]-->

10. Support IE8 and below (IE7 and below)

Copy code
The code is as follows:

<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="ie7Down.css">
<![endif]-->

or

Copy code
The code is as follows:

<!--[if lte IE 7]>
<link rel="stylesheet" type="text/css" href="ie7Down.css">
<![endif]-->

11. Support IE7 and below (IE6 and below)

Copy code
The code is as follows:

<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="ie6Down.css">
<![endif]-->

or

Copy code
The code is as follows:

<!--[if lte IE 6]>
<link rel="stylesheet" type="text/css" href="ie6Down.css">
<![endif]-->

12. Versions higher than IE9 (IE10 and above)

Copy code
The code is as follows:

<!--[if gt IE 9]>
<link rel="stylesheet" type="text/css" href="ie10Up.css">
<![endif]-->

or

Copy code
The code is as follows:

<!--[if gte IE 10]>
<link rel="stylesheet" type="text/css" href="ie10Up.css">
<![endif]-->

13. Versions higher than IE8 (IE9 and above)

Copy code
The code is as follows:

<!--[if gt IE 8]>
<link rel="stylesheet" type="text/css" href="ie9Up.css">
<![endif]-->

or

Copy code
The code is as follows:

<!--[if gte IE 9]>
<link rel="stylesheet" type="text/css" href="ie9Up.css">
<![endif]-->

14. Versions higher than IE7 (IE8 and above)

Copy code
The code is as follows:

<!--[if gt IE 7]>
<link rel="stylesheet" type="text/css" href="ie8Up.css">
<![endif]-->

or

Copy code
The code is as follows:

<!--[if gte IE 8]>
<link rel="stylesheet" type="text/css" href="ie8Up.css">
<![endif]-->

15. Versions higher than IE6 (IE7 and above)

Copy code
The code is as follows:

<!--[if gt IE 6]>
<link rel="stylesheet" type="text/css" href="ie7Up.css">
<![endif]-->

or

Copy code
The code is as follows:

<!--[if gte IE 7]>
<link rel="stylesheet" type="text/css" href="ie7Up.css">
<![endif]-->

16. Reference JavaScript tags with conditional comments

Copy code
The code is as follows:

<!--[if IE]>
<script type="text/javascript" src="IE.js"></script>
<![endif]-->

The above shows how to create conditional comment styles. You can use one or more of them according to your needs. Finally, conditional comments are mainly aimed at IE browsers, so we also call them IE conditional comments. In this way, managing separate styles written for IE compatibility brings great convenience and benefits. With conditional comments we can easily write conditional styles and solve problems in browsers. This is the end of the introduction to conditional comments.

<<:  MySQL example to explain single-row functions and character math date process control

>>:  Build a severe weather real-time warning system with Node.JS

Recommend

Example code for realizing charging effect of B station with css+svg

difficulty Two mask creation of svg graphics Firs...

Supplementary article on front-end performance optimization

Preface I looked at the previously published arti...

JavaScript singleton mode to implement custom pop-up box

This article shares the specific code of JavaScri...

Will css loading cause blocking?

Maybe everyone knows that js execution will block...

Install centos7 virtual machine on win10

1. Download VMware Workstation 64 version https:/...

A brief discussion on how Tomcat breaks the parent delegation mechanism

Table of contents JVM Class Loader Tomcat class l...

How to deploy MongoDB container with Docker

Table of contents What is Docker deploy 1. Pull t...

jQuery realizes the full function of shopping cart

This article shares the specific code of jQuery t...

Some tips for using less in Vue projects

Table of contents Preface 1. Style penetration 1....

Markup Languages ​​- What to learn after learning HTML?

Click here to return to the 123WORDPRESS.COM HTML ...

Detailed explanation of docker-machine usage

Docker-machine is a Docker management tool offici...

Detailed steps to install JDK and Tomcat in Linux environment

Table of contents 1. Install JDK Manual Installat...

Analysis of the process of building a cluster environment with Apache and Tomcat

In fact, it is not difficult to build an Apache c...

How to deploy kafka in docker

Table of contents 1. Build Docker 2. Enter the co...