[selector] {
[property]: [value]; /* ← Declaration */
}
| 規範項 | 標準 | 示例 |
|---|---|---|
| Class 命名 | kebab-case(連字符分隔) | .widget-header, .btn-primary |
| 縮進 | 4 空格(或 2 空格,團隊統一) | 見下文 |
| 聲明拆行 | 每條聲明獨佔一行 | color: red;\nbackground: blue; |
| 聲明順序 | 功能分組:佈局→盒模型→視覺→文字→動畫→其他 | 見下文 |
| 前綴對齊 | 值垂直對齊 | -webkit-xxx: val;\n -moz-xxx: val; |
| 縮進反映 DOM | 巢狀層級對應 HTML 結構 | .parent { .child { } } |
| 保留尾隨分號 | 最後一條聲明也加分號 | color: red; |
.widget {
padding: 10px;
border: 1px solid #bada55;
background-color: #c0ffee;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.widget-heading {
font-size: 1.5rem;
line-height: 1;
font-weight: bold;
color: #bada55;
margin-right: -10px;
margin-left: -10px;
padding: 0.25em;
}
觀察:`.widget-heading` 比 `.widget` 多一級縮進,直觀反映它是 `.widget` 的子元素,閱讀代碼即可獲取 DOM 結構信息。
.example {
/* 1. 佈局/定位:脫離文檔流、建立格式化上下文 */
position: absolute;
top: 0; right: 0;
z-index: 100;
display: flex;
flex-direction: column;
/* 2. 盒模型:尺寸、間距、邊框 */
width: 300px;
height: auto;
padding: 1rem;
margin-bottom: 1rem;
border: 1px solid #e0e0e0;
border-radius: 8px;
/* 3. 視覺/裝飾:背景、陰影、漸層、濾鏡 */
background: linear-gradient(135deg, #fff, #f5f5f5);
box-shadow: 0 4px 12px rgba(0,0,0,.08);
/* 4. 文字/排版:字體、行高、顏色、對齊 */
font-family: system-ui, sans-serif;
font-size: 1rem;
line-height: 1.6;
color: #333;
text-align: left;
/* 5. 動畫/過渡/變換 */
transition: transform 0.2s ease, box-shadow 0.2s ease;
transform: translateZ(0); /* 啟用 GPU 層 */
/* 6. 其他:游標、指標事件、溢出等 */
cursor: pointer;
pointer-events: auto;
overflow: hidden;
}
當屬性極簡、高度相關、大量重複時,單行寫法反而更易掃描:
.t10 { width: 10%; }
.t20 { width: 20%; }
.t25 { width: 25%; } /* 1/4 */
.t30 { width: 30%; }
.t33 { width: 33.333%; } /* 1/3 */
.t40 { width: 40%; }
.t50 { width: 50%; } /* 1/2 */
.t60 { width: 60%; }
.t66 { width: 66.666%; } /* 2/3 */
.t70 { width: 70%; }
.t75 { width: 75%; } /* 3/4 */
.t80 { width: 80%; }
.t90 { width: 90%; }
來源:inuit.css 表格網格系統。此類工具類類別,單行更緊湊、對比更清晰。
.block { } /* 獨立塊 */
.block__element { } /* 塊內元素 */
.block--modifier { } /* 狀態/變體 */
.block__element--modifier { }
:root {
/* 色彩系統 */
--color-primary: #3498db;
--color-primary-hover: #2980b9;
--color-text: #333;
--color-text-muted: #999;
--color-border: #e0e0e0;
/* 間距系統 */
--space-xs: 0.25rem;
--space-sm: 0.5rem;
--space-md: 1rem;
--space-lg: 1.5rem;
--space-xl: 2rem;
/* 字體系統 */
--fs-xs: 0.75rem;
--fs-sm: 0.875rem;
--fs-base: 1rem;
--fs-lg: 1.125rem;
--fs-xl: 1.25rem;
--fs-2xl: 1.5rem;
/* 斷點(配合 PostCSS @custom-media) */
--bp-sm: 576px;
--bp-md: 768px;
--bp-lg: 992px;
--bp-xl: 1200px;
}
:root { --bg: #fff; --text: #333; }
[data-theme="dark"] { --bg: #1a1a1a; --text: #f5f5f5; }
body { background: var(--bg); color: var(--text); }
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; line-height: 1.5; }
img, video { max-width: 100%; height: auto; display: block; }
input, button, textarea, select { font: inherit; }
.sr-only {
position: absolute;
width: 1px; height: 1px;
padding: 0; margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
這份指南的核心思想:**代碼結構映射 DOM 結構,聲明順序遵循渲染管線,命名語意化扁平化,工具鏈守護一致性**。將這些規範內化為習慣,配合 Stylelint + Prettier 自動化,即可編寫出既優雅又健壯的 CSS 代碼庫。