優秀的 CSS 註釋不是「解釋代碼做了什麼」,而是「傳達設計意圖、架構決策、協作契約」。本文構建**文件級目錄、模塊級分隔、行級邏輯**三級註釋體系,並引入可機器解析的智能標籤,讓註釋成為團隊知識資產。
放在文件頂部,提供宏觀地圖:
/*======================================================================
= @file: button.css
= @description: 按鈕組件完整樣式定義(基礎/狀態/尺寸/圖標/加載態)
= @author: Li Si
= @version: 2.1.0
= @dependencies: variables.css, mixins.css, reset.css
= @changelog:
= 2.1.0 (2024-01-15): 新增 loading 狀態、無障礙焦點環優化
= 2.0.0 (2023-11-01): 重構為 BEM 命名、引入 CSS 變數體系
======================================================================*/
/*--------------------------------------------------
目錄
1. Reset & Base
2. Variants (Primary/Secondary/Ghost/Danger)
3. Sizes (SM/MD/LG)
4. States (Hover/Active/Disabled/Loading/Focus)
5. Icon Button
6. Accessibility
--------------------------------------------------*/
使用**統一視覺分隔符**,配合**語義標籤**支持 IDE 折疊/跳轉:
/*======================================================================
= @section Base Styles
======================================================================*/
.btn {
--btn-height: 40px;
--btn-padding-x: 16px;
--btn-font-size: 14px;
display: inline-flex;
align-items: center;
justify-content: center;
height: var(--btn-height);
padding: 0 var(--btn-padding-x);
font-size: var(--btn-font-size);
font-weight: 500;
border: 1px solid transparent;
border-radius: 6px;
cursor: pointer;
transition: all 0.2s ease;
text-decoration: none;
white-space: nowrap;
}
/*======================================================================
= @section Variants
======================================================================*/
/* @variant Primary -- 主要行動號召 */
.btn--primary {
--btn-bg: var(--color-primary-500);
--btn-color: #fff;
--btn-border: var(--color-primary-500);
--btn-hover-bg: var(--color-primary-600);
--btn-active-bg: var(--color-primary-700);
}
/* @variant Secondary -- 次要操作 */
.btn--secondary { ... }
/* @variant Ghost -- 幽靈按鈕(無背景) */
.btn--ghost { ... }
/*======================================================================
= @section States
======================================================================*/
/* @state Hover */
.btn:hover:not(:disabled) { ... }
/* @state Focus-visible -- 鍵盤無障礙焦點 */
.btn:focus-visible {
outline: none;
box-shadow: 0 0 0 3px var(--color-primary-200);
}
/* @state Loading -- 內聯 SVG 動畫 */
.btn--loading { ... }
只註釋**為什麼(Why)**,不註釋**做什麼(What)**:
.btn::before {
content: '';
/* 使用偽元素實現邊框漸變動畫,避免直接 transition border-color 觸發重排 */
position: absolute;
inset: -2px;
border-radius: 8px;
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
opacity: 0;
transition: opacity 0.3s ease;
z-index: -1; /* 置於文字之下 */
}
.btn:hover::before { opacity: 1; }
/* @hack Firefox < 64 不支持 focus-visible,需 polyfill */
/* @todo 2024-Q2: 遷移至 :where() 降低專用度 */
/* @compat IE11: 需補充 -ms-flex 兼容 */
引入**結構化標籤**,支持工具提取生成文檔/圖譜:
/** @component Button **/
/** @variant Primary **/
/** @state Hover **/
/** @prop --btn-bg {Color} Background color **/
/** @prop --btn-height {Length} Button height **/
/** @dependency variables.css **/
/** @a11y WCAG 2.1 AA **/
/** @browser Chrome 60+, Firefox 55+, Safari 13+, Edge 79+ **/
配合 Style Dictionary 可自動生成設計令牌文檔、Figma 插件同步、多端代碼輸出。
避免過度限定選擇器(如 `ul.nav`),但需告知預期掛載元素:
/* @element nav */ /* 或 /*html*/ */
.nav { ... }
/* @element ol */
.breadcrumb { ... }
/* @element p */
.intro { ... }
保持選擇器低專用度,同時傳達語義上下文。
為相關功能打標,支持全局搜尋定位:
/** @tag layout grid flexbox **/
.grid { ... }
/** @tag typography heading **/
.heading-1 { ... }
/** @tag interactive button form **/
.btn { ... }
搜尋 `@tag flexbox` 即可定位所有佈局相關模塊。
跨文件繼承關係顯式化:
/* base.css */
/** @extends theme.css .card **/
.card-base { ... }
/* theme.css */
/** @base base.css .card-base **/
.card-elevated { ... }
註釋是給**未來的自己**和**現在的隊友**寫的愛 letter。投入 5% 編碼時間寫好註釋,換來 95% 維護期的順暢協作——這筆 ROI,無可辯駁。