Home > Article > Web Front-end > How to put css in html file
Methods to put css in html files: 1. Add CSS directly to the style attribute in the HTML tag; 2. Write CSS under the style tag in the HTML header; 3. Use the head tag to introduce external CSS files; 4. Use CSS rules to introduce external CSS files.
The operating environment of this article: Windows7 system, HTML5&&CSS3 version, DELL G3 computer
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 adding CSS directly to the style attribute in the HTML tag.
Example:
<div style="background: red"></div>
This is usually a bad way of writing, it can only change the style of the current tag. If you want multiple dc6dce4a544fdca2df29d5ac0ea9906b to have the same style, you have to Repeatedly add the same style to each dc6dce4a544fdca2df29d5ac0ea9906b. 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 writing CSS code under the c9ccee2e6ea535a969eb3f532ad9fe89 tag in the HTML header.
Example:
<head> <style> .content { background: red; } </style> </head>
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 93f0f5c25f18dab9d176bd4f6de5d30e tag in the HTML header to introduce external CSS files.
Example:
<head> <link rel="stylesheet" type="text/css" href="style.css"> </head>
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:
<style> @import url(style.css); </style>
Compare the link method and the import method
The link method (replaced by link below) and the import method (replaced by @import below) both introduce external CSS file method, let’s compare these two methods and explain why @import is not recommended.
link belongs to HTML, and external files are introduced through the href attribute in the 2cdf5bf648cf2f33323966d7f58a7f3f tag, while @import belongs to CSS, so the import statement should be written in CSS. It should be noted that the import statement should be written in the style at the beginning of the table, otherwise external files 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 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 not be loaded until the page is completely downloaded;
Summary: We should try to use the 2cdf5bf648cf2f33323966d7f58a7f3f tag to import For external CSS files, avoid or use less of the other three methods.
【Recommended learning: css video tutorial】
The above is the detailed content of How to put css in html file. For more information, please follow other related articles on the PHP Chinese website!