寫過一輪 CSS Reset 之後,你大概會想自己動手做一套——畢竟每個人的需求與手感都不同。以下兩套可以當作參考起點,剛好代表兩種不同做法。
html, body, div, span, applet, object, iframe, h1, h2, h3,
h4, h5, h6, p, blockquote, pre, a, abbr, acronym,
address, big, cite, code, del, dfn, em, font, img, ins,
kbd, q, s, samp, small, strike, strong, sub, sup, tt,
var, dl, dt, dd, ol, ul, li, fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-weight: inherit;
font-style: inherit;
font-size: 100%;
font-family: inherit;
vertical-align: baseline;
}
:focus { outline: 0; }
a, a:link, a:visited, a:hover, a:active { text-decoration: none; }
table { border-collapse: separate; border-spacing: 0; }
th, td { text-align: left; font-weight: normal; }
img, iframe { border: none; text-decoration: none; }
ol, ul { list-style: none; }
input, textarea, select, button {
font-size: 100%;
font-family: inherit;
}
select { margin: inherit; }
hr {
margin: 0;
padding: 0;
border: 0;
color: #000;
background-color: #000;
height: 1px;
}
這套的特徵是「選擇器清單長、但每條規則都克制」——不做額外的排版重建,只把差異歸零。特別值得一提的是它把 button 也納入表單元素的字型重置,這是很多人會漏掉的一項,漏了之後按鈕在 Safari 與 Mac 上會長得跟頁面其他部分格格不入。
* {
vertical-align: baseline;
font-family: inherit;
font-style: inherit;
font-size: 100%;
border: none;
padding: 0;
margin: 0;
}
body { padding: 5px; }
h1, h2, h3, h4, h5, h6, p, pre, blockquote, form, ul, ol, dl {
margin: 20px 0;
}
li, dd, blockquote { margin-left: 40px; }
table {
border-collapse: collapse;
border-spacing: 0;
}
這套的哲學完全相反:用萬用選擇器 * 一口氣歸零,然後「把合理的值加回來」。段落間距 20px、清單縮排 40px,都是刻意保留的排版節奏。
Ateneu 的方式是「從零開始」,reset 之後畫布是空白的,所有間距都得自己定。好處是完全可控,適合有設計系統的團隊;缺點是每個新專案都要重複定義一遍基礎排版。
Chris Poteet 的方式是「歸零後補回合理值」,reset 完就能直接寫內容,畫面已有基本呼吸感。缺點是萬用選擇器 * 的效能成本較高,且會連不該被重設的元素一起動到。
還有一個常被忽略的成本:萬用選擇器在大型 DOM 上的樣式計算開銷明顯高於具體選擇器。對於像 Yahoo 這種規模的網站,這差異會被放大成實際的載入時間,因此大型專案多半傾向精簡過的 reset,而非全面歸零。
如果打算自製,起點建議這樣:先用 Normalize.css 當底(它修正差異而非歸零,副作用最小),再疊上你自己真正需要的重設,最後補 box-sizing: border-box 統一盒模型計算。這樣的組合比任何單一 reset 腳本都好維護,也更容易說清楚每一行為什麼存在。