// 設計令牌
$color-primary: #1f605b;
$color-primary-light: lighten($color-primary, 15%);
$space-unit: 8px;
$bp-tablet: 768px;
$font-stack-system: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
// 全站引用
.btn-primary { background: $color-primary; }
.card { padding: $space-unit * 2; }
@media (min-width: $bp-tablet) { .container { max-width: 720px; } }
body { font-family: $font-stack-system; }
效益:修改一行變數,全站同步更新;設計師調整設計稿,工程師只改變數檔;杜絕「二十種藍色」散落代碼庫。
.card {
padding: $space-md;
border-radius: 8px;
&__header {
display: flex; justify-content: space-between;
h3 { margin: 0; font-size: 1.25rem; }
}
&__body { line-height: 1.6; }
&__footer {
margin-top: $space-md;
padding-top: $space-sm;
border-top: 1px solid #eee;
}
&.is-elevated { box-shadow: 0 8px 24px rgba(0,0,0,.15); }
}
效益:HTML 結構與 CSS 結構同構,易讀、易維護、易重構;避免 BEM 手寫冗長類名。
@mixin flex-center($direction: row) {
display: flex; align-items: center; justify-content: center; flex-direction: $direction;
}
@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
border-radius: $radius;
}
@mixin respond-to($bp) {
@if $bp == mobile { @media (max-width: $bp-mobile) { @content; } }
@else if $bp == tablet { @media (min-width: $bp-tablet) { @content; } }
@else if $bp == desktop { @media (min-width: $bp-desktop) { @content; } }
}
// 使用
.btn { @include flex-center; @include border-radius(999px); }
.card { @include respond-to(tablet) { padding: $space-lg; } }
效益:封裝瀏覽器前綴、響應式斷點、複雜視覺模式;參數化實現極致復用。
// styles/
// ├── _tokens.scss // 設計令牌 (色彩、間距、字體、斷點、Z-index)
// ├── _mixins.scss // 共用混入
// ├── _functions.scss // 自定義函數
// ├── _reset.scss // Normalize/Reboot
// ├── base/
// │ ├── _typography.scss
// │ └── _forms.scss
// ├── components/
// │ ├── _button.scss
// │ ├── _card.scss
// │ └── _modal.scss
// ├── layout/
// │ ├── _header.scss
// │ ├── _footer.scss
// │ └── _grid.scss
// └── main.scss // 入口
// main.scss
@use 'tokens' as *; // 無命名空間直接用變數
@use 'mixins' as *;
@use 'functions' as *;
@use 'reset';
@use 'base/typography';
@use 'base/forms';
@use 'components/button' as btn;
@use 'components/card' as card;
@use 'layout/header';
@use 'layout/footer';
@use 'layout/grid';
效益:命名空間隔離、無重複編譯、私有成員 (以 -/_ 開頭)、明確依賴圖、支援 Tree Shaking (配合現代打包器)。
calc() 編譯時計算、unit()、percentage()、random() (SASS 內建)。@if/@else、@for/@each/@while 產生重複樣式 (如網格類、圖標類)。lighten/darken、mix、adjust-hue、saturate/desaturate、opacity、rgba() 解構重組。&:.btn { &:hover { ... } &.is-loading { ... } }、.parent & { ... } (嵌套上下文引用父)。%placeholder:不輸出 CSS 的靜默類別,專供 @extend 使用,避免汙染輸出。| 特性 | SASS (SCSS) | Less | Stylus | PostCSS | CSS Custom Properties |
|---|---|---|---|---|---|
| 成熟度/生態 | ★★★★★ | ★★★★☆ | ★★★☆☆ | ★★★★★ (插件生態) | ★★★★★ (原生) |
| 語法親和力 | CSS 超集 | CSS 超集 | 縮排/無符號 | 標準 CSS + 插件語法 | 原生 |
| 邏輯/控制流 | 豐富 (@if/@for/@each) | 基本 (@if/@each) | 豐富 | 需插件 (postcss-conditionals) | 無 (僅 @media/@supports) |
| 模組系統 | @use/@forward (新) | @import | @import/@require | ESM import | @import / 模組腳本 |
| 運行時 | 編譯時 | 編譯時/運行時 | 編譯時 | 建置時 | **瀏覽器運行時** |
| 動態主題切換 | 需重新編譯/多套變數 | 同 | 同 | 同 | **原生支援 (JS 修改變數即時生效)** |
// SASS 編譯時:設計令牌、邏輯、Mixin、模組化
$color-primary: #1f605b;
:root {
// 輸出為 CSS 變數,支援運行時動態主題切換
--color-primary: #{$color-primary};
--color-primary-rgb: 31, 96, 91; // 供 rgba(var(--color-primary-rgb), 0.1) 使用
}
// 元件使用 CSS 變數
.btn-primary {
background: var(--color-primary);
color: white;
@include respond-to(mobile) { width: 100%; }
}
結合 SASS 編譯時工程化能力 (變數、邏輯、模組、Mixin) 與 CSS 變數運行時動態性 (主題切換、用戶偏好、CSS-in-JS 互通),是目前大型前端專案最推薦的樣式架構策略。