CSS 提供了多种引入方式,让我们可以在 HTML 文件中应用样式。不同的引入方式有不同的特点和适用场景,选择合适的引入方式对于项目的可维护性和性能都至关重要。
CSS 主要有三种引入方式:
<style> 标签中定义样式<link> 标签引入内联样式是将 CSS 样式直接写在 HTML 元素的 style 属性中。
<element style="property: value; property: value;">
内容
</element>
<!-- 单个样式 -->
<p style="color: blue;">这段文字是蓝色的</p>
<!-- 多个样式 -->
<div style="background: #f5f5f5; padding: 20px; border-radius: 8px;">
<h1 style="color: #333; margin-bottom: 10px;">标题</h1>
<p style="line-height: 1.6; color: #666;">内容</p>
</div>
<!-- 链接样式 -->
<a href="#" style="color: #3498db; text-decoration: none;">链接</a>
<!-- 按钮样式 -->
<button style="padding: 10px 20px; background: #007bff; color: white; border: none; border-radius: 4px;">
点击我
</button>
优点:
缺点:
<!-- 1. 动态样式 -->
<div id="dynamic-element" style="background: red;">
动态样式元素
</div>
<script>
// JavaScript 动态修改样式
document.getElementById('dynamic-element').style.background = 'blue';
</script>
<!-- 2. 临时调试 -->
<p style="border: 2px solid red;">调试这个元素</p>
<!-- 3. 特殊情况覆盖 -->
<div class="card" style="margin-top: 20px;">
特殊卡片
</div>
<!-- 4. 邮件模板(某些邮件客户端只支持内联样式) -->
<table style="width: 100%; border-collapse: collapse;">
<tr>
<td style="padding: 20px; background: #f5f5f5;">
内容
</td>
</tr>
</table>
内部样式表是将 CSS 样式写在 HTML 文件的 <style> 标签中,通常放在 <head> 部分。
<head>
<style>
selector {
property: value;
}
</style>
</head>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>内部样式表示例</title>
<style>
/* 全局样式 */
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
background: #f5f5f5;
}
/* 容器样式 */
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
/* 标题样式 */
h1 {
color: #2c3e50;
margin-bottom: 20px;
}
/* 按钮样式 */
.button {
padding: 10px 20px;
background: #007bff;
color: white;
text-decoration: none;
border-radius: 4px;
border: none;
cursor: pointer;
}
.button:hover {
background: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h1>欢迎</h1>
<p>这是一个使用内部样式表的示例。</p>
<button class="button">点击我</button>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>多个 style 标签</title>
<!-- 全局样式 -->
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
</style>
<!-- 布局样式 -->
<style>
.header {
background: #333;
color: white;
padding: 20px;
}
.main {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
</style>
<!-- 组件样式 -->
<style>
.card {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<div class="header">
<h1>网站标题</h1>
</div>
<div class="main">
<div class="card">
<h2>卡片标题</h2>
<p>卡片内容</p>
</div>
</div>
</body>
</html>
优点:
缺点:
<!-- 1. 单页应用 -->
<!DOCTYPE html>
<html>
<head>
<style>
/* 完整的样式表 */
</style>
</head>
<body>
<!-- 单页应用内容 -->
</body>
</html>
<!-- 2. 小型网站 -->
<!DOCTYPE html>
<html>
<head>
<style>
/* 简单的样式表 */
body { font-family: Arial; }
h1 { color: blue; }
</style>
</head>
<body>
<!-- 小型网站内容 -->
</body>
</html>
<!-- 3. 原型设计 -->
<!DOCTYPE html>
<html>
<head>
<style>
/* 快速原型样式 */
</style>
</head>
<body>
<!-- 原型内容 -->
</body>
</html>
<!-- 4. 独立组件 -->
<div style="display: none;">
<template id="my-component">
<style>
/* 组件样式 */
.component {
background: white;
padding: 20px;
}
</style>
<div class="component">
组件内容
</div>
</template>
</div>
外部样式表是将 CSS 样式写在独立的 .css 文件中,通过 <link> 标签引入到 HTML 文件中。
<head>
<link rel="stylesheet" href="styles.css">
</head>
styles.css
/* 全局样式 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Microsoft YaHei', Arial, sans-serif;
line-height: 1.6;
color: #333;
background: #f5f5f5;
}
/* 容器样式 */
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 20px;
}
/* 标题样式 */
h1, h2, h3, h4, h5, h6 {
color: #2c3e50;
margin-bottom: 15px;
}
h1 {
font-size: 32px;
}
h2 {
font-size: 24px;
}
/* 按钮样式 */
.button {
display: inline-block;
padding: 12px 24px;
background: #007bff;
color: white;
text-decoration: none;
border-radius: 4px;
border: none;
cursor: pointer;
transition: background 0.3s ease;
}
.button:hover {
background: #0056b3;
}
.button-primary {
background: #007bff;
}
.button-success {
background: #28a745;
}
.button-danger {
background: #dc3545;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>外部样式表示例</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>欢迎</h1>
<p>这是一个使用外部样式表的示例。</p>
<button class="button button-primary">主要按钮</button>
<button class="button button-success">成功按钮</button>
<button class="button button-danger">危险按钮</button>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>多个外部样式表</title>
<!-- 重置样式 -->
<link rel="stylesheet" href="reset.css">
<!-- 基础样式 -->
<link rel="stylesheet" href="base.css">
<!-- 组件样式 -->
<link rel="stylesheet" href="components.css">
<!-- 页面样式 -->
<link rel="stylesheet" href="page.css">
<!-- 响应式样式 -->
<link rel="stylesheet" href="responsive.css">
</head>
<body>
<!-- 页面内容 -->
</body>
</html>
优点:
缺点:
<!-- 1. 大型网站 -->
<link rel="stylesheet" href="styles/main.css">
<link rel="stylesheet" href="styles/components.css">
<link rel="stylesheet" href="styles/pages.css">
<!-- 2. 多页面网站 -->
<!-- index.html -->
<link rel="stylesheet" href="styles/common.css">
<link rel="stylesheet" href="styles/home.css">
<!-- about.html -->
<link rel="stylesheet" href="styles/common.css">
<link rel="stylesheet" href="styles/about.css">
<!-- 3. 使用 CSS 框架 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
<!-- 4. 第三方样式库 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
| 特性 | 内联样式 | 内部样式表 | 外部样式表 |
|---|---|---|---|
| 内容与表现分离 | ❌ | ❌ | ✅ |
| 样式复用 | ❌ | ✅ | ✅ |
| 跨页面共享 | ❌ | ❌ | ✅ |
| 维护性 | ❌ | ⚠️ | ✅ |
| 缓存支持 | ❌ | ❌ | ✅ |
| 优先级 | 最高 | 中等 | 最低 |
| 适用规模 | 小型 | 中型 | 大型 |
<!-- 内联样式 - 性能最差 -->
<div style="color: blue; font-size: 16px; padding: 10px;">内容</div>
<!-- 内部样式表 - 性能中等 -->
<style>
.text { color: blue; font-size: 16px; padding: 10px; }
</style>
<!-- 外部样式表 - 性能最好(考虑缓存) -->
<link rel="stylesheet" href="styles.css">
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="external.css">
<style>
/* 内部样式表 */
.text {
color: green;
}
</style>
</head>
<body>
<!-- 内联样式 -->
<div class="text" style="color: red;">这段文字是什么颜色?</div>
</body>
</html>
/* external.css */
.text {
color: blue;
}
/* 结果:红色 - 内联样式优先级最高 */
<!-- 推荐 -->
<link rel="stylesheet" href="styles.css">
<!-- 不推荐 - 除非特殊情况 -->
<div style="color: blue;">内容</div>
<!-- 按功能组织 -->
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/base.css">
<link rel="stylesheet" href="css/components.css">
<link rel="stylesheet" href="css/pages.css">
<link rel="stylesheet" href="css/utilities.css">
<!-- 按需加载样式 -->
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="print.css" media="print">
<link rel="stylesheet" href="mobile.css" media="screen and (max-width: 768px)">
<!-- 不推荐 -->
<div style="background: white; padding: 20px; border-radius: 8px;">
内容
</div>
<!-- 推荐 -->
<div class="card">
内容
</div>
/* styles.css */
.card {
background: white;
padding: 20px;
border-radius: 8px;
}
CSS 提供了三种引入方式,各有其特点和适用场景:
记住以下几点: