萬盛學電腦網

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

CSS 多重聲明語法規範:分號分隔、換行縮排、單行 vs 多行風格選擇指南

多重聲明核心語法

CSS 規則塊內可包含多個聲明,**分號 `;` 為分隔符** (非結束符):**

selector {
  property-1: value-1;
  property-2: value-2;
  property-3: value-3;
  /* 最後一條聲明可省略分號,但強烈建議保留 */
}

為什麼建議每行保留分號?

場景無尾隨分號風險有尾隨分號
新增屬性忘記在前一行加分號 → 語法錯誤、後續失效直接新增行,無風險
刪除屬性刪除最後一行 → 新最後一行缺分號無影響
移動行順序移動後可能缺分號無影響
Prettier/格式化可能產生非預期合併穩定輸出
壓縮工具需智能處理標準輸入

單行 vs 多行聲明風格

多行展開式 (推薦:屬性 ≥ 3、團隊協作、需註解)

.card {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 24px;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  background: white;
  box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1);
}

/* 相關屬性分組 + 空行分隔 */
.card {
  /* Layout */
  display: flex;
  flex-direction: column;
  gap: 16px;
  
  /* Box */
  padding: 24px;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  
  /* Visual */
  background: white;
  box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1);
}

單行緊湊式 (適用:屬性 ≤ 2、工具類、高頻複用)

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
.d-flex { display: flex; }
.flex-1 { flex: 1; }
.text-center { text-align: center; }
.mt-4 { margin-top: 1rem; }

混合策略:專案級規範

/* 規則:
   1. 元件/頁面層級選擇器:多行展開,相關屬性分組
   2. 工具類/原子類:單行
   3. 變數定義:單行分組
   4. @media/@supports 內部:遵循外層風格
*/

聲明排序規範 (提升可讀性與維護性)

推薦順序:濾波器 → 佈局 → 盒模型 → 視覺 → 文字 → 其他

.example {
  /* 1. 濾波器/混合模式 (影響合成) */
  isolation: isolate;
  mix-blend-mode: multiply;
  filter: drop-shadow(0 4px 6px rgb(0 0 0 / 0.1));
  
  /* 2. 定位/顯示/佈局 */
  position: relative;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 16px;
  
  /* 3. 盒模型 */
  width: 100%;
  max-width: 400px;
  padding: 24px;
  margin: 0 auto;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  
  /* 4. 背景/邊框/陰影 (視覺) */
  background: white;
  box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
  
  /* 5. 文字/字體 */
  font-family: var(--font-family-base);
  font-size: 1rem;
  line-height: 1.6;
  color: #111827;
  
  /* 6. 其他 */
  transition: box-shadow 0.2s ease;
  cursor: pointer;
}

Stylelint 排序規則配置

/* .stylelintrc.js */
{
  "plugins": ["stylelint-order"],
  "rules": {
    "order/order": [
      "custom-properties",
      "dollar-variables",
      "at-rules",
      "declarations"
    ],
    "order/properties-order": [
      /* Layout */
      "position", "z-index", "top", "right", "bottom", "left",
      "display", "flex", "flex-flow", "flex-direction", "flex-wrap",
      "justify-content", "align-items", "align-content", "gap",
      "grid", "grid-template", "grid-area",
      "float", "clear",
      /* Box Model */
      "width", "min-width", "max-width",
      "height", "min-height", "max-height",
      "margin", "margin-top", "margin-right", "margin-bottom", "margin-left",
      "padding", "padding-top", "padding-right", "padding-bottom", "padding-left",
      "box-sizing",
      /* Visual */
      "border", "border-radius", "background", "box-shadow",
      "opacity", "transform", "filter", "backdrop-filter",
      /* Typography */
      "font", "font-family", "font-size", "font-weight", "line-height",
      "color", "text-align", "text-decoration", "white-space",
      /* Other */
      "transition", "animation", "cursor", "pointer-events"
    ],
    "order/alphabetical": false
  }
}

註解最佳實踐

/* ========================================
   Section: Card Component
   ======================================== */

.card {
  /* Layout: Flex column with gap */
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  
  /* Box: Padding, border, radius */
  padding: var(--space-6);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  
  /* Visual: Background, shadow */
  background: var(--color-bg);
  box-shadow: var(--shadow-md);
  
  /* State: Hover elevation */
  transition: box-shadow var(--transition-base), transform var(--transition-fast);
}

.card:hover {
  box-shadow: var(--shadow-lg);
  transform: translateY(-2px);
}

/* Modifier: Featured card */
.card--featured {
  border-color: var(--color-primary);
  background: linear-gradient(135deg, var(--color-primary-50), var(--color-bg));
}

總結:可讀性黃金法則

  1. **尾隨分號永不省略** — 防止維護錯誤
  2. **屬性 ≥ 3 用多行**,≤ 2 可單行
  3. **相關屬性分組 + 空行分隔** — 認知區塊化
  4. **統一聲明排序** — 團隊配置 Stylelint Order 強制
  5. **關鍵邏輯加註解** — 為什麼這樣寫、什麼情況覆蓋
  6. **工具類單一職責** — 單行、語意明確、高優先權
copyright © 萬盛學電腦網 all rights reserved