以下整理了一份約定俗成的 CSS 命名規則,可作為程式碼規範化的參考,重點是提升可讀性與團隊協作效率。
(1)頁面結構
容器:container
頁頭:header
內容:content / container
頁面主體:main
頁尾:footer
導航:nav
側欄:sidebar
欄目:column
左右中:left / right / center
外圍整體寬度控制:wrapper
(2)導航
導航:nav
主導航:mainnav
子導航:subnav
頂導航:topnav
邊導航:sidebar
左導航:leftsidebar
右導航:rightsidebar
選單:menu
子選單:submenu
標題:title
摘要:summary
(3)功能模組
標志:logo 廣告:banner
登陸:login 登錄條:loginbar
注冊:register 搜索:search
功能區:shop 標題:title
加入:joinus 狀態:status
按鈕:btn 滾動:scroll
標籤頁:tab 文章列表:list
提示資訊:msg 當前:current
小技巧:tips 圖標:icon
註釋:note 指南:guide
服務:service 熱點:hot
新聞:news 下載:download
投票:vote 合作夥伴:partner
友情鏈接:link 版權:copyright
(1)顏色 — 直接用顏色的名稱或 16 進位色碼:
.red { color: red; }
.f60 { color: #f60; }
.ff8600 { color: #ff8600; }
(2)字體大小 — 用「font + 字級」:
.font12px { font-size: 12px; }
.font9pt { font-size: 9pt; }
(3)對齊樣式 — 用對齊方向的英文:
.left { float: left; }
.bottom { float: bottom; }
(4)標題欄樣式 — 用「類別 + 功能」:
.barnews { }
.barproduct { }
main01_div01_ul01,代表主體第一區塊下第一個 div 裡的第一個 ul。補充一點值得警惕的地方:把顏色、字級寫進 class 名(如 .red、.font12px)雖然直白,但一旦設計稿改版換色,class 名就會與實際樣式矛盾,維護時容易誤導。較穩妥的做法是以用途或角色命名,例如 .price、.text-muted,把「看起來怎樣」交給樣式決定。
主要:master.css
模塊:module.css
基本共用:base.css
主題:themes.css
專欄:columns.css
打印:print.css
文字:font.css
表單:forms.css
補丁:mend.css
版面:layout.css