As an important part of web development, HTML is not only the content carrier of the page, but also the definer of structure and style. As a language for operating the appearance and style of the page, CSS can make the content of the web page more colorful and beautiful.
In actual development, we often need to use different styles for different pages or different parts. At this time, we need to call different CSS files. This article will introduce how to call different CSS files and how to use them in the page to diversify styles.
1. How to call different CSS files
You can call external CSS files in the HTML header through the link tag. The link tag must contain three attributes: href, rel, and type.
The specific format is as follows:
Among them, href is used For specifying the path of the called CSS file, rel is used to define the relationship between the document and the linked document, usually set to stylesheet. type specifies the MIME type of the linked document, usually set to text/css.
For example, we can use the following code to call two different CSS files in the HTML header:
<!DOCTYPE html> <html> <head> <link href="style1.css" rel="stylesheet" type="text/css" /> <link href="style2.css" rel="stylesheet" type="text/css" /> <title>调用不同CSS文件示例</title> </head> <body> <!-- 页面内容 --> </body> </html>
At this time, we can use the style1.css file and the style2.css file Define different styles in and call them in different pages to achieve style diversification.
In addition to using the link tag, we can also use the @import method to call CSS files. In a CSS file, you can use the @import statement to call another CSS file, and the styles in the called file will be merged with the styles in the main file.
The specific syntax is as follows:
@import url("file path");
For example, in the main style sheet style.css, we can use the following code Load the sub-style sheet style2.css:
@import url("style2.css");
In this way, when style.css is called in the HTML page, the style of style2.css will be automatically merged in, thereby achieving style diversification.
2. Apply different CSS styles
After calling multiple CSS files, we can use different styles in some specific ways. Two commonly used methods are introduced below.
In an HTML page, we can set the id attribute for an element to allow us to set special CSS styles for it.
For example, in the style1.css file, we can define the following CSS style:
#box { width: 200px; height: 200px; background-color: red; }
In the style2.css file, we can define the following CSS style:
#box { width: 100px; height: 100px; background-color: blue; }
Then, in the HTML page, we can use the following code to call these two styles:
<!DOCTYPE html> <html> <head> <link href="style1.css" rel="stylesheet" type="text/css" /> <link href="style2.css" rel="stylesheet" type="text/css" /> <title>调用不同CSS文件示例</title> </head> <body> <div id="box">样式设置示例</div> </body> </html>
At this time, when we open the page, we will find that the background color of the box element is red, not blue color. This is because in HTML, the id selector has higher priority than the label selector, so the box element uses the style defined in style1.css.
Similar to the id selector, we can also use the class selector to set styles for elements. When using the class selector, you need to add a "." sign in front of the selector when defining the style in the CSS file.
For example, in the style1.css file, we can define the following CSS style:
.red-box { width: 200px; height: 200px; background-color: red; }
In the style2.css file, we can define the following CSS style:
.blue-box { width: 100px; height: 100px; background-color: blue; }
Then, in the HTML page, we can use the following code to call these two styles:
<!DOCTYPE html> <html> <head> <link href="style1.css" rel="stylesheet" type="text/css" /> <link href="style2.css" rel="stylesheet" type="text/css" /> <title>调用不同CSS文件示例</title> </head> <body> <div class="red-box">样式设置示例</div> <div class="blue-box">样式设置示例</div> </body> </html>
At this time, when we open the page, we will find that the background color of the first box element is red. The background color of the second box element is blue. This is because in HTML, the class selector has a lower priority than the id selector, but higher than the label selector, so the first box element uses the style defined in style1.css, while the second box element uses Styles defined in style2.css.
3. Summary
In web development, style settings can make the page more beautiful and easier to identify. By calling different CSS files, we can use different styles in different pages to achieve diverse effects. After calling multiple CSS files, we can use id selectors or class selectors to use different styles to meet the style needs of different elements. Therefore, the reasonable calling and use of CSS files is one of the important knowledge points in web development.
The above is the detailed content of Call different css. For more information, please follow other related articles on the PHP Chinese website!