How CSS is introduced

Source: Internet
Author: User
Tags html header

How CSS is introduced in HTML

There are 4 ways to introduce CSS in HTML. There are 2 ways to add CSS code directly to the HTML file, and the other two are the introduction of external CSS files. Let's take a look at these ways and their pros and cons.

Inline mode

inline means directly in the HTML tag. style add CSS to the properties.

Example:

<p style= "background:red" ></p>

This is usually a bad way to write, it can only change the style of the current label, if you want multiple <p> having the same style, you have to repeat for each <p> Add the same style, and if you want to modify a style, you have to modify all the code in the style. Obviously, inline CSS code can lead to lengthy HTML code and make the Web page difficult to maintain.

Embedding method

The embedding method refers to the HTML header. <style> tag to write the CSS code.

Example:

    <style>

. Content {
background:red;
}

</style>

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

How to link

The link method refers to the use of the HTML header tags into the external CSS file.

Example:

    <link rel= "stylesheet" type= "Text/css" href= "Style.css" >

This is the most common and recommended way to introduce CSS. In this way, all CSS code exists only in a separate CSS file, so it has good maintainability. And all the CSS code exists only in the CSS file, the CSS file will be introduced at the first load, and later to switch the page only need to load the HTML file.

How to Import

Importing means using CSS rules to introduce external CSS files.

Example:

<style>
@import URL (style.css);
</style>

Compare links and how to import

The way you link (with link instead) and the way you import (with @import instead) is the way to introduce an external CSS file, let's compare both ways, and explain why @import is not recommended.

    • link belongs to HTML, through <link> the href attribute in the tag to introduce the external file, and @import belong to CSS, so the import statement should be written in the CSS, note that the import statement should be written at the beginning of the style sheet, otherwise it will not be imported correctly external files;

    • @import is the concept that CSS2.1 appears, so if the browser version is low, the external style file cannot be imported correctly;

    • When the HTML file is loaded, the file that the link refers to is loaded at the same time, and the file @import referenced will be loaded after all the pages have been downloaded;

Summary : We should try to use <link> tags to import external CSS files, avoid or use the other three different ways.

Resources:

    1. Http://www.stevesouders.com/blog/2009/04/09/dont-use-import

    2. Http://matthewjamestaylor.com/blog/adding-css-to-html-with-link-embed-inline-and-import

How CSS is introduced in HTML

There are 4 ways to introduce CSS in HTML. There are 2 ways to add CSS code directly to the HTML file, and the other two are the introduction of external CSS files. Let's take a look at these ways and their pros and cons.

Inline mode

inline means directly in the HTML tag. style add CSS to the properties.

Example:

<p style= "background:red" ></p>

This is usually a bad way to write, it can only change the style of the current label, if you want multiple <p> having the same style, you have to repeat for each <p> Add the same style, and if you want to modify a style, you have to modify all the code in the style. Obviously, inline CSS code can lead to lengthy HTML code and make the Web page difficult to maintain.

Embedding method

The embedding method refers to the HTML header. <style> tag to write the CSS code.

Example:

    <style>

. Content {
background:red;
}

</style>

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

How to link

The link method refers to the use of the HTML header tags into the external CSS file.

Example:

    <link rel= "stylesheet" type= "Text/css" href= "Style.css" >

This is the most common and recommended way to introduce CSS. In this way, all CSS code exists only in a separate CSS file, so it has good maintainability. And all the CSS code exists only in the CSS file, the CSS file will be introduced at the first load, and later to switch the page only need to load the HTML file.

How to Import

Importing means using CSS rules to introduce external CSS files.

Example:

<style>
@import URL (style.css);
</style>

Compare links and how to import

The way you link (with link instead) and the way you import (with @import instead) is the way to introduce an external CSS file, let's compare both ways, and explain why @import is not recommended.

    • link belongs to HTML, through <link> the href attribute in the tag to introduce the external file, and @import belong to CSS, so the import statement should be written in the CSS, note that the import statement should be written at the beginning of the style sheet, otherwise it will not be imported correctly external files;

    • @import is the concept that CSS2.1 appears, so if the browser version is low, the external style file cannot be imported correctly;

    • When the HTML file is loaded, the file that the link refers to is loaded at the same time, and the file @import referenced will be loaded after all the pages have been downloaded;

Summary : We should try to use <link> tags to import external CSS files, avoid or use the other three different ways.

Related recommendations:

Four ways to introduce CSS

"CSS Basics" priority, Introduction mode, Hack_html/css_web-itnose

4 ways to introduce CSS and priority _html/css_web-itnose

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.