本文内容思维导图:

精通CSS基础:选择器、盒模型与样式完全指南
第一部分:选择器深度解析
1.1 基础选择器类型
1.1.1 元素选择器
| |
| p { |
| line-height: 1.6; |
| } |
| |
| |
| div { |
| box-sizing: border-box; |
| } |
复制
1.1.2 类选择器(实战技巧)
| |
| .btn.primary { |
| background: #2196F3; |
| } |
| |
| |
| [class^="icon-"] { |
| width: 24px; |
| height: 24px; |
| } |
复制
1.1.3 ID选择器(特殊场景)
| #main-content { |
| max-width: 1200px; |
| margin: 0 auto; |
| } |
| |
| |
| |
复制
1.2 组合选择器
1.2.1 后代选择器
| nav ul li a { |
| color: #333; |
| } |
| |
| |
| .nav-link { |
| color: #333; |
| } |
复制
1.2.2 子选择器(>)
| |
| .menu > li { |
| border-bottom: 1px solid #eee; |
| } |
复制
1.2.3 相邻兄弟选择器(+)
复制
1.2.4 通用兄弟选择器(~)
复制
1.3 属性选择器(高级应用)
| |
| input[type="password"] { |
| font-family: monospace; |
| } |
| |
| |
| a[href*="example.com"] { |
| color: #f00; |
| } |
| |
| |
| a[href^="https"]::after { |
| content: "🔒"; |
| } |
| |
| |
| a[href$=".pdf"]::after { |
| content: "📄"; |
| } |
复制
1.4 伪类选择器
1.4.1 状态伪类
| |
| input:disabled { |
| opacity: 0.5; |
| } |
| |
| input:checked + label { |
| font-weight: bold; |
| } |
复制
1.4.2 结构伪类
| |
| tr:nth-child(odd) { |
| background: #f8f9fa; |
| } |
| |
| |
| li:nth-child(3n+1) { |
| color: blue; |
| } |
| |
| |
| li:nth-last-child(2) { |
| border: 2px solid red; |
| } |
复制
1.5 伪元素选择器
| |
| p::first-letter { |
| font-size: 2em; |
| float: left; |
| } |
| |
| |
| ul li::marker { |
| content: "►"; |
| color: #f00; |
| } |
| |
| |
| input::placeholder { |
| color: #999; |
| font-style: italic; |
| } |
复制
1.6 选择器特异性计算
选择器类型 | 特异性值(a-b-c-d) |
---|
行内样式 | 1-0-0-0 |
ID选择器 | 0-1-0-0 |
类/属性/伪类 | 0-0-1-0 |
元素/伪元素 | 0-0-0-1 |
计算示例:
| #nav .item:hover {} |
| .header ul li a {} |
复制
第二部分:盒模型深度解析
2.1 盒模型组成

| div { |
| width: 300px; |
| padding: 20px; |
| border: 2px solid #000; |
| margin: 30px; |
| } |
复制
2.2 盒模型类型
2.2.1 标准盒模型
| .box-standard { |
| box-sizing: content-box; |
| |
| } |
复制
2.2.2 替代(IE)盒模型
| .box-alternate { |
| box-sizing: border-box; |
| |
| } |
复制
现代开发最佳实践:
| |
| *, |
| *::before, |
| *::after { |
| box-sizing: border-box; |
| } |
复制
2.3 边距折叠问题
三种折叠场景:
- 相邻元素
- 父元素与第一个/最后一个子元素
- 空元素
解决方案:
| .parent { |
| padding-top: 1px; |
| } |
| |
| .child { |
| margin: 20px 0; |
| } |
复制
2.4 尺寸计算进阶
| .container { |
| width: min(90%, 1200px); |
| height: calc(100vh - 60px); |
| padding: clamp(1rem, 5vw, 2rem); |
| } |
复制
第三部分:基础样式与实战
3.1 文本样式
| .text-advanced { |
| |
| font: 500 1.125rem/1.7 'SF Pro Text', system-ui, sans-serif; |
| |
| |
| text-rendering: optimizeLegibility; |
| font-variant: small-caps; |
| font-feature-settings: "kern", "liga"; |
| |
| |
| text-shadow: |
| 0 2px 1px rgba(0, 0, 0, 0.1), |
| 1px 3px 2px rgba(0, 0, 0, 0.15); |
| |
| |
| hyphens: auto; |
| line-break: strict; |
| |
| |
| @supports (text-wrap: balance) { |
| text-wrap: balance; |
| } |
| } |
| |
| |
| @font-face { |
| font-family: 'ModernVariable'; |
| src: url('ModernVariable.woff2') format('woff2-variations'); |
| font-weight: 100 900; |
| font-stretch: 75% 125%; |
| } |
| |
| .variable-font-demo { |
| font-family: 'ModernVariable', sans-serif; |
| font-weight: 450; |
| font-stretch: 92%; |
| } |
复制
3.2 背景样式
| .text-advanced { |
| |
| font: 500 1.125rem/1.7 'SF Pro Text', system-ui, sans-serif; |
| |
| |
| text-rendering: optimizeLegibility; |
| font-variant: small-caps; |
| font-feature-settings: "kern", "liga"; |
| |
| |
| text-shadow: |
| 0 2px 1px rgba(0, 0, 0, 0.1), |
| 1px 3px 2px rgba(0, 0, 0, 0.15); |
| |
| |
| hyphens: auto; |
| line-break: strict; |
| |
| |
| @supports (text-wrap: balance) { |
| text-wrap: balance; |
| } |
| } |
| |
| |
| @font-face { |
| font-family: 'ModernVariable'; |
| src: url('ModernVariable.woff2') format('woff2-variations'); |
| font-weight: 100 900; |
| font-stretch: 75% 125%; |
| } |
| |
| .variable-font-demo { |
| font-family: 'ModernVariable', sans-serif; |
| font-weight: 450; |
| font-stretch: 92%; |
| } |
复制
3.3 边框样式
| .border-magic { |
| |
| border: 2px solid transparent; |
| border-image: |
| linear-gradient( |
| 135deg, |
| #ff6b6b 0%, |
| #4ecdc4 50%, |
| #45b7d1 100% |
| ) 1; |
| border-image-slice: 1; |
| |
| |
| animation: border-flow 3s linear infinite; |
| } |
| |
| @keyframes border-flow { |
| 100% { border-image-source: |
| linear-gradient( |
| 135deg, |
| #45b7d1 0%, |
| #ff6b6b 50%, |
| #4ecdc4 100% |
| ); |
| } |
| } |
| |
| |
| .neomorphic-border { |
| border: none; |
| position: relative; |
| } |
| |
| .neomorphic-border::after { |
| content: ''; |
| position: absolute; |
| inset: -8px; |
| background: |
| linear-gradient(145deg, |
| rgba(255,255,255,0.1), |
| rgba(0,0,0,0.1)); |
| border-radius: inherit; |
| z-index: -1; |
| filter: blur(12px); |
| } |
复制
3.4 实战案例库
案例1:动态按钮组件
| .dynamic-button { |
| --btn-color: #4ecdc4; |
| --shadow-depth: 8px; |
| |
| padding: 1rem 2rem; |
| border: none; |
| background: var(--btn-color); |
| color: white; |
| |
| |
| box-shadow: |
| 0 var(--shadow-depth) 0 0 color-mix(in srgb, var(--btn-color) 80%, black), |
| 0 4px 12px rgba(0,0,0,0.1); |
| |
| transition: |
| transform 0.2s, |
| box-shadow 0.2s; |
| } |
| |
| .dynamic-button:active { |
| transform: translateY(calc(var(--shadow-depth) / 2)); |
| box-shadow: |
| 0 calc(var(--shadow-depth) / 2) 0 0 color-mix(in srgb, var(--btn-color) 80%, black), |
| 0 2px 6px rgba(0,0,0,0.1); |
| } |
| |
复制
案例2:玻璃拟态卡片
| .glass-card { |
| background: rgba(255, 255, 255, 0.1); |
| backdrop-filter: blur(12px); |
| border-radius: 16px; |
| border: 1px solid rgba(255,255,255,0.2); |
| box-shadow: |
| 0 8px 32px rgba(0, 0, 0, 0.1), |
| inset 0 1px 2px rgba(255,255,255,0.3); |
| |
| |
| position: relative; |
| overflow: hidden; |
| } |
| |
| .glass-card::before { |
| content: ''; |
| position: absolute; |
| top: -50%; |
| left: -50%; |
| width: 200%; |
| height: 200%; |
| background: linear-gradient( |
| 45deg, |
| transparent 48%, |
| rgba(255,255,255,0.15) 50%, |
| transparent 52% |
| ); |
| animation: shine 4s infinite linear; |
| } |
| |
| @keyframes shine { |
| 100% { transform: translate(50%, 50%); } |
| } |
| |
复制
3.5 现代CSS特性集成
| |
| :root { |
| --primary-color: oklch(65% 0.25 210); |
| } |
| |
| .modern-box { |
| |
| background: color-mix( |
| in oklch, |
| var(--primary-color) 70%, |
| white |
| ); |
| |
| |
| container-type: inline-size; |
| |
| |
| width: clamp(300px, 50cqi, 600px); |
| padding: max(1rem, 3vmin); |
| |
| |
| animation: fade-in auto linear; |
| animation-timeline: view(); |
| } |
| |
| @keyframes fade-in { |
| entry 0% { opacity: 0; } |
| entry 100% { opacity: 1; } |
| } |
| |
复制
第四部分:调试与常见问题
4.1 浏览器开发者工具技巧
- 盒模型可视化
- 强制元素状态(:hover)
- 实时编辑样式
- 计算样式查看
4.2 常见问题解决方案
问题1:样式不生效
问题2:意外边距
| |
| * { |
| margin: 0; |
| padding: 0; |
| } |
| |
| |
| body { |
| line-height: 1.5; |
| -webkit-font-smoothing: antialiased; |
| } |
复制
问题3:边框影响布局
| |
| .debug * { |
| outline: 1px solid red !important; |
| } |
复制
综合案例:电商商品卡片
文件目录:
| ───CSS |
| │ css.md |
| │ index.html |
| │ styles.css |
| │ 优质羊毛衫.jpg |
复制
实现效果:

| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>电商商品卡片</title> |
| <link rel="stylesheet" href="styles.css"> |
| </head> |
| <body> |
| <div class="product-container"> |
| <div class="product-card"> |
| <img src="product.jpg" alt="商品图片" class="product-image"> |
| <h3 class="product-title">优质羊毛衫</h3> |
| <p class="product-description">这是一款高品质的羊毛衫,采用优质羊毛制作,柔软舒适,保暖效果极佳。</p> |
| <div class="price">¥299</div> |
| <button class="add-to-cart">加入购物车</button> |
| </div> |
| |
| <div class="product-card"> |
| <img src="product.jpg" alt="商品图片" class="product-image"> |
| <h3 class="product-title">优质羊毛衫</h3> |
| <p class="product-description">这是一款高品质的羊毛衫,采用优质羊毛制作,柔软舒适,保暖效果极佳。</p> |
| <div class="price">¥299</div> |
| <button class="add-to-cart">加入购物车</button> |
| </div> |
| |
| <div class="product-card"> |
| <img src="product.jpg" alt="商品图片" class="product-image"> |
| <h3 class="product-title">优质羊毛衫</h3> |
| <p class="product-description">这是一款高品质的羊毛衫,采用优质羊毛制作,柔软舒适,保暖效果极佳。</p> |
| <div class="price">¥299</div> |
| <button class="add-to-cart">加入购物车</button> |
| </div> |
| </div> |
| </body> |
| </html> |
复制
| |
| .product-container { |
| display: flex; |
| justify-content: space-around; |
| align-items: flex-start; |
| padding: 20px; |
| background-color: #f5f5f5; |
| } |
| |
| |
| .product-card { |
| width: calc(33.333% - 20px); |
| border: 1px solid #eee; |
| border-radius: 8px; |
| padding: 16px; |
| box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); |
| text-align: center; |
| background-color: #fff; |
| margin: 0 10px; |
| } |
| |
| |
| .product-image { |
| width: 100%; |
| height: auto; |
| border-radius: 8px; |
| margin-bottom: 12px; |
| } |
| |
| |
| .product-title { |
| font-size: 1.2rem; |
| margin: 12px 0; |
| color: #333; |
| font-weight: bold; |
| } |
| |
| |
| .product-description { |
| font-size: 0.9rem; |
| color: #666; |
| margin: 8px 0; |
| line-height: 1.4; |
| } |
| |
| |
| .price { |
| font-size: 1.5rem; |
| color: #e53935; |
| margin: 12px 0; |
| font-weight: bold; |
| } |
| |
| |
| .add-to-cart { |
| display: block; |
| width: 100%; |
| padding: 12px; |
| background: #2196F3; |
| color: white; |
| border: none; |
| border-radius: 4px; |
| cursor: pointer; |
| transition: background 0.3s; |
| margin-top: 12px; |
| } |
| |
| .add-to-cart:hover { |
| background: #1976D2; |
| } |
复制
学习路线检查清单
- 能正确计算选择器特异性
- 能解释标准盒模型与替代盒模型的区别
- 能使用属性选择器完成复杂匹配
- 能处理边距折叠问题
- 能使用现代单位(vw、rem、min/max)进行响应式布局
- 能使用开发者工具调试盒模型
通过系统掌握以上内容,开发者将能:
✅ 精准控制页面元素的样式表现
✅ 构建稳定可靠的布局结构
✅ 快速定位和解决样式问题
✅ 为后续学习Flex/Grid布局打下坚实基础