/* 盒模型:順時針 上右下左 */
margin: 10px 20px 15px 5px; /* 四值 */
margin: 10px 20px 15px; /* 三值:上、左右、下 */
margin: 10px 20px; /* 兩值:上下、左右 */
margin: 10px; /* 一值:四邊 */
/* 邊框 */
border: 1px solid #333; /* width style color */
/* 背景 */
background: #f00 url(bg.png) no-repeat fixed 50% 50%; /* color image repeat attachment position */
/* 字體 —— 必須包含 font-size 與 font-family */
font: italic small-caps bold 1em/1.5 "Microsoft YaHei", sans-serif;
/* 列表 */
list-style: square inside url(bullet.png);
/* ✅ 正確 */
margin: 0; padding: 10px 0; line-height: 1.5;
/* ❌ 錯誤 */
margin: 0px; padding: 10px 0px; line-height: 1.5px;
選擇器、屬性名、屬性值(十六進制顏色、關鍵字)統一小寫,避免 XHTML/嚴格模式下的匹配陷阱。
/* ❌ 冗余 */
div#header { } fieldset.details { }
/* ✅ 精簡 */
#header { } .details { }
ID 全局唯一,Class 可復用,標簽限定只會增加優先級計算開銷。
/* 現代輕量級重置 */
*, *::before, *::after {
box-sizing: border-box;
margin: 0; padding: 0;
}
img, video, iframe { display: block; max-width: 100%; }
ul, ol { list-style: none; }
a { text-decoration: none; color: inherit; }
color, font-family, line-height, text-align, visibility 等屬性天然可繼承。在 body 統一定義,子元素僅覆蓋差異值。
/* 權重計算:內聯(1000) > ID(100) > Class/屬性/偽類(10) > 標簽/偽元素(1) > 繼承(0) */
p { color: #333; } /* 權重 1 */
.content p { color: #666; } /* 權重 11 */
#main .content p { color: #999; } /* 權重 111 */
善用 類選擇器 控制權重,避免 ID 濫用與 !important 滥用。
<button class="btn btn-primary btn-lg"></button>
.btn { padding: .5em 1em; border-radius: 4px; }
.btn-primary { background: #007bff; color: #fff; }
.btn-lg { padding: .75em 1.5em; font-size: 1.25rem; }
/* ❌ 扁平化 class 暴露結構耦合 */
.nav-item { } .nav-link { } .nav-icon { }
/* ✅ 結構選擇器封裝 */
.nav { }
.nav > li { } /* 直接子元素 */
.nav a { } /* 任意後代鏈接 */
.nav .active > a { } /* 狀態組合 */
background: url(images/bg.png) center / cover no-repeat;
引號在某些舊瀏覽器會觸發解析錯誤,無引號最穩健。
h1, h2, h3, .heading { font-family: "PingFang SC", sans-serif; color: #1a1a1a; }
h1 { font-size: 2.5rem; }
h2 { font-size: 2rem; }
a:link { color: #0066cc; } /* 未訪問 */
a:visited { color: #663399; } /* 已訪問 */
a:hover { color: #ff3300; text-decoration: underline; } /* 懸停 */
a:active { color: #cc0000; } /* 點擊瞬間 */
/* 鍵盤可訪問性增強 */
a:focus { outline: 2px solid #0066cc; outline-offset: 2px; }
a:focus:not(:focus-visible) { outline: none; } /* 現代瀏覽器僅鍵盤聚焦顯示 */
| 方案 | 代碼 | 優/缺點 |
|---|---|---|
空標簽 <br class="clear"> | .clear{clear:both} | 語義污染、結構耦合 |
| Overflow 法 | .wrap{overflow:hidden} | 簡潔,但會剪切陰影/絕對定位溢出 |
| 偽元素法(推薦) | .clearfix::after{content:"";display:table;clear:both} | 無副作用、語義純淨、兼容 IE8+ |
/* 塊級定寬 */
.wrap { width: 800px; margin-inline: auto; }
/* Flex 容器 */
.parent { display: flex; justify-content: center; }
/* Grid 容器 */
.parent { display: grid; place-items: center; }
/* 絕對定位 */
.child { position: absolute; left: 50%; transform: translateX(-50%); }
/* 屬性級 Hack(僅示例,生產慎用) */
.selector { _property: value; } /* IE6 */
.selector { *property: value; } /* IE7 */
.selector { property: value\9; } /* IE8/9 */
/* 條件註釋(HTML 層面,更安全) */
<!--[if IE]><link rel="stylesheet" href="ie.css"><![endif]-->
<!--[if gt IE 8]><link rel="stylesheet" href="modern.css"><![endif]-->
.debug * { outline: 1px solid rgba(255,0,0,.3); } /* 不影響佈局 */
.debug *::before { content: attr(class); font-size: 10px; color: red; }
優先用 outline 而非 border,不佔空間、不觸發回流。
.selector-1,
.selector-2 {
property-1: value-1;
property-2: value-2;
/* 每條聲明獨立一行,冒號後一空格,分號結尾 */
}
.card {
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,.1);
color: #333;
display: flex;
flex-direction: column;
margin: 0 auto;
max-width: 600px;
padding: 24px;
}
同屬性多次出現會相鄰,易發現覆蓋錯誤;Git Diff 噪音最小。
| 反模式 | 影響 | 替代方案 |
|---|---|---|
通配符重置 *{margin:0;padding:0} | 遍歷所有元素,首屏渲染延遲 | 針對性重置列表標簽 |
後代選擇器濫用 .nav li a | 向上遍歷 DOM 樹,匹配開銷大 | 直接類選擇器 .nav-link |
標簽+類 div.card | 增加優先級計算,無語義增益 | 純類 .card |
濫用 !important | 破壞層疊上下文,維護地獄 | 重構優先級、BEM 命名 |
過濾器 filter:alpha(opacity=50) | IE 專有,GPU 加速失效 | opacity: .5 或 rgba() |
| 1px 背景圖平鋪 | 重繪次數爆炸 | 最小 8px+ 或 CSS 漸變 |
遵循這 18 條鐵律,配合 Stylelint/PostCSS 自動化檢查,可將 CSS 體積壓縮 30%+,首屏渲染提速 15%+,團隊維護成本大幅降低。