CSS 规则是 CSS 的基本构建单元,理解 CSS 规则的结构是学习 CSS 的第一步。每个 CSS 规则都由两个主要部分组成:选择器和声明块。
一个完整的 CSS 规则包含以下要素:
选择器 {
属性1: 值1;
属性2: 值2;
属性3: 值3;
}
选择器用于指定要应用样式的 HTML 元素。选择器告诉浏览器:"找到这些元素,然后把样式应用上去"。
h1 {
color: blue;
}
在这个例子中,h1 就是选择器,它选中所有的 <h1> 标签元素。
声明块包含在一对大括号 {} 中,包含一个或多个声明。每个声明定义了一个或多个样式属性及其对应的值。
p {
color: #333;
font-size: 16px;
line-height: 1.6;
}
每个声明由一个属性和一个值组成,用冒号 : 分隔,以分号 ; 结束。
属性: 值;
让我们通过一个具体的例子来深入理解 CSS 规则的结构:
.article-title {
color: #2c3e50;
font-size: 24px;
font-weight: bold;
margin-bottom: 15px;
text-align: center;
}
选择器:.article-title
class="article-title" 的元素声明块:大括号 {} 中的内容
声明 1:color: #2c3e50;
color(文本颜色)#2c3e50(深灰色)声明 2:font-size: 24px;
font-size(字体大小)24px(24 像素)声明 3:font-weight: bold;
font-weight(字体粗细)bold(粗体)声明 4:margin-bottom: 15px;
margin-bottom(底部外边距)15px(15 像素)声明 5:text-align: center;
text-align(文本对齐)center(居中)虽然 CSS 对空格和换行不敏感,但良好的代码格式能提高可读性:
推荐的格式:
/* 良好的格式 */
.card {
background: white;
border: 1px solid #ddd;
padding: 20px;
border-radius: 8px;
}
不推荐的格式:
/* 难以阅读 */
.card{background:white;border:1px solid #ddd;padding:20px;border-radius:8px;}
每个声明必须以分号 ; 结束,最后一个声明的分号可以省略,但建议保留:
/* 推荐的写法 */
.button {
padding: 10px 20px;
background: #007bff;
color: white;
border: none;
}
在 CSS 中添加注释可以提高代码的可维护性:
/* 主要导航样式 */
.main-nav {
background: #333;
padding: 15px 0;
}
/* 导航链接样式 */
.main-nav a {
color: white;
text-decoration: none;
margin-right: 20px;
}
/* 选中所有段落 */
p {
font-size: 16px;
line-height: 1.6;
color: #333;
}
/* 选中所有标题 */
h1, h2, h3 {
color: #2c3e50;
margin-bottom: 15px;
}
/* 选中所有具有 highlight 类的元素 */
.highlight {
background-color: #fff3cd;
padding: 10px;
border-left: 4px solid #ffc107;
}
/* 选中所有具有 btn 类的元素 */
.btn {
display: inline-block;
padding: 10px 20px;
background: #007bff;
color: white;
text-decoration: none;
border-radius: 4px;
}
/* 选中 ID 为 header 的元素 */
#header {
background: #fff;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
padding: 20px 0;
}
/* 选中 ID 为 main-content 的元素 */
#main-content {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
/* 选中所有 div 元素内的段落 */
div p {
margin-bottom: 15px;
}
/* 选中所有直接作为 nav 子元素的链接 */
nav > a {
font-weight: bold;
text-transform: uppercase;
}
CSS 属性名使用小写字母,多个单词用连字符 - 连接:
/* 正确的属性命名 */
background-color
font-size
border-top-left-radius
/* 错误的属性命名 */
backgroundColor /* 应该使用连字符 */
FontSize /* 应该使用小写 */
CSS 属性值有多种类型:
display: block;
font-weight: bold;
text-align: center;
width: 100px;
height: 2em;
margin: 1.5rem;
color: red;
background: #ffffff;
border-color: rgb(0, 0, 0);
background-image: url('background.jpg');
list-style-image: url('bullet.png');
margin: 10px 20px; /* 上下10px,左右20px */
padding: 5px 10px 15px 20px; /* 上5px,右10px,下15px,左20px */
border: 1px solid #ddd; /* 宽度、样式、颜色 */
当多个 CSS 规则应用于同一个元素时,浏览器需要决定使用哪个规则。这涉及到 CSS 的优先级机制:
/* 优先级:0,0,1 */
p {
color: blue;
}
/* 优先级:0,1,0 */
.text {
color: green;
}
/* 优先级:1,0,0 */
#content {
color: red;
}
/* 优先级:最高 */
.highlight {
color: yellow !important;
}
/* 全局样式 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
line-height: 1.6;
color: #333;
background: #f5f5f5;
}
/* 容器样式 */
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 20px;
}
/* 标题样式 */
h1 {
font-size: 32px;
color: #2c3e50;
margin-bottom: 20px;
text-align: center;
}
h2 {
font-size: 24px;
color: #34495e;
margin-bottom: 15px;
}
/* 按钮样式 */
.btn {
display: inline-block;
padding: 12px 24px;
background: #3498db;
color: white;
text-decoration: none;
border-radius: 4px;
border: none;
cursor: pointer;
transition: background 0.3s ease;
}
.btn:hover {
background: #2980b9;
}
/* 卡片样式 */
.card {
background: white;
border: 1px solid #ddd;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* 错误 */
.button {
background: #007bff
color: white
}
/* 正确 */
.button {
background: #007bff;
color: white;
}
/* 错误 */
.text {
backgroud-color: white; /* 拼写错误 */
}
/* 正确 */
.text {
background-color: white;
}
/* 错误 - 类选择器需要加点 */
title {
color: #333;
}
/* 正确 */
.title {
color: #333;
}
/* 错误 */
.border {
border: 1px solid; /* 缺少颜色值 */
}
/* 正确 */
.border {
border: 1px solid #ddd;
}
CSS 规则结构是 CSS 的基础,理解它对于编写有效的 CSS 至关重要。记住以下几点:
掌握了 CSS 规则结构,你就可以开始编写自己的 CSS 样式了。在接下来的章节中,我们将学习各种选择器的使用方法,以及常用的 CSS 属性。