🤖 Assistant
🎨 CSS 零基础完全指南
🌟 CSS 是什么?
- CSS(Cascading Style Sheets):层叠样式表
- 控制网页的外观表现(如颜色、布局、字体)
- HTML负责结构,CSS负责美化(类似毛坯房 + 装修)
- 文件扩展名
.css
(如 style.css
)
📦 CSS 三种引入方式
1. 内联样式(直接写在标签里)
1
| <p style="color: red;">红色文字</p>
|
2. 内部样式表(写在 <style>
标签中)
1 2 3 4 5 6 7
| <head> <style> p { font-size: 16px; } </style> </head>
|
3. 外部样式表(最常用,单独写.css文件)
1 2
| <link rel="stylesheet" href="style.css">
|
🔍 CSS 选择器
1. 基础选择器
选择器 |
示例 |
说明 |
元素选择器 |
p { ... } |
选择所有 <p> 标签 |
类选择器 |
.box { ... } |
选择所有 class="box" 的元素 |
ID选择器 |
#header { ... } |
选择 id="header" 的元素 |
通配符选择器 |
* { ... } |
选择页面所有元素 |
2. 组合选择器
1 2 3 4 5 6 7 8 9 10 11
| div p { ... }
div > span { ... }
h1 + p { ... }
h1, h2, h3 { ... }
|
3. 伪类 & 伪元素
1 2 3 4 5 6 7
| a:hover { ... } li:nth-child(2) { ... }
p::first-letter { ... } div::after { content: "▶"; }
|
📐 盒模型(快递盒子理论)

1 2 3 4 5 6 7
| div { width: 200px; height: 100px; padding: 20px; border: 5px solid #000; margin: 30px; }
|
盒模型切换
1 2
| box-sizing: content-box; box-sizing: border-box;
|
🎯 常用CSS属性
1. 文本样式
1 2 3 4 5 6 7 8
| p { color: #ff0000; font-size: 16px; font-family: "Microsoft Yahei", Arial; line-height: 1.5; text-align: center; text-decoration: underline; }
|
2. 背景样式
1 2 3 4 5 6
| div { background-color: #f0f0f0; background-image: url("bg.jpg"); background-repeat: no-repeat; background-size: cover; }
|
3. 布局属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| display: block; display: inline; display: inline-block;
position: static; position: relative; position: absolute; position: fixed;
.container { display: flex; justify-content: center; align-items: center; }
|
🌀 Flex 布局速查表
容器属性
1 2 3 4 5
| flex-direction: row; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 20px;
|
项目属性
1 2 3
| flex: 1; align-self: flex-start; order: 2;
|
📱 响应式设计(媒体查询)
1 2 3 4 5 6 7 8 9
| @media (max-width: 600px) { .box { width: 100%; flex-direction: column; } }
@media (orientation: portrait) { }
|
🌈 CSS 进阶特性
1. 过渡动画
1 2 3 4 5 6
| button { transition: all 0.3s ease; } button:hover { transform: scale(1.1); }
|
2. 关键帧动画
1 2 3 4 5 6 7
| @keyframes slide { from { transform: translateX(0); } to { transform: translateX(200px); } } .box { animation: slide 2s infinite alternate; }
|
3. CSS 变量
1 2 3 4 5 6
| :root { --main-color: #4285f4; } header { background-color: var(--main-color); }
|
🛠️ 综合案例:个人主页美化
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 42 43 44 45
| body { font-family: 'Arial', sans-serif; margin: 0; padding: 20px; }
header { background: linear-gradient(45deg, #2196F3, #E91E63); color: white; padding: 2rem; text-align: center; }
nav a { color: white; text-decoration: none; margin: 0 15px; padding: 5px; border-radius: 4px; }
nav a:hover { background: rgba(255,255,255,0.2); }
.profile-img { width: 150px; height: 150px; border-radius: 50%; box-shadow: 0 5px 15px rgba(0,0,0,0.2); }
.grid-container { display: grid; grid-template-columns: 1fr 1fr; gap: 30px; margin-top: 40px; }
@media (max-width: 768px) { .grid-container { grid-template-columns: 1fr; } }
|
🚀 最佳实践建议
- 使用语义化类名(如
.nav-link
而非 .blue-text
)
- 遵循移动优先原则(先写移动端样式,再用媒体查询适配大屏)
- 善用开发者工具调试(按F12,实时调整样式)
- 保持代码整洁(使用CSS预处理器如Sass/Less)
- 学习使用CSS框架(Bootstrap/Tailwind CSS)
CSS的学习需要大量练习,尝试模仿优秀的网页设计,并亲手调试每个样式属感受变化!✨
```