Home > Article > Web Front-end > CSS introduction method
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.
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>
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.
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>
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.
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>
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 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>
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:
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.
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>
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.
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>
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.
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>
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 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>
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:
[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!