萬盛學電腦網

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

CSS屬性書寫順序規範:從瀏覽器前綴到邏輯屬性的現代最佳實踐

為什麼需要屬性書寫順序規範?

  • 可讀性:統一順序降低認知負荷,Code Review 專注邏輯而非格式
  • 可維護性:相關屬性聚合,修改時不易遺漏
  • 工具鏈友好:Stylelint/Prettier 自動修復,CI 攔截不合規代碼
  • 團隊協作:新成員零適應成本,歷史代碼統一風格

推薦順序:濃縮版(基於 CSScomb / Idiomatic CSS / Stylelint-order)

.selector {
  /* 1. 佈局與定位:決定元素在文檔流中的位置與佔據空間 */
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  float: left;           /* 避免使用,僅列示順序 */
  clear: both;

  /* 2. 盒模型:尺寸、邊距、邊框、背景 */
  width: 100%;
  max-width: 400px;
  height: auto;
  aspect-ratio: 16/9;
  margin: 0 auto;
  padding: 16px;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: var(--color-bg);
  box-shadow: var(--shadow-md);
  overflow: hidden;

  /* 3. 字體與文本:內容渲染樣式 */
  font-family: var(--font-sans);
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: var(--color-text);
  text-align: left;
  text-decoration: none;
  white-space: nowrap;
  text-overflow: ellipsis;

  /* 4. 視覺效果:變換、動畫、過渡、濾鏡 */
  transform: translateZ(0);
  transition: all 0.2s ease;
  animation: fadeIn 0.3s;
  opacity: 1;
  filter: none;
  backdrop-filter: blur(8px);
  clip-path: none;

  /* 5. 交互與偽類:狀態變化 */
  cursor: pointer;
  user-select: none;
  pointer-events: auto;

  /* 6. 列表、表格、其他 */
  list-style: none;
  table-layout: fixed;
  border-collapse: collapse;

  /* 7. SVG 專用 */
  fill: currentColor;
  stroke: currentColor;
  stroke-width: 1;
}

現代 CSS 書寫順序工具鏈自動化

Stylelint 配置(推薦)

// .stylelintrc.js
module.exports = {
  extends: ['stylelint-config-standard', 'stylelint-config-recess-order'],
  rules: {
    'order/properties-alphabetical-order': false,  // 使用邏輯分組而非字母序
    'order/order': [
      'custom-properties',      // CSS 變量優先
      'dollar-variables',       // Sass 變量
      'at-rules',               // @media, @supports 等
      'declarations',           // 普通屬性,按以下分組序
      {
        type: 'at-rule',
        name: 'media',
        hasBlock: true
      }
    ],
    // 自定義分組順序(需 stylelint-order 6.0+)
    'order/properties-order': [
      // 1. 佈局定位
      'position', 'top', 'right', 'bottom', 'left', 'inset',
      'z-index', 'display', 'flex*', 'grid*', 'float', 'clear',
      // 2. 盒模型
      'width', 'min-width', 'max-width', 'height', 'min-height', 'max-height',
      'aspect-ratio', 'margin', 'margin-*', 'padding', 'padding-*',
      'border', 'border-*', 'border-radius', 'box-shadow',
      'background', 'background-*', 'overflow', 'overflow-*',
      // 3. 字體文本
      'font', 'font-*', 'line-height', 'color', 'text-*', 'letter-spacing',
      'word-*', 'white-space', 'text-overflow', 'direction', 'unicode-bidi',
      // 4. 視覺效果
      'transform', 'transform-*', 'transition', 'transition-*',
      'animation', 'animation-*', 'opacity', 'filter', 'backdrop-filter',
      'clip', 'clip-path', 'mask', 'mask-*', 'shape-*',
      // 5. 交互
      'cursor', 'pointer-events', 'user-select', 'resize', 'scroll-*',
      // 6. 其他
      'list-style', 'counter-*', 'table-layout', 'border-collapse',
      'empty-cells', 'caption-side', 'vertical-align', 'align-*', 'justify-*',
      'place-*', 'gap', 'row-gap', 'column-gap',
      // 7. SVG
      'fill', 'stroke', 'stroke-*', 'marker*', 'paint-order', 'vector-effect'
    ]
  }
};

Prettier + CSS/SCSS 格式化

// .prettierrc
{
  "cssDeclarationSorter": true,  // 需安裝 prettier-plugin-css-order 或使用 css-declaration-sorter
  "singleQuote": true,
  "trailingComma": "none"
}

特殊場景處理

1. 邏輯屬性優先(新項目強制)

.box {
  /* ✅ 邏輯屬性在前,物理屬性作為降級或不寫 */
  margin-inline: auto;
  padding-block: 16px;
  border-inline-start: 1px solid;
  border-block-end: 2px dashed;
  
  /* ❌ 避免混用物理屬性,除非兼容極舊瀏覽器 */
  /* margin-left: auto; margin-right: auto; */
}

2. CSS 變量聲明在最前

:root {
  --color-primary: #1890ff;
  --space-md: 16px;
}

.component {
  --component-bg: var(--color-bg);  /* 組件級變量 */
  --component-padding: var(--space-md);
  
  background: var(--component-bg);
  padding: var(--component-padding);
}

3. @media / @supports / @container 內部保持相同順序

.card {
  padding: 16px;
  @media (min-width: 768px) {
    padding: 24px;        /* 同層級屬性順序不變 */
    display: flex;
    gap: 16px;
  }
  @container (min-width: 400px) {
    flex-direction: row;
  }
}

遷移現有代碼庫

  1. 安裝工具:npm i -D stylelint stylelint-config-recess-order stylelint-order
  2. 運行自動修復:npx stylelint "**/*.{css,scss,vue}" --fix
  3. 納入 CI:"lint:css": "stylelint \"**/*.{css,scss,vue}\""
  4. IDE 集成:VSCode 安裝 Stylelint 插件,保存自動格式化

統一的屬性書寫順序不是「強迫症」,而是工程化協作的基礎設施。建議團隊在專案啟動時確立規範,接入自動化工具鏈,將格式爭議從 Code Review 中徹底移除。

copyright © 萬盛學電腦網 all rights reserved