萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

Web 前端開發 SASS 完全指南:變數、巢狀、Mixin 與模組化架構

為什麼選擇 SASS?

CSS 本身缺乏變數、巢狀、混入、繼承、模組化等程式語言特性,導致大型專案樣式碼重複率高、維護困難。SASS (Syntactically Awesome Style Sheets) 為 CSS 超集,編譯輸出標準 CSS,提供強大的抽象能力,讓樣式碼像程式碼一樣可維護、可複用、可擴展。

兩種語法風格

/* SCSS (Sassy CSS) - 主流、CSS 超集、大括號語法 */
$primary: #1f605b;
.button {
  background: $primary;
  &:hover { background: darken($primary, 10%); }
}

/* SASS (Indented Syntax) - 縮排語法、無大括號/分號 */
$primary: #1f605b
.button
  background: $primary
  &:hover
    background: darken($primary, 10%)

建議使用 SCSS,與 CSS 無縫切換,團隊協作門檻低。

核心功能實戰

1. 變數 - 設計系統基石

// 顏色系統
$color-primary: #1f605b;
$color-primary-light: lighten($color-primary, 15%);
$color-on-primary: #fff;

// 間距系統 (8px 基準)
$space-unit: 8px;
$space-xs: $space-unit * 0.5;  // 4px
$space-sm: $space-unit;        // 8px
$space-md: $space-unit * 2;    // 16px
$space-lg: $space-unit * 3;    // 24px

// 斷點
$bp-mobile: 480px;
$bp-tablet: 768px;
$bp-desktop: 1024px;

2. 巢狀 - 結構化樣式

.card {
  padding: $space-md;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,.1);

  &__header {
    display: flex;
    justify-content: space-between;
    margin-bottom: $space-sm;

    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); }
  &.is-outlined { border: 1px solid $color-primary; background: transparent; }
}

3. Mixin - 可複用樣式塊 (支援參數)

// 彈性盒子快捷混入
@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($breakpoint) {
  @if $breakpoint == mobile {
    @media (max-width: $bp-mobile) { @content; }
  } @else if $breakpoint == tablet {
    @media (max-width: $bp-tablet) { @content; }
  } @else if $breakpoint == desktop {
    @media (min-width: $bp-desktop) { @content; }
  }
}

// 使用範例
.btn {
  @include border-radius(999px); // 胶囊圆角
  @include flex-center;
  padding: $space-xs $space-md;

  @include respond-to(mobile) {
    width: 100%;
  }
}

4. @extend / %placeholder - 繼承與靜默類別

%btn-base {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: all .2s ease;
}

.btn-primary {
  @extend %btn-base;
  background: $color-primary;
  color: $color-on-primary;
}

.btn-secondary {
  @extend %btn-base;
  background: #e7f3ff;
  color: #1f605b;
}

5. 模組化架構 (Partials + @use/@forward)

// 檔案結構
styles/
├── _variables.scss    // 設計令牌
├── _mixins.scss       // 共用混入
├── _reset.scss        // 重置/正規化
├── _typography.scss   // 字體系統
├── components/
│   ├── _button.scss
│   ├── _card.scss
│   └── _modal.scss
├── layout/
│   ├── _header.scss
│   ├── _footer.scss
│   └── _grid.scss
└── main.scss          // 入口檔

// main.scss (SASS 新模組系統 @use)
@use 'variables' as *;    // 直接使用變數,無需命名空間
@use 'mixins' as *;
@use 'reset';
@use 'typography';
@use 'components/button' as btn;
@use 'components/card' as card;
@use 'layout/header';
@use 'layout/footer';
@use 'layout/grid';

建置整合

  • Vite:npm i -D sass 即可開箱支援。
  • Webpack:sass-loader + css-loader + style-loader/MiniCssExtractPlugin。
  • CLI:sass --watch src/styles:dist/css --style=compressed --source-map。

避坑指南

  1. 避免過深巢狀 (超過 3-4 層),產出的選擇器過長影響效能與專一度。
  2. 慎用 @extend 跨檔案繼承,易產生難以預測的選擇器組合;優先用 Mixin + 組合。
  3. 善用 @use 取代 @import (已棄用),享有命名空間、避免重複編譯、支援私有成員 (以 - 或 _ 開頭)。
  4. 顏色函數 (lighten, darken, mix) 在設計系統階段使用,避免運行時計算。

SASS 不只是 CSS 預處理器,更是構建可維護、可擴展設計系統的基礎設施。掌握其核心特性與模組化架構,能讓團隊樣式開發效能提升數倍。

copyright © 萬盛學電腦網 all rights reserved