在大型項目中,CSS 註釋是架構文檔、協作契約、重構指南。好的註釋能:
/* ❌ 過度修飾:提高特異性、降低復用、綁定標籤 */
ul.nav { }
p.intro { }
ul.image-thumbs { }
/* ✅ 準修飾:註釋標籤,保持類選擇器純粹 */
/*html*/.page-header { } /* 作用於 html/body 根容器 */
/*ol*/.breadcrumb { } /* 語義:有序列表結構 */
/*p*/.intro { } /* 語義:段落級引導文本 */
/*ul*/.image-thumbs { } /* 語義:無序列表縮略圖網格 */
/*dl*/.definition-list { } /* 語義:定義列表 */
規則:/*tagName*/.className,標籤名用註釋包裹,不參與選擇器匹配。
/**
* ^navigation ^header ^lists
* @component header
* @owner @team-frontend
*/
.site-header { }
/**
* ^grids ^cards ^layout ^responsive
* @pattern grid-auto-fit
* @breakpoint md lg
*/
.auto-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: var(--space-lg);
}
/**
* ^forms ^validation ^accessibility
* @state error disabled loading
*/
.form-field { }
.form-field--error { }
.form-field--disabled { }
搜索用法:IDE 中搜索 ^navigation 即可定位所有導航相關樣式;@state error 找到所有錯誤狀態樣式。
/* base/typography.scss */
/**
* @base heading-base
* @extendable true
* @description 基礎標題樣式,子系統可通過 @extend 繼承
*/
.heading-base {
font-weight: 600;
line-height: 1.3;
color: var(--color-text-primary);
margin-block: 0.5em 0.25em;
}
/* components/article.scss */
/**
* @extends .heading-base from base/typography.scss
* @customizes font-size, color
*/
.article-title {
@extend .heading-base;
font-size: var(--font-size-3xl);
color: var(--color-brand);
}
工具鏈支持:Sass @extend、PostCSS postcss-extend、Stylelint 規則檢查 @extends 引用是否存在。
/* ==========================================================================
模塊:Button 組件
描述:全站通用按鈕,支持多變體、尺寸、狀態
作者:@designer-name @dev-name
更新:2024-01-15
依賴:design-tokens/colors, design-tokens/spacing
========================================================================== */
/* --- 變量與配置 ----------------------------------------------------------- */
.btn {
--btn-padding-y: var(--space-sm);
--btn-padding-x: var(--space-md);
--btn-font-size: var(--font-size-base);
--btn-radius: var(--radius-md);
--btn-transition: background 0.2s, box-shadow 0.2s, transform 0.1s;
}
/* --- 基礎樣式 ------------------------------------------------------------- */
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: var(--space-xs);
padding: var(--btn-padding-y) var(--btn-padding-x);
font-size: var(--btn-font-size);
font-weight: 500;
line-height: 1.5;
border-radius: var(--btn-radius);
border: 1px solid transparent;
cursor: pointer;
transition: var(--btn-transition);
text-decoration: none;
white-space: nowrap;
user-select: none;
}
/* --- 變體 ---------------------------------------------------------------- */
/* @variant primary */
.btn--primary {
background: var(--color-primary);
color: var(--color-primary-contrast);
}
.btn--primary:hover { background: var(--color-primary-hover); }
.btn--primary:active { background: var(--color-primary-active); }
/* @variant secondary */
.btn--secondary { ... }
/* --- 尺寸 ---------------------------------------------------------------- */
/* @size sm */
.btn--sm { --btn-padding-y: var(--space-xs); --btn-padding-x: var(--space-sm); --btn-font-size: var(--font-size-sm); }
/* --- 狀態 ---------------------------------------------------------------- */
/* @state disabled */
.btn:disabled,
.btn--disabled {
opacity: 0.5;
cursor: not-allowed;
pointer-events: none;
}
/* @state loading */
.btn--loading { position: relative; color: transparent; }
.btn--loading::after {
content: "";
position: absolute;
width: 1em; height: 1em;
border: 2px solid currentColor;
border-right-color: transparent;
border-radius: 50%;
animation: spin 0.6s linear infinite;
}
| 工具 | 用途 | 配置示例 |
|---|---|---|
| Stylelint | 強制註釋規範 | require-description: true, comment-pattern: "^\\s*[@\\^]" |
| PostCSS | 提取註釋生成文檔 | postcss-doc 生成 Markdown/HTML 組件文檔 |
| VS Code | 懸停提示顯示註釋 | 安裝 CSS Documentation 插件 |
| Storybook | 組件文檔自動同步 | parameters: { docs: { extractComponentDescription: true } } |
/* --- 區塊名 --- */ 分隔符 + 語義標簽/* @decision: 為什麼這樣寫,替代方案是什麼 *//* @hack: 臨時修復 #issue-123, 截止 2024-03-01 重構 *//* 設置顏色 */)、已失效代碼殘留、個人簽名檔CSS 註釋是隱性知識顯性化的載體。將註釋從「事後補充」升級為「設計先行的契約文檔」,配合工具鏈自動化,能讓樣式代碼庫保持長期的可理解性、可維護性與可演進性。