"; 2. Use the "@import" keyword Call, the syntax is ""."/> "; 2. Use the "@import" keyword Call, the syntax is "".">

Home  >  Article  >  Web Front-end  >  What is the method to call external styles in html page

What is the method to call external styles in html page

青灯夜游
青灯夜游Original
2022-09-15 18:17:246329browse

Two calling methods: 1. Use the link tag to call, the syntax is ""; 2 , use the "@import" keyword to call, the syntax is "".

What is the method to call external styles in html page

The operating environment of this tutorial: Windows 7 system, HTML5 version, Dell G3 computer.

CSS External Style Sheet

If the CSS style is placed in a file outside the web document, it is called an external style sheet, a CSS A style sheet document represents an external style sheet.

What is the method to call external styles in html page

In fact, the external style sheet is a text file with the extension .css. When you copy the CSS style code into a text file and save it as a .css file, it is an external style sheet.

Two methods of calling external style sheets in html pages

External style sheets must be imported into the web page document before they can be used by the browser Identify and parse. External style sheet files can be imported into HTML documents in two ways.

1. Use the tag to call (link style)

Use the tag to call the external style sheet file:

<link href="外部样式表文件路径" rel="stylesheet" type="text/css" />

Description of each attribute:

  • #href attribute sets the address of the external style sheet file, which can be a relative address or an absolute address.

  • The rel attribute defines the associated document, which here indicates that the associated document is a style sheet.

  • The type attribute defines the type of imported file. Like the style element, text/css indicates a CSS text file.

Generally when defining the tag, three basic attributes should be defined, among which href is a must-set attribute.

Linking external style sheet files to HTML documents through HTML's tag is the most commonly used method for websites on the Internet, and it is also the most practical method. This method completely separates HTML documents and CSS files, achieves a complete separation of the structure layer and the presentation layer, and enhances the scalability of the web page structure and the maintainability of CSS styles.

Example: Use link style to apply styles to HTML code, which is easy to write and change.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="lianjie.css" type="text/css" rel="stylesheet" />
<link href="lianjie-2.css" type="text/css" rel="stylesheet" />
</head>
<body>
    <p>我是被 lianjie-2.css 文件控制的,楼下的你呢??</p>
    <h3>楼上的,<span>lianjie.css</span> 文件给我穿的花衣服。</h3>
</body>
</html>

What is the method to call external styles in html page

In the above example, two CSS files are linked through link, and both are valid. This is why the website maker puts the public part into one CSS file, and the current page style Write a new style file.

lianjie.css File code:

h3{
    font-weight: normal;  /*取消标题默认加粗效果*/
    background-color: #66CC99;  /* 设置背景色 */
    height: 50px;  /*设置标签的高度*/
    line-height:50px;  /* 设置标签的行高 */
}
span{
    color: #FFOOOO;  /* 字体颜色 */
    font-weight:bold;  /* 字体加粗 */
}

lianjie-2.css File code:

p{
    color: #FF3333;  /*字体颜色设置*/
    font-weight: bold;  /* 字体加粗 */
    border-bottom: 3px dashed #009933;  /* 设置下边框线 */
    line-height: 30px;  /* 设置行高 */
}

Linked style completely separates CSS code and HTML code to achieve structure and style The separation allows the HTML code to specifically build the page structure, while the beautification work is completed by CSS.

Benefits of link-based import of CSS styles:

  • CSS files can be placed in different HTML files to make all pages of the website unified in style;

  • Furthermore, putting the CSS code into a CSS file facilitates management and reduces code and maintenance time;

  • When the CSS file is modified, all applications that apply this CSS file The HTML files will all be updated without having to retrieve all the pages from the server and then upload them after modifications.

2. Use the @import keyword to call (import)

The @import directive is part of the CSS language. Use this directive when using it. Added to a

To associate with an external CSS file, use url instead of href, and put the path in parentheses;

<style type="text/css">
@import url("外部样式表文件路径");
</style>

After the @import keyword, use the url() function to include the address of the specific external style sheet file.

Example: Import the style sheets lianjie.css and daoru.css and write the background color of the

tag. Note that the imported style sheet and tag styles cannot be reversed.
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
    @import url(lianjie.css);
    @import url(daoru.css);
    body { background-color: #e4e929; }
</style>
</head>
<body>
    <div>
        <p>我是被 lianjie-2.css 文件控制的,楼下的你呢??</p>
        <h3>褛上的,<span>lianjie.css</span>文件给我穿的花衣服。</h3>
    </div>
</body>
</html>

What is the method to call external styles in html page

In the above example, it must be @import url("lianjie-2.css"); p{text-indent: 3em;}, not p{text-indent:3em;} @import url("lianjie-2.css");, otherwise the import effect will be invalid. In the CSS file, @import also needs to be placed at the front and the CSS style is added at the end, otherwise it will be invalid.

lianjie.css file code, the same as the previous example, that is, link type.

daoru.css file code:

@import url("lianjie-2.css");
p { text-indent: 3em; }

The difference between the two methods (link and @import)

tag belongs to html tag , and @import is a method provided by css. The tag can not only introduce css, but also do other things, while @import can only introduce css;

The difference in loading order: when a page is browsed, the css introduced by link will be loaded synchronously, while the css referenced by @import will not be loaded until all other elements have been downloaded;

Compatible Differences in nature: @import was only proposed in CSS2.1, so it is only supported by IE5 and above, and is not supported by lower version browsers. The tag does not have this problem;

When controlled using javascript When the DOM changes the style, you can only use the tag, because @import is not controllable by the DOM.

(Learning video sharing: Getting started with web front-end)

The above is the detailed content of What is the method to call external styles in html page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:What is html and xhtmlNext article:What is html and xhtml