前言
CSS(Cascading Style Sheets)是网页的”化妆品”,负责控制 HTML 元素的外观和布局。没有 CSS 的网页就像白纸黑字——有了 CSS,页面才能变得美观、灵动。
本文从零开始,带你系统掌握 CSS 的核心知识。
一、CSS 写在哪里?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <p style="color: red; font-size: 18px;">红色文字</p>
<head> <style> p { color: blue; } </style> </head>
<head> <link rel="stylesheet" href="style.css"> </head>
|
二、选择器
2.1 基础选择器
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| p { color: #333; }
.title { font-size: 24px; }
#header { height: 60px; }
* { margin: 0; padding: 0; }
h1, h2, h3 { font-weight: bold; }
|
2.2 层次选择器
1 2 3 4 5 6 7 8 9 10 11
| article p { line-height: 1.8; }
ul > li { list-style: none; }
h2 + p { margin-top: 0; }
h2 ~ p { color: #666; }
|
2.3 伪类选择器
1 2 3 4 5 6 7 8 9 10 11 12
| a:link { color: blue; } a:visited { color: purple; } a:hover { color: red; } a:active { color: orange; }
li:first-child { font-weight: bold; } li:last-child { border-bottom: none; } li:nth-child(odd) { background: #f5f5f5; } li:nth-child(even) { background: #fff; } li:nth-child(3n) { color: red; }
|
2.4 伪元素
1 2 3 4 5 6 7 8 9 10 11 12 13
| .clearfix::after { content: ""; display: table; clear: both; }
p::first-line { font-weight: bold; } p::first-letter { font-size: 2em; color: red; }
li::marker { color: #49b1f5; }
|
2.5 优先级(特异度)
1
| !important > 内联(1000) > ID(100) > 类/伪类(10) > 标签(1)
|
1 2 3 4 5 6 7 8 9 10 11
| p { color: black; }
.special p { color: blue; }
#main { color: red; }
p { color: green !important; }
|
三、盒模型
每个元素都是一个矩形盒子:
1 2 3 4 5 6 7 8 9 10 11 12
| ┌──────────────────────────┐ │ margin │ │ ┌──────────────────┐ │ │ │ border │ │ │ │ ┌──────────┐ │ │ │ │ │ padding │ │ │ │ │ │ ┌──────┐ │ │ │ │ │ │ │ 内容 │ │ │ │ │ │ │ └──────┘ │ │ │ │ │ └──────────┘ │ │ │ └──────────────────┘ │ └──────────────────────────┘
|
1 2 3 4 5 6 7 8 9 10
| .box { width: 200px; height: 100px; padding: 20px; border: 2px solid #333; margin: 30px; box-sizing: border-box; }
|
box-sizing 对比:
| 值 |
width 包含 |
content-box(默认) |
仅内容 |
border-box(推荐) |
内容 + padding + border |
四、文本与字体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| .text { color: #333; font-size: 16px; font-weight: bold; font-family: "Microsoft YaHei", sans-serif; text-align: center; line-height: 1.8; letter-spacing: 1px; text-decoration: underline; text-indent: 2em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; word-break: break-all; }
|
五、背景与颜色
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| .card { background-color: #f5f5f5; background-image: url("bg.jpg"); background-size: cover; background-position: center; background-repeat: no-repeat; background: linear-gradient(135deg, #667eea, #764ba2); background: linear-gradient(to right, red, orange, yellow); background: radial-gradient(circle, #fff, #333); background: #f5f5f5 url("bg.jpg") center/cover no-repeat; opacity: 0.8; background: rgba(0, 0, 0, 0.5); }
|
颜色表达方式:
1 2 3 4 5
| color: #ff6600; color: rgb(255, 102, 0); color: rgba(255, 102, 0, 0.8); color: hsl(24, 100%, 50%); color: var(--main-color);
|
六、CSS 变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| :root { --primary: #49b1f5; --text-color: #333; --bg-color: #fff; --spacing: 16px; --radius: 8px; }
.btn { background: var(--primary); color: var(--bg-color); padding: var(--spacing); border-radius: var(--radius); }
[data-theme="dark"] { --text-color: #eee; --bg-color: #1a1a2e; }
|
七、Display 属性
1 2 3 4 5 6 7 8 9 10 11 12 13
| .block { display: block; }
.inline { display: inline; }
.inline-block { display: inline-block; }
.hidden { display: none; } .invisible { visibility: hidden; } .transparent { opacity: 0; }
|
八、Flexbox 布局(最常用)⭐
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| .flex-container { display: flex; flex-direction: row; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 16px; }
.flex-item { flex: 1; }
.flex-item-2 { flex: 2; }
.center-box { display: flex; justify-content: center; align-items: center; }
|
常见 Flex 布局:
1 2 3 4 5 6 7 8 9
| .nav { display: flex; justify-content: space-between; }
.columns { display: flex; } .columns > * { flex: 1; }
.hero { display: flex; justify-content: center; align-items: center; height: 400px; }
|
九、Grid 布局(二维布局最强)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: auto; gap: 20px; }
.grid-varied { grid-template-columns: 200px 1fr 1fr; }
.grid-auto { grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); }
.grid-item { grid-column: span 2; grid-row: span 2; }
|
十、定位(Position)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| .static { position: static; }
.relative { position: relative; top: 10px; left: 20px; }
.absolute { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
.fixed { position: fixed; top: 0; right: 0; z-index: 999; }
.sticky { position: sticky; top: 0; }
|
十一、过渡与动画
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| .btn { background: #49b1f5; transition: all 0.3s ease; }
.btn:hover { background: #ff7242; transform: scale(1.05); }
@keyframes fadeIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
.animate-in { animation: fadeIn 0.5s ease forwards; }
@keyframes spin { to { transform: rotate(360deg); } }
.spinner { width: 40px; height: 40px; border: 4px solid #eee; border-top-color: #49b1f5; border-radius: 50%; animation: spin 0.8s linear infinite; }
|
十二、响应式设计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| .container { padding: 16px; }
@media (min-width: 768px) { .container { padding: 24px; } }
@media (min-width: 1024px) { .container { max-width: 1200px; margin: 0 auto; } }
@media (prefers-color-scheme: dark) { body { background: #1a1a2e; color: #eee; } }
@media print { nav, .sidebar { display: none; } }
|
十三、常见 UI 组件
1 2 3 4 5 6 7 8 9 10 11
| .card { background: #fff; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); padding: 24px; transition: box-shadow 0.3s; } .card:hover { box-shadow: 0 4px 16px rgba(0,0,0,0.15); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| .btn { display: inline-block; padding: 10px 24px; border: none; border-radius: 6px; background: #49b1f5; color: #fff; cursor: pointer; font-size: 14px; transition: all 0.3s; } .btn:hover { opacity: 0.85; } .btn:active { transform: scale(0.97); }
|
CSS 常用速查表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| .truncate { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.clearfix::after { content: ""; display: table; clear: both; }
.hide-scrollbar { overflow: auto; scrollbar-width: none; } .hide-scrollbar::-webkit-scrollbar { display: none; }
img { max-width: 100%; height: auto; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }
|
结语
CSS 看似简单,实则暗藏玄机。学习路线建议:
- 掌握盒模型和选择器(基础中的基础)
- 熟练使用 Flexbox(80% 的布局用它就够了)
- 了解 Grid(复杂二维布局的首选)
- 理解响应式设计(现代网页必备)
- 实战复制几个你喜欢的网站页面
最好的 CSS 学习方法:打开浏览器 F12,把别人的样式改一改,看看效果。🎨