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

Introduction to the use of MySQL source command

Table of contents Thoughts triggered by an online...

JS implements dragging the progress bar to change the transparency of elements

What I want to share today is to use native JS to...

Implementation of scheduled backup in Mysql5.7

1. Find mysqldump.exe in the MySQL installation p...

Detailed explanation of how to migrate a MySQL database to another machine

1. First find the Data file on the migration serv...

Detailed explanation of MySQL transaction isolation level and MVCC

Table of contents Transaction Isolation Level Pro...

WeChat applet implements SMS login in action

Table of contents 1. Interface effect preview 2.u...

CSS3 to achieve dynamic background gradient effect

Learning CSS3 is more about getting familiar with...

MySQL 5.7.25 installation and configuration method graphic tutorial

There are two types of MySQL installation files, ...

Detailed explanation of this pointing problem in JavaScript

Preface The this pointer in JS has always been a ...

Detailed installation history of Ubuntu 20.04 LTS

This article records the creation of a USB boot d...

How to use css overflow: hidden (overflow hiding and clearing floats)

Overflow Hide It means hiding text or image infor...

PostgreSQL materialized view process analysis

This article mainly introduces the process analys...

Div adaptive height automatically fills the remaining height

Scenario 1: Html: <div class="outer"...