Home Web Front-end Front-end Q&A Call different css

Call different css

May 21, 2023 pm 12:01 PM

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

  1. Calling through the link tag

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>
Copy after login

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.

  1. Calling via @import

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");
Copy after login

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.

  1. Using the id selector

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;
}
Copy after login

In the style2.css file, we can define the following CSS style:

#box {
  width: 100px;
  height: 100px;
  background-color: blue;
}
Copy after login

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>
Copy after login

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.

  1. Using the class selector

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;
}
Copy after login

In the style2.css file, we can define the following CSS style:

.blue-box {
  width: 100px;
  height: 100px;
  background-color: blue;
}
Copy after login

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>
Copy after login

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!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

React and the Frontend: Building Interactive Experiences React and the Frontend: Building Interactive Experiences Apr 11, 2025 am 12:02 AM

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

React Components: Creating Reusable Elements in HTML React Components: Creating Reusable Elements in HTML Apr 08, 2025 pm 05:53 PM

React components can be defined by functions or classes, encapsulating UI logic and accepting input data through props. 1) Define components: Use functions or classes to return React elements. 2) Rendering component: React calls render method or executes function component. 3) Multiplexing components: pass data through props to build a complex UI. The lifecycle approach of components allows logic to be executed at different stages, improving development efficiency and code maintainability.

What are the benefits of using TypeScript with React? What are the benefits of using TypeScript with React? Mar 27, 2025 pm 05:43 PM

TypeScript enhances React development by providing type safety, improving code quality, and offering better IDE support, thus reducing errors and improving maintainability.

How can you use useReducer for complex state management? How can you use useReducer for complex state management? Mar 26, 2025 pm 06:29 PM

The article explains using useReducer for complex state management in React, detailing its benefits over useState and how to integrate it with useEffect for side effects.

React and the Frontend Stack: The Tools and Technologies React and the Frontend Stack: The Tools and Technologies Apr 10, 2025 am 09:34 AM

React is a JavaScript library for building user interfaces, with its core components and state management. 1) Simplify UI development through componentization and state management. 2) The working principle includes reconciliation and rendering, and optimization can be implemented through React.memo and useMemo. 3) The basic usage is to create and render components, and the advanced usage includes using Hooks and ContextAPI. 4) Common errors such as improper status update, you can use ReactDevTools to debug. 5) Performance optimization includes using React.memo, virtualization lists and CodeSplitting, and keeping code readable and maintainable is best practice.

React's Ecosystem: Libraries, Tools, and Best Practices React's Ecosystem: Libraries, Tools, and Best Practices Apr 18, 2025 am 12:23 AM

The React ecosystem includes state management libraries (such as Redux), routing libraries (such as ReactRouter), UI component libraries (such as Material-UI), testing tools (such as Jest), and building tools (such as Webpack). These tools work together to help developers develop and maintain applications efficiently, improve code quality and development efficiency.

React vs. Backend Frameworks: A Comparison React vs. Backend Frameworks: A Comparison Apr 13, 2025 am 12:06 AM

React is a front-end framework for building user interfaces; a back-end framework is used to build server-side applications. React provides componentized and efficient UI updates, and the backend framework provides a complete backend service solution. When choosing a technology stack, project requirements, team skills, and scalability should be considered.

See all articles