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

Website front-end performance optimization: JavaScript and CSS

I have read an article written by the Yahoo team ...

Analysis of product status in interactive design that cannot be ignored in design

In the process of product design, designers always...

Write a simple calculator using JavaScript

The effect is as follows:Reference Program: <!...

Pure CSS to achieve input box placeholder animation and input verification

For more exciting content, please visit https://g...

Linux /etc/network/interfaces configuration interface method

The /etc/network/interfaces file in Linux is used...

MySQL 5.7.18 installation and configuration tutorial under Windows

This article shares the installation and configur...

Modification of the default source sources.list file of ubuntu20.04 LTS system

If you accidentally modify the source.list conten...

How to install ZSH terminal in CentOS 7.x

1. Install basic components First, execute the yu...

A brief discussion on the differences between FTP, FTPS and SFTP

Table of contents Introduction to FTP, FTPS and S...

How to view nginx configuration file path and resource file path

View the nginx configuration file path Through ng...

React entry-level detailed notes

Table of contents 1. Basic understanding of React...