* { box-sizing: border-box; margin: 0; padding: 0; }box-sizing: border-box 全局生效display: flow-root / Flex/Grid 容器/* ❌ 反模式:表現類名、ID 樣式、深層嵌套 */
#header .nav ul li a { color: #333; }
.red-text { color: red; }
.left-sidebar { float: left; width: 200px; }
/* ✅ 最佳實踐:語義化 BEM + 扁平選擇器 */
.c-header { /* 頁首組件 */ }
.c-header__nav { /* 導航區塊 */ }
.c-header__link { /* 導航鏈接 */ }
.c-header__link--active { /* 活躍狀態 */ }
/* 頁面級狀態控制 */
.page-home .c-hero { background: var(--hero-home); }
.theme-dark .c-card { background: var(--color-surface-dark); }
.logged-in .c-nav__auth { display: none; }
/* 目錄結構 */
src/styles/
├── 0-settings/ /* 設計令牌:_colors.scss, _spacing.scss, _tokens.scss */
├── 1-tools/ /* Mixins/Functions: _mixins.scss, _functions.scss */
├── 2-generic/ /* Reset/Normalize: _reset.scss, _normalize.scss */
├── 3-base/ /* 裸標籤: _typography.scss, _forms.scss, _links.scss */
├── 4-objects/ /* 佈局模式: _container.scss, _grid.scss, _media.scss */
├── 5-components/ /* 業務組件: _button.scss, _card.scss, _modal.scss */
├── 6-utilities/ /* 高優先級工具: _spacing.scss, _visibility.scss */
└── 7-overrides/ /* 最後手段: _legacy.scss, _third-party.scss */
/* main.scss 導入順序 = 優先級遞增順序 */
@use 'settings/*' as *;
@use 'tools/*';
@forward 'generic/reset';
@forward 'base/*';
@forward 'objects/*';
@forward 'components/*';
@forward 'utilities/*';
@forward 'overrides/*';
/* 統一盒模型:現代標準 */
*, *::before, *::after { box-sizing: border-box; }
/* 歷史對比 (僅供考古) */
.IE5-box { width: 200px; padding: 20px; border: 5px solid; }
/* IE5/Quirks: 可見寬度 200px (含 padding+border) */
/* Standard: 可見寬度 250px (200 + 40 + 10) */
/* 現代處理:父級 padding / 子級 margin 替代盒模型計算 */
.card { padding: 1.5rem; } /* 內部間距 */
.card + .card { margin-top: 1.5rem; } /* 外部間距 */
/* 相對定位:視覺偏移,佔位保留 */
.relative-box { position: relative; top: 10px; left: 20px; /* 原位置保留空間 */ }
/* 絕對定位:脫離流,相對最近非 static 祖先 */
.absolute-box { position: absolute; top: 0; right: 0; }
/* 固定定位:相對視口,滾動不動 */
.fixed-header { position: fixed; top: 0; left: 0; right: 0; z-index: 1000; }
/* 黏性定位:滾動到閾值固定 */
.sticky-nav { position: sticky; top: 0; z-index: 100; }
/* Float 僅用於文字環繞 */
.article-img { float: left; margin-right: 1rem; shape-outside: circle(); }
/* background-position: 20% 30% */
/* 含義:圖片 20% 30% 的點 對齊 容器 20% 30% 的點 */
/* 0% 0% = 左上角對齊 */
/* 100% 100% = 右下角對齊 */
/* 50% 50% = 中心點對齊 (居中) */
/* 實戰:圖片焦點控制 */
.hero { background-position: 50% 30%; } /* 人物臉部通常在上 1/3 */
.avatar { background-position: center; } /* 頭像居中 */
/* 經典滑動門:左右兩圖片拼接適應寬度 */
/* 現代方案 1:Border-image (單圖九宮格) */
.btn {
border: 6px solid transparent;
border-image: url(btn-frame.svg) 6 6 6 6 stretch stretch;
border-image-slice: 6 fill;
}
/* 現代方案 2:多背景漸層 */
.btn {
background:
linear-gradient(90deg, #007bff, #0056b3) left / 12px 100% no-repeat, /* 左圓角 */
linear-gradient(90deg, #0056b3, #007bff) right / 12px 100% no-repeat, /* 右圓角 */
#007bff center / calc(100% - 24px) 100% no-repeat; /* 中間拉伸 */
}
/* 現代方案 3:CSS 變數 + 單背景 */
.btn {
--_l: 12px; --_r: 12px;
background:
radial-gradient(circle at var(--_l) 50%, transparent 99%, #007bff 100%) left,
radial-gradient(circle at calc(100% - var(--_r)) 50%, transparent 99%, #007bff 100%) right,
#007bff;
background-repeat: no-repeat;
background-size: var(--_l) 100%, var(--_r) 100%, calc(100% - var(--_l) - var(--_r)) 100%;
}
/* 完美居中:現代三大法寶 */
.center-flex { display: flex; justify-content: center; align-items: center; }
.center-grid { display: grid; place-items: center; }
.center-abs { position: absolute; inset: 0; margin: auto; width: fit-content; height: fit-content; }
/* 圖標系統:Sprite → SVG Symbol → Icon Font → Inline SVG (現代首選) */
/* SVG Symbol 用法 */
<svg style="display:none">
<symbol id="icon-user" viewBox="0 0 24 24">...</symbol>
</svg>
<svg class="icon"><use xlink:href="#icon-user"></use></svg>
.icon { width: 1em; height: 1em; vertical-align: -0.125em; fill: currentColor; }
| 維度 | 檢查項 | 工具 |
|---|---|---|
| 語義化 | HTML5 標籤正確使用、ARIA 屬性完整 | axe-core, html-validate |
| 架構 | ITCSS 層級遵循、無循環依賴、無 @import 運行時 | Stylelint, Madge |
| 性能 | Critical CSS 內聯、未用樣式清理、字體子集化 | Penthouse, PurgeCSS, fonttools |
| 維護性 | 設計令牌覆蓋率 100%、無魔法數字、組件文檔完整 | Storybook, Style Dictionary |
| 兼容性 | Browserslist 策略、Polyfill 按需、優雅降級 | PostCSS Preset Env, Browserslist |
這 15 條原則構成了現代 CSS 工程化的基石,從語義化 HTML 到設計系統落地,每一條都是經過大規模生產驗證的最佳實踐。