CSS (Cascading Style Sheets) 不僅是「控制顏色字體大小」的工具。它是瀏覽器渲染管線的關鍵輸入、用戶體驗的直接塑造者、前端工程化的架構基石、商業轉化的隱形乘數。本文系統拆解 CSS 的完整作用鏈路。
HTML → DOM Tree
CSS → CSSOM Tree
DOM + CSSOM → Render Tree (僅包含可見節點及其幾何/樣式信息)
Render Tree → Layout (幾何計算) → Paint (像素繪製) → Composite (合成輸出)
| 佈局模式 | 核心 CSS 屬性 | 適用場景 |
|---|---|---|
| 流式佈局 | display: block/inline、margin、width/height | 文檔結構、文章排版 |
| Flexbox (一維) | display: flex、flex-direction、justify-content、align-items、gap、flex | 導航、卡片列表、表單、居中、等高 |
| Grid (二維) | display: grid、grid-template-columns/rows/areas、gap、minmax、auto-fit | 頁面骨架、儀表盤、相冊、複雜對齊 |
| 定位 | position: relative/absolute/fixed/sticky、top/right/bottom/left、z-index | 彈層、吸頂、側邊欄、工具提示、錨點定位 |
| 浮動 (歷史) | float、clear、clearfix | 文字環繞、舊項目維護 |
| 多欄佈局 | column-count/width/gap/rule、break-inside | 文章分欄、瀑布流 |
現代原則:優先 Flex/Grid,佈局邏輯內聚於容器,子元素無感知,響應式通過容器查詢/媒體查詢在容器層面切換佈局模式。
:root {
/* 色彩系統 */
--color-primary-500: #007bff;
--color-semantic-success: var(--color-green-500);
--color-semantic-error: var(--color-red-500);
/* 間距系統 (4px 基數) */
--space-1: 4px; --space-2: 8px; --space-3: 16px; --space-4: 24px; --space-5: 32px;
/* 字體系統 */
--font-family-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
--font-size-scale: 1.25; /* Major Third 音階比例 */
--font-size-base: 1rem;
/* 動效系統 */
--easing-standard: cubic-bezier(0.4, 0, 0.2, 1);
--duration-fast: 150ms; --duration-normal: 250ms; --duration-slow: 350ms;
/* 陰影/圓角/Z-index 系統化... */
}
| 類別 | 核心屬性 | 現代增強 |
|---|---|---|
| 色彩 | color、background-color、border-color | color-mix()、relative color syntax、OKLCH/P3 廣色域、color-scheme |
| 字體 | font-family/size/weight/style、line-height、letter-spacing | Variable Fonts (wght/wdth/opsz/ital)、font-palette、font-feature-settings |
| 文本 | text-align/decoration/transform/indent/overflow/overflow-wrap | text-wrap: balance/pretty、text-box-trim、text-wrap-mode |
| 背景 | background-image/position/size/repeat/attachment/origin/clip | conic-gradient、background-image: image-set()、CSS Images Level 4 |
| 邊框 | border-width/style/color/radius、border-image | border-radius 多值語法、logical border properties |
| 陰影/濾鏡 | box-shadow、filter、backdrop-filter | drop-shadow()、多層陰影分層、濾鏡動畫性能 |
| 變形/動畫 | transform、transition、animation、@keyframes | @property 類型化動畫、scroll-driven animations、view-transitions |
| 剪裁/遮罩 | clip-path、mask、mask-image | clip-path: path()、mask-composite、SVG 整合 |
/*, *::before, *::after { transition: all 0.2s var(--easing-standard); }*/ /* 全局基礎過渡 */
/* 交互狀態標準化 */
.interactive {
cursor: pointer;
transition: background-color var(--duration-fast) var(--easing-standard),
transform var(--duration-fast) var(--easing-standard),
box-shadow var(--duration-fast) var(--easing-standard);
}
.interactive:hover { /* 狀態變化 */ }
.interactive:active { transform: scale(0.98); }
.interactive:focus-visible { outline: 2px solid var(--color-primary-500); outline-offset: 2px; }
.interactive:disabled { opacity: 0.5; cursor: not-allowed; pointer-events: none; }
/* 載入狀態 */
.is-loading { position: relative; color: transparent; pointer-events: none; }
.is-loading::after {
content: ""; position: absolute; inset: 0; border-radius: inherit;
background: linear-gradient(90deg, transparent, rgba(255,255,255,.4), transparent)
var(--bg-primary); animation: shimmer 1.5s infinite;
}
@keyframes shimmer { to { background-position: 200% 0; } }
| 指標 | CSS 影響機制 | 優化手段 |
|---|---|---|
| FCP/LCP | 阻塞渲染、字體加載、圖片佈局位移 | 關鍵 CSS 內聯、font-display: swap、預載入、width/height/aspect-ratio 預留 |
| CLS | 動態內容插入、字體回退佈局位移、無尺寸圖片 | 預留空間、font-size-adjust、size-adjust、content-visibility |
| INP/FID | 大樣式表阻塞主線程、複雜選擇器、佈局抖動 | 代碼分割、關鍵 CSS、contain、will-change、減少強制同步佈局 |
| TBT/TTI | CSS 解析/計算佔用主線程 | 移除未使用 CSS、簡化選擇器、避免 @import 鏈 |
設置 CSS 樣式,本質上是將設計意圖、業務邏輯、品牌資產、性能預算、無障礙承諾,編碼為瀏覽器可執行的幾何與視覺指令集。這不是裝飾,這是構建數字產品的基礎設施工程。