01三种引入方式与基本语法
CSS 由「选择器 + 声明块」组成。声明块内的每一条声明都是 属性: 值; 的形式,最后一条建议也写分号,方便后续追加。
/* 选择器 { 属性: 值; } */
.card {
padding: 24px;
border-radius: 12px;
background: #fffdf8;
border: 1px solid rgba(76, 107, 87, 0.15);
}
/* 多个选择器共享同一组声明 */
h1, h2, h3 {
font-family: "Songti SC", serif;
letter-spacing: 1px;
}
| 引入方式 | 写法 | 适用场景 |
|---|---|---|
| 外部样式表 | <link rel="stylesheet" href="style.css"> | 推荐,可缓存、可复用 |
| 内部样式 | <style> ... </style> | 单页演示、关键首屏样式 |
| 行内样式 | <p style="color: red"> | 仅用于 JS 动态计算的值 |
优先级提示:行内样式 > 内部/外部样式(按书写顺序与权重)。!important 能强制覆盖,但应尽量避免使用,它会让样式难以维护。
02选择器与优先级
选择器负责「选中谁」,优先级负责「谁说了算」。理解权重计算,是解决样式冲突的关键。
常用选择器
/* 标签选择器 */
p { color: #333; }
/* 类选择器:最常用,可复用 */
.card { background: #fff; }
/* ID 选择器:唯一,权重高 */
#header { height: 64px; }
/* 后代选择器:.nav 里面所有 a */
.nav a { color: #4c6b57; }
/* 子代选择器:只要直接子元素 */
.nav > ul { display: flex; }
/* 相邻兄弟:紧挨着的下一个 */
h2 + p { margin-top: 0; }
/* 属性选择器 */
a[target="_blank"] { color: #a3716d; }
input[type="email"] { border-color: #a9c4cf; }
/* 伪类 */
a:hover { text-decoration: underline; }
li:nth-child(odd) { background: #faf7f0; }
input:focus-visible { outline: 2px solid #6f8f7a; }
/* 伪元素 */
blockquote::before { content: "“"; }
::selection { background: #e7efe6; }
/* 现代::has() 让父元素根据子元素变化 */
.card:has(img) { padding-top: 0; }
优先级权重表
| 类型 | 权重 | 示例 |
|---|---|---|
| 行内样式 | 1000 | style="color: red" |
| ID | 100 | #header |
| 类 / 属性 / 伪类 | 10 | .card、[type]、:hover |
| 标签 / 伪元素 | 1 | p、::before |
| 通配 / 继承 | 0 | * |
不要堆叠选择器来「强行提高权重」。例如 .page .content .card .title,不仅难读,还会让后续覆盖变得困难。优先保持选择器扁平。
03盒模型:一切布局的起点
每个元素在页面上都是一个矩形盒子,由内容(content)、内边距(padding)、边框(border)、外边距(margin)四层组成。
content-box 与 border-box
/* 默认:宽度只算内容,padding 和 border 会额外撑大盒子 */
.box-default {
width: 300px;
padding: 20px;
border: 2px solid #ccc;
box-sizing: content-box;
/* 实际占据宽度 = 300 + 40 + 4 = 344px */
}
/* 推荐:宽度包含 padding 与 border,所见即所得 */
.box-border {
width: 300px;
padding: 20px;
border: 2px solid #ccc;
box-sizing: border-box;
/* 实际占据宽度 = 300px */
}
/* 项目里通常全局重置 */
*, *::before, *::after {
box-sizing: border-box;
}
margin 折叠
相邻块级元素的上下 margin 会合并,取其中较大的一个,而不是相加。这正是很多人「间距莫名变小」的原因。
.a { margin-bottom: 30px; }
.b { margin-top: 20px; }
/* 两者间距是 30px(取较大值),不是 50px */
/* 阻止折叠的常见方式 */
.parent {
display: flow-root; /* 推荐:创建 BFC,不影响其他布局 */
}
/* 或者使用 padding / border / overflow: hidden */
04单位、颜色与字体
单位的取舍
| 单位 | 相对于 | 推荐场景 |
|---|---|---|
px | 绝对像素 | 边框、固定尺寸图标 |
rem | 根元素字号 | 字号、间距,便于整体缩放 |
em | 当前元素字号 | 组件内相对间距,注意嵌套放大 |
% | 父元素对应尺寸 | 宽度、高度百分比 |
vw / vh | 视口宽高 | 全屏区块,注意移动端地址栏 |
dvh / svh | 动态视口高度 | 移动端全屏布局更稳 |
ch | 字符 "0" 宽度 | 限制阅读宽度,如 65ch |
/* 颜色的四种写法 */
.a { color: #4c6b57; } /* 十六进制 */
.b { color: rgb(76 107 87); } /* RGB */
.c { color: rgba(76, 107, 87, 0.8); } /* 带透明度 */
.d { color: hsl(140 24% 36% / 0.9); } /* HSL,更适合调色 */
.e { color: oklch(0.55 0.06 150); } /* 现代感知均匀色彩空间 */
/* 字体栈:从左到右匹配,最后兜底 */
body {
font-family: "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
font-size: 16px;
line-height: 1.8; /* 无单位:相对自身字号 */
}
/* 可变字体与字重 */
.title {
font-family: "Noto Serif SC", serif;
font-weight: 600;
letter-spacing: 2px;
}
/* 用 clamp 做流体字号:最小 24px,理想 4vw,最大 40px */
h1 {
font-size: clamp(24px, 4vw, 40px);
}
小技巧:正文宽度控制在 65ch 左右最易读;行高建议 1.6–1.9。这两个数字能立刻提升页面的「高级感」。
05层叠、继承与 BEM 命名
当多条规则同时命中一个元素,浏览器按「来源与重要性 → 优先级 → 书写顺序」决定最终生效的样式,这就是层叠。
继承
- 会继承:颜色、字体、字号、行高、字间距、文字对齐等文本相关属性。
- 不会继承:margin、padding、border、宽高、背景、display、定位。
- 可以用
inherit、initial、unset显式控制。
<!-- BEM:Block__Element--Modifier,让类名自解释 -->
<article class="card card--featured">
<h2 class="card__title">山中笔记</h2>
<p class="card__text">……</p>
<button class="card__btn card__btn--ghost">继续阅读</button>
</article>
<style>
.card { padding: 24px; }
.card__title { font-size: 22px; }
.card__btn { padding: 8px 16px; }
.card__btn--ghost { background: transparent; }
.card--featured { border-color: #c9a86a; }
</style>
06Flexbox:一维布局的瑞士军刀
Flex 适合在一行或一列内排布元素。设置 display: flex 后,子元素成为 flex item,主轴方向由 flex-direction 决定。
.toolbar {
display: flex;
/* 主轴方向:row | row-reverse | column | column-reverse */
flex-direction: row;
/* 主轴对齐:flex-start | center | space-between | space-around | space-evenly */
justify-content: space-between;
/* 交叉轴对齐:flex-start | center | flex-end | stretch | baseline */
align-items: center;
/* 换行:nowrap(默认)| wrap */
flex-wrap: wrap;
/* 行与行之间的间距 */
gap: 12px;
}
/* 子项伸缩规则 */
.toolbar .search {
flex: 1 1 240px;
/* 等价于 flex-grow:1; flex-shrink:1; flex-basis:240px;
剩余空间会分配给搜索框,最小宽度 240px */
}
.toolbar .avatar {
flex: 0 0 40px; /* 不放大、不缩小,固定 40px */
}
/* 单个子项的对齐覆盖 */
.toolbar .badge {
align-self: flex-start;
}
经典场景:垂直居中
/* 方案一:Flex(最通用) */
.center {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
}
/* 方案二:Grid(一行代码) */
.center-grid {
display: grid;
place-items: center;
min-height: 60vh;
}
/* 方案三:绝对定位 + transform(老项目常见) */
.center-abs {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
flex-basis 还是 width?在 flex 布局里优先用 flex-basis 描述初始主轴尺寸,它会被 flex-grow / flex-shrink 参与计算;width 更偏向最终尺寸。
07Grid:二维布局的秩序感
Grid 同时管理行与列,适合整体页面骨架、卡片墙、仪表盘等二维结构。灵活使用 repeat()、minmax()、auto-fit 能写出自适应极强的网格。
.gallery {
display: grid;
/* 三列等宽,间距 20px */
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
/* 跨两列、跨两行 */
.gallery .feature {
grid-column: span 2;
grid-row: span 2;
}
/* 响应式卡片墙:不用媒体查询 */
.auto-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 18px;
}
/* 命名区域:直观描述整页骨架 */
.page {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 240px 1fr;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
.page > header { grid-area: header; }
.page > aside { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }
| 属性 | 含义 |
|---|---|
grid-template-columns | 定义列轨道 |
grid-template-rows | 定义行轨道 |
grid-template-areas | 用名字描述区域布局 |
gap | 行与列间距,替代 margin 拼凑 |
grid-column / grid-row | 子项跨列、跨行 |
justify-items / align-items | 单元格内水平 / 垂直对齐 |
place-items | 前两者的简写 |
选择建议:一维排列用 Flex,二维结构用 Grid。二者可以嵌套:页面骨架用 Grid,导航栏内部用 Flex,这是最常见的组合。
08响应式与容器查询
响应式的核心不是「写很多媒体查询」,而是让布局本身具备弹性:优先用流式布局、弹性单位与自动换行,媒体查询只处理必要的断点。
/* 移动优先:先写小屏,再用 min-width 逐层增强 */
.layout {
display: grid;
grid-template-columns: 1fr;
gap: 20px;
}
@media (min-width: 768px) {
.layout { grid-template-columns: 240px 1fr; }
}
@media (min-width: 1200px) {
.layout { grid-template-columns: 280px 1fr 220px; }
}
/* 图片与视频永不溢出 */
img, video {
max-width: 100%;
height: auto;
}
/* 容器查询:根据「父容器」宽度调整,而不是视口 */
.card-list {
container-type: inline-size;
}
@container (min-width: 480px) {
.card {
display: grid;
grid-template-columns: 160px 1fr;
gap: 16px;
}
}
为什么用容器查询?同一个卡片组件可能出现在窄侧栏,也可能出现在宽主区。媒体查询只能感知视口,而容器查询能感知它真正被放入的容器,组件因此真正「可复用」。
09过渡与关键帧动画
过渡用于「两种状态之间」的平滑变化,关键帧动画则适合更复杂的多阶段运动。二者都要注意性能:优先动画 transform 与 opacity。
/* 1. 过渡:hover 时缓慢浮起 */
.card {
transition: transform 0.35s ease, box-shadow 0.35s ease;
}
.card:hover {
transform: translateY(-6px);
box-shadow: 0 16px 40px rgba(80, 72, 55, 0.12);
}
/* 2. 关键帧动画 */
@keyframes float {
0% { transform: translateY(0) rotate(-2deg); }
50% { transform: translateY(-12px) rotate(0deg); }
100% { transform: translateY(0) rotate(-2deg); }
}
.orb {
animation: float 3.2s ease-in-out infinite;
/* 名称 时长 缓动 循环次数 */
}
/* 3. 入场动画:配合 JS 添加 class */
@keyframes fadeUp {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.reveal {
animation: fadeUp 0.6s cubic-bezier(0.22, 0.61, 0.36, 1) both;
}
/* 4. 尊重用户的「减少动态效果」偏好 */
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
| 概念 | 说明 |
|---|---|
transition | 属性值变化时自动补间,需要触发条件(hover、class) |
@keyframes | 定义 0%–100% 的多阶段状态 |
animation-fill-mode | both 可让动画结束后保持终态 |
will-change | 提前提示浏览器优化,但不要滥用 |
cubic-bezier() | 自定义缓动曲线,塑造「手感」 |
10现代 CSS 特性
近几年 CSS 的能力提升很快,下面这些特性已经可以在主流浏览器中放心使用。
自定义属性(CSS 变量)
:root {
--brand: #4c6b57;
--paper: #faf7f0;
--radius: 14px;
--space: 8px;
}
.button {
background: var(--brand);
border-radius: var(--radius);
padding: calc(var(--space) * 1.5) calc(var(--space) * 3);
}
/* 局部覆盖:只影响这个组件内部 */
.card--pink {
--brand: #a3716d;
}
/* JS 也能读写变量,适合主题切换 */
/* document.documentElement.style.setProperty('--brand', '#888'); */
一组好用的现代属性
/* aspect-ratio:固定宽高比,图片/视频不塌陷 */
.cover {
aspect-ratio: 16 / 9;
object-fit: cover;
width: 100%;
}
/* inline-size / cqw:基于容器宽度 */
.title { font-size: clamp(18px, 4cqw, 28px); }
/* gap 也能用于 flex */
.row { display: flex; gap: 12px; }
/* :is() / :where() 简化选择器,:where 权重为 0 */
:where(ul, ol) { padding-left: 1.2em; }
/* :has() 让父级感知子级 */
.form:has(input:invalid) { border-color: #c98b84; }
/* 文本装饰 */
p {
text-wrap: pretty; /* 避免孤字 */
hyphens: auto;
}
/* 滚动驱动动画(新特性,渐进增强) */
@supports (animation-timeline: scroll()) {
.progress {
animation: grow linear both;
animation-timeline: scroll();
}
}
渐进增强:使用新属性前可先用 @supports 检测,不支持的浏览器自然降级,不影响核心阅读体验。
11实战:把读书卡片排好看
接着 HTML 篇章的练习,用约 60 行 CSS 完成一张「小清新读书卡片」。重点观察:留白、层级、边框与阴影的克制使用。
瓦尔登湖
在湖畔独居两年,记录对自然、孤独与生活的思考。慢一点,也是一种抵达。
.book-card {
display: grid;
grid-template-columns: 130px 1fr;
gap: 22px;
align-items: start;
max-width: 560px;
padding: 24px;
background: #fffdf8;
border: 1px solid rgba(76, 107, 87, 0.14);
border-radius: 16px;
box-shadow: 0 12px 30px rgba(80, 72, 55, 0.06);
transition: transform 0.35s ease, box-shadow 0.35s ease;
}
.book-card:hover {
transform: translateY(-4px);
box-shadow: 0 20px 44px rgba(80, 72, 55, 0.1);
}
.book-card figure {
margin: 0;
aspect-ratio: 3 / 4;
border-radius: 6px;
overflow: hidden;
background: linear-gradient(160deg, #dbe7d8, #eef3ea 45%, #f6eceb);
}
.book-card img {
width: 100%;
height: 100%;
object-fit: cover;
}
.book-card__title {
font-family: "Songti SC", serif;
font-size: 20px;
letter-spacing: 2px;
margin-bottom: 6px;
}
.book-card__meta {
font-size: 12px;
color: #6f6a61;
margin-bottom: 14px;
}
.book-card__text {
font-size: 13px;
line-height: 1.95;
color: #4a463f;
}
.book-card .tags {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 16px;
}
.book-card .tags li {
font-size: 11px;
padding: 3px 10px;
border-radius: 999px;
background: #f3efe4;
color: #6f6a61;
}
/* 窄屏时改为上下结构 */
@media (max-width: 480px) {
.book-card {
grid-template-columns: 1fr;
}
.book-card figure {
max-width: 130px;
}
}
12常见坑与自测
- 不会用 gap。还在用 margin 拼间距,容易产生多余外边距;Flex 与 Grid 都支持 gap。
- 滥用绝对定位。绝对定位会让元素脱离文档流,页面一变就错位。优先用正常流 + Flex/Grid。
- 写死高度。内容一多就溢出,尽量用 min-height 代替 height。
- z-index 乱飞。先检查是否在同一个层叠上下文里,盲目加数值只会越来越乱。
- 忽略焦点样式。
outline: none会破坏键盘可用性,应换成自定义的:focus-visible样式。 - 忘记移动端。写完桌面版,一定要缩窄窗口或用设备模拟器检查一遍。
自测清单:文字是否清晰易读?间距是否有节奏?层级是否分明?交互状态(hover / focus / active)是否都有反馈?把窗口缩到 375px 再看一次。
下一篇进入 JavaScript 篇章,让页面开始「动」起来。