萬盛學電腦網

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

CSS 代碼規範十二條軍規:從 Reset 到壓縮部署的專業實踐

專業級 CSS 代碼規範:可維護、高效能、團隊協作標準

混亂的 CSS 是技術債務溫床。遵循統一規範能大幅降低維護成本、提升渲染效能、保障團隊協作順暢。以下十二條軍規涵蓋 Reset、命名、縮寫、繼承、選擇器、註解、排序、可讀性、屬性值選擇、載入方式、表達式、壓縮部署。

1. 使用 Reset,但拒絕全域通配符 Reset

反模式:

* { margin: 0; padding: 0; } /* 慢、過度、破壞表單/替換元素原生樣式 */

推薦:針對性重置常用元素(參考 Normalize.css / Eric Meyer Reset / YUI Reset):

/*** 1. Reset 區塊 ***/
body, h1, h2, h3, h4, h5, h6, hr, p, blockquote,
dl, dt, dd, ul, ol, li,
pre, form, fieldset, legend, button, input, textarea,
th, td, img {
  margin: 0; padding: 0; border: 0;
}

/*** 2. 基礎字體 ***/
body, button, input, select, textarea {
  font: 14px/1.5 "Microsoft JhengHei", "PingFang TC", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
h1, h2, h3, h4, h5, h6 { font-size: 100%; font-weight: normal; }
em { font-style: normal; }

/*** 3. 列表/連結/圖片/表格 ***/
ul, ol { list-style: none; }
a { text-decoration: none; color: #333; }
a:hover { text-decoration: underline; color: #f40; }
img { border: 0; vertical-align: middle; }
table { border-collapse: collapse; border-spacing: 0; }

2. 語義化命名:拒絕表象、擁抱結構

反模式:.red { color: red; } → 後改藍色變 .red { color: blue; }(語義崩壞)
反模式:.leftBar { float: left; } → 後移右側變 .leftBar { float: right; }(命名誤導)

推薦:以結構/功能/模組命名:

#header, #nav, #main, #sidebar, #footer
.module-news, .module-product, .widget-search
.btn-primary, .btn-secondary, .alert-success
.is-active, .is-hidden, .has-error

工具類(Utility Class)可用表象命名,因為不會變更:

.text-center { text-align: center; }
.float-left { float: left; }
.clearfix::after { content: ""; display: table; clear: both; }

3. 熟練縮寫屬性:精簡代碼、提升可讀性

/*** 展開寫法 ***/
li {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 1.2em;
  line-height: 1.4em;
  padding-top: 5px;
  padding-bottom: 10px;
  padding-left: 5px;
}

/*** 縮寫寫法 ***/
li {
  font: 1.2em/1.4em Arial, Helvetica, sans-serif;
  padding: 5px 0 10px 5px; /* 順時針:上 右 下 左 */
}

常用縮寫屬性:margin、padding、border、font、background、flex、grid、顏色值 #RGB / #RRGGBB。

4. 善用 CSS 繼承:減少重複宣告

/*** 重複宣告 ***/
#container li { font-family: Georgia, serif; }
#container p  { font-family: Georgia, serif; }
#container h1 { font-family: Georgia, serif; }

/*** 繼承寫法 ***/
#container { font-family: Georgia, serif; } /* 子元素自動繼承 */

可繼承屬性:color、font-*、line-height、text-*、visibility、list-style 等。

5. 合併群組選擇器:消除重複規則集

/*** 分散 ***/
h1 { font-family: Arial; font-weight: normal; }
h2 { font-family: Arial; font-weight: normal; }
h3 { font-family: Arial; font-weight: normal; }

/*** 合併 ***/
h1, h2, h3 { font-family: Arial; font-weight: normal; }

6. 結構化註解:建立代碼地圖

/*------------------------------------
  目錄
  1. Reset & Base
  2. Layout (Header, Content, Sidebar, Footer)
  3. Modules (Buttons, Forms, Cards, Tables)
  4. Utilities
  5. Media Queries
  6. Print Styles
-----------------------------------*/

/*** 2. Layout - Header ***/
#header { height: 145px; position: relative; }
#header h1 { width: 324px; margin: 45px 0 0 20px; float: left; }

/*** 3. Modules - Button ***/
.btn { display: inline-block; padding: 8px 16px; border-radius: 4px; }
.btn-primary { background: #007bff; color: #fff; }

7. 屬性字母排序:極速定位修改

/*** 字母序 ***/
.card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,.1);
  color: #333;
  display: flex;
  flex-direction: column;
  margin: 16px;
  padding: 24px;
  width: 100%;
  z-index: 1;
}

團隊可共用 IDE 格式化規則(如 Stylelint + Prettier)自動強制排序。

8. 可讀性優先:多行展開 vs 單行緊湊

/*** 推薦:屬性多 → 多行展開 ***/
.complex-component {
  background-color: #f5f5f5;
  border: 1px solid #ddd;
  border-radius: 4px;
  display: flex;
  flex-wrap: wrap;
  margin: 10px;
  padding: 15px;
  width: 300px;
}

/*** 可接受:屬性少(≤3) → 單行 ***/
.simple-rule { color: #666; font-size: 14px; }
.icon { width: 16px; height: 16px; }

原則:統一風格 > 個人偏好。專案統一採用一種並配置 Lint 強制。

9. 選擇高效能屬性值

不推薦推薦理由
border: 0;border: none;0 仍觸發 border-width/color 渲染;none 完全跳過
visibility: hiddendisplay: none前者保留版面空間與渲染資源
outline: 0outline: none同 border 邏輯

10. 取代 @import:平行載入、無阻塞

<!-- 推薦:平行下載,不阻塞渲染 -->
<link rel="stylesheet" href="css/main.css">

<!-- 不推薦:串行下載,延遲渲染 -->
<style> @import url("css/main.css"); </style>

@import 必須在 CSS 規則之前,且會阻塞後續樣式下載,嚴重影響首屏速度。

11. 外部樣式表:利用瀏覽器快取

內嵌 <style> 每次請求隨 HTML 重傳;外部 .css 可被瀏覽器快取、CDN 加速、並行下載。無特殊理由(如關鍵 CSS 內聯優化首屏),一律外部引入。

12. 避免 CSS Expression(IE 專屬惡夢)

/*** 毫秒級重算,嚴重拖垮效能 ***/
background-color: expression((new Date()).getHours() % 2 ? "#B8D4FF" : "#F08A00");

Expression 在滾動、縮放、移動滑鼠時瘋狂重算(輕鬆萬次/秒)。現代 CSS 已有 calc()、自訂屬性、Container Queries 等標準替代方案,**嚴禁使用**。

13. 部署前壓縮:YUI Compressor / cssnano / Lightning CSS

  • 移除註解、空白、換行
  • 合併重複規則、縮短顏色值(#ffffff → #fff)
  • 輸出 .min.css,配合 Cache-Control: max-age=31536000 長效快取
  • 建構流程整合(Vite/Webpack/Gulp/PostCSS)自動化

總結:規範即自由

這十二條規範不是束縛,而是讓團隊在「如何寫」上達成共識,將精力集中在「寫什麼」與「如何更好」。建議專案啟動即納入 .stylelintrc、.prettierrc,CI 流程強制檢查,讓規範成為肌肉記憶而非負擔。

copyright © 萬盛學電腦網 all rights reserved