CSS introduction method

小云云
Release: 2017-12-08 14:53:15
Original
1290 people have browsed it

How to introduce CSS in HTML

There are 4 ways to introduce CSS in HTML. Two of them are to add CSS code directly in the HTML file, and the other two are to introduce external CSS files. Let’s take a look at these methods and their advantages and disadvantages.

Inline method

The inline method refers to the <span style="font-size: 16px;">#style# directly in the HTML tag </span>## Add CSS to the attribute.

Example:

<span style="font-size: 16px;"><p style="background: red"></p><br></span>
Copy after login
Copy after login

This is usually a bad way of writing, it can only change the style of the current label, if you want multiple <p><span style="font-size: 16px;"></span> have the same style, you have to repeat it for each <p><span style="font-size: 16px;"></span> Add the same style. If you want to modify a style, you have to modify the code in all styles. Obviously, introducing CSS code inline will cause the HTML code to become verbose and make the web page difficult to maintain.

Embedding method

The embedding method refers to the <style> in the HTML header. <span style="font-size: 16px;"></span> Write CSS code under the tag.

Example:

<span style="font-size: 16px;"><head><br>    <style><br><br>    .content {<br>        background: red;<br>    }<br><br>    </style><br></head><br></span>
Copy after login
Copy after login

Embedded CSS is only valid for the current web page. Because the CSS code is in the HTML file, it makes the code more concentrated, which is usually advantageous when we write template web pages. Because someone looking at the template code can see the HTML structure and CSS styles at a glance. Because embedded CSS is only valid for the current page, when multiple pages need to introduce the same CSS code, writing this way will lead to code redundancy and is not conducive to maintenance.

Link method

The link method refers to using the HTML header<head><span style="font-size: 16px;"></span> tag introduces external CSS files.

Example:

<span style="font-size: 16px;"><head><br>    <link rel="stylesheet" type="text/css" href="style.css"><br></head><br></span>
Copy after login
Copy after login

This is the most common and recommended way to introduce CSS. Using this approach, all CSS code only exists in a single CSS file, so it has good maintainability. And all CSS code only exists in the CSS file. The CSS file will be introduced when it is loaded for the first time. When switching pages in the future, you only need to load the HTML file.

Import method

Import method refers to using CSS rules to introduce external CSS files.

Example:

<span style="font-size: 16px;"><style><br>    @import url(style.css);<br></style><br></span>
Copy after login
Copy after login

Compare link method and import method

Link method (used below link instead) and import method (replaced by @import below) are both ways to introduce external CSS files. Let’s compare these two methods below and explain why @import is not recommended.

  • link belongs to HTML, through the href attribute in the <link><span style="font-size: 16px;"></span> tag to introduce external files, and @import belongs to CSS, so the import statement should be written in CSS. It should be noted that the import statement should be written at the beginning of the style sheet, otherwise the external file cannot be imported correctly;

  • @import is a concept that only appeared in CSS2.1, so if the browser version is lower, external style files cannot be imported correctly;

  • When the HTML file is loaded, the file referenced by link will be loaded at the same time, while the file referenced by @import will wait until the page is completely downloaded before being loaded;


Summary: We should try to use the <link><span style="font-size: 16px;"></span> tag to import external CSS files to avoid or reduce Use the other three methods.



Reference:

  1. ##http://www.stevesouders.com/blog /2009/04/09/dont-use-import<p><span style="font-size: 16px;"></span>

  2. ##http://matthewjamestaylor.com/blog/adding-css-to-html-with- link-embed-inline-and-import
  3. <p><span style="font-size: 16px;"></span>

How to introduce CSS into HTML

There are 4 ways to introduce CSS in HTML. Two of them are to add CSS code directly in the HTML file, and the other two are to introduce external CSS files. Let’s take a look at these methods and their advantages and disadvantages.

Inline method

The inline method refers to the <span style="font-size: 16px;">#style# directly in the HTML tag </span>## Add CSS to the attribute.

Example:

<span style="font-size: 16px;"><p style="background: red"></p><br></span>
Copy after login
Copy after login

This is usually a bad way of writing, it can only change the style of the current label, if you want multiple <p><span style="font-size: 16px;"></span> have the same style, you have to repeat it for each <p><span style="font-size: 16px;"></span> Add the same style. If you want to modify a style, you have to modify the code in all styles. Obviously, introducing CSS code inline will cause the HTML code to become verbose and make the web page difficult to maintain.

Embedding method

The embedding method refers to the <style> in the HTML header. <span style="font-size: 16px;"></span> Write CSS code under the tag.

Example:

<span style="font-size: 16px;"><head><br>    <style><br><br>    .content {<br>        background: red;<br>    }<br><br>    </style><br></head><br></span>
Copy after login
Copy after login

Embedded CSS is only valid for the current web page. Because the CSS code is in the HTML file, it makes the code more concentrated, which is usually advantageous when we write template web pages. Because someone looking at the template code can see the HTML structure and CSS styles at a glance. Because embedded CSS is only valid for the current page, when multiple pages need to introduce the same CSS code, writing this way will lead to code redundancy and is not conducive to maintenance.

Link method

The link method refers to using the HTML header<head><span style="font-size: 16px;"></span> tag introduces external CSS files.

Example:

<span style="font-size: 16px;"><head><br>    <link rel="stylesheet" type="text/css" href="style.css"><br></head><br></span>
Copy after login
Copy after login

This is the most common and recommended way to introduce CSS. Using this approach, all CSS code only exists in a single CSS file, so it has good maintainability. And all CSS code only exists in the CSS file. The CSS file will be introduced when it is loaded for the first time. When switching pages in the future, you only need to load the HTML file.

Import method

Import method refers to using CSS rules to introduce external CSS files.

Example:

<span style="font-size: 16px;"><style><br>    @import url(style.css);<br></style><br></span>
Copy after login
Copy after login

Compare link method and import method

Link method (used below link instead) and import method (replaced by @import below) are both ways to introduce external CSS files. Let’s compare these two methods below and explain why @import is not recommended.

  • link belongs to HTML, through the href attribute in the <link><span style="font-size: 16px;"></span> tag to introduce external files, and @import belongs to CSS, so the import statement should be written in CSS. It should be noted that the import statement should be written at the beginning of the style sheet, otherwise the external file cannot be imported correctly;

  • @import is a concept that only appeared in CSS2.1, so if the browser version is lower, external style files cannot be imported correctly;

  • When the HTML file is loaded, the file referenced by link will be loaded at the same time, while the file referenced by @import will wait until the page is completely downloaded before being loaded;


Summary: We should try to use the <link><span style="font-size: 16px;"></span> tag to import external CSS files to avoid or reduce Use the other three methods.

Related recommendations:

Four ways to introduce CSS

[CSS Basics] Priority, introduction method, Hack_html/css_WEB-ITnose

4 types of CSS introduction methods and priority_html/css_WEB-ITnose

The above is the detailed content of CSS introduction method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template