DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
15 套 CSS Reset 範例:從極簡到完整的重設方案
編輯:CSS詳解     

在當前的網頁開發流程中,用 CSS 為語意化的 (X)HTML 標記加上樣式,是絕對的核心工作。設計師夢想中的世界是這樣的:所有瀏覽器都完整理解並一致呈現每一條 CSS 規則,沒有任何相容性問題。

但我們顯然不在那個世界裡。同一段 CSS,在不同瀏覽器下經常有著不同的解釋與呈現。

問題從哪裡來

Firefox、Opera、Internet Explorer、Chrome、Safari 這些主流瀏覽器,各自以些微不同的方式理解 CSS 規範。當設計師沒有明確定義某個屬性時,瀏覽器就會動用自己的預設值來補上——而這些預設值彼此並不一致。結果就是同一份程式碼,在 A 瀏覽器上完全符合設計稿,在 B 瀏覽器上卻走了樣。更麻煩的是,有些瀏覽器甚至會直接忽略某些宣告。

什麼是 CSS Reset

CSS Reset(樣式重設,也有人稱為 CSS 復位、默認樣式重置)就是為了解決這件事而生的。由於各家瀏覽器對 CSS 的初始值詮釋不同,設計師未定義的屬性會被各自填入不同的預設值;因此我們先寫一段樣式,把所有元素的起點統一,後續的樣式才有共同的基準線。

以下整理 15 套 CSS Reset 方案,從最極簡的三行版本,到考慮周全的完整版本都有。你可以直接選一套用,也可以把它們當素材,最後剪出一份真正屬於自己的重設樣式表。

一、最極簡版本

* {
    padding: 0;
    margin: 0;
}

最普遍、最簡單的重設:把全站元素的 padding 與 margin 歸零,避開各家瀏覽器在這兩個屬性預設值上的分歧。

* {
    padding: 0;
    margin: 0;
    border: 0;
}

再加上 border 歸零。初始值為 0 同樣能消除一批惱人的差異。

* {
    outline: 0;
    padding: 0;
    margin: 0;
    border: 0;
}

再補上 outline,避免焦點框帶來的意外樣式。三個版本層層遞進,你可以依專案需要停在任一階段。

二、濃縮實用型

* {
    vertical-align: baseline;
    font-weight: inherit;
    font-family: inherit;
    font-style: inherit;
    font-size: 100%;
    outline: 0;
    padding: 0;
    margin: 0;
    border: 0;
}

出自 Perishable Press,是作者長期使用的版本。除了歸零,還把字型相關屬性改為繼承,讓字型控制集中在 body 一處,不必逐個元素重設。

三、Poor Man's Reset

html, body {
    padding: 0;
    margin: 0;
}
html {
    font-size: 1em;
}
body {
    font-size: 100%;
}
a img, :link img, :visited img {
    border: 0px;
}

只處理 html 與 body 的 padding、margin,分別設定兩者的字級基準,最後把連結圖片的預設邊框拿掉——這是早期最常見的一類惱人問題。

四、Siolon's Global Reset

* {
    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;
}

在全域歸零之後,重新給標題、段落、清單一組合理的垂直節奏,並把表格的邊框合併打開。屬於「歸零後立刻重建基礎排版」的思路。

五、Shaun Inman's Global Reset

body, div, dl, dt, dd, ul, ol, li,
h1, h2, h3, h4, h5, h6, pre, form, fieldset, input, p, blockquote,
table, th, td, embed, object {
    padding: 0;
    margin: 0;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
fieldset, img, abbr { border: 0; }
address, caption, cite, code, dfn, em,
h1, h2, h3, h4, h5, h6, strong, th, var {
    font-weight: normal;
    font-style: normal;
}
ul { list-style: none; }
caption, th { text-align: left; }
h1, h2, h3, h4, h5, h6 { font-size: 1.0em; }
q:before, q:after { content: ""; }
a, ins { text-decoration: none; }

涵蓋面更廣的版本,把標題與強調元素的粗體斜體一併取消,讓所有視覺層級都必須重新定義——好處是徹底,代價是你要記得把 與 的樣式補回來。

六、Yahoo (YUI) CSS Reset

body, div, dl, dt, dd, ul, ol, li,
h1, h2, h3, h4, h5, h6, pre,
form, fieldset, input, textarea, p, blockquote, th, td {
    padding: 0;
    margin: 0;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
fieldset, img { border: 0; }
address, caption, cite, code, dfn, em, strong, th, var {
    font-weight: normal;
    font-style: normal;
}
ol, ul { list-style: none; }
caption, th { text-align: left; }
h1, h2, h3, h4, h5, h6 {
    font-weight: normal;
    font-size: 100%;
}
q:before, q:after { content: ""; }
abbr, acronym { border: 0; }

Yahoo 官方維護的版本,也是最多人使用的一套。它當年通常搭配 YUI 的基礎字型樣式表一起載入,形成完整的「重設 + 字型基準」組合。

七、Eric Meyer's CSS Reset

html, body, div, span, applet, object, iframe, table, caption,
tbody, tfoot, thead, tr, th, td, del, dfn, em, font, img, ins,
kbd, q, s, samp, small, strike, strong, sub, sup, tt, var,
h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr,
acronym, address, big, cite, code, dl, dt, dd, ol, ul, li,
fieldset, form, label, legend {
    vertical-align: baseline;
    font-family: inherit;
    font-weight: inherit;
    font-style: inherit;
    font-size: 100%;
    outline: 0;
    padding: 0;
    margin: 0;
    border: 0;
}
:focus { outline: 0; }
body {
    background: white;
    line-height: 1;
    color: black;
}
ol, ul { list-style: none; }
table {
    border-collapse: separate;
    border-spacing: 0;
}
caption, th, td {
    font-weight: normal;
    text-align: left;
}
blockquote:before, blockquote:after, q:before, q:after { content: ""; }
blockquote, q { quotes: "" ""; }

影響力最大的一套。Meyer 把幾乎所有 HTML 元素列舉出來統一歸零,並設定 body 的基礎色彩與行高。他自己也強調:這份檔案該被拿來「讀懂並改造」,而不是整包丟進專案裡從不改動。

八、Condensed Meyer Reset

body, div, dl, dt, dd, ul, ol, li,
h1, h2, h3, h4, h5, h6,
pre, form, fieldset, input, textarea, p, blockquote, th, td {
    padding: 0;
    margin: 0;
}
fieldset, img { border: 0; }
table {
    border-collapse: collapse;
    border-spacing: 0;
}
ol, ul { list-style: none; }
address, caption, cite, code, dfn, em, strong, th, var {
    font-weight: normal;
    font-style: normal;
}
caption, th { text-align: left; }
h1, h2, h3, h4, h5, h6 {
    font-weight: normal;
    font-size: 100%;
}
q:before, q:after { content: ""; }
abbr, acronym { border: 0; }

Meyer 版本的精簡改良:拿掉逐個屬性的繼承設定,只保留真正會造成跨瀏覽器差異的部分,檔案更小、副作用更少。

九、Ateneu Popular 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;
}

這一套特別照顧了表單元素:讓 input、textarea、select、button 繼承字型設定,並把 hr 改成用背景色畫出的 1px 細線。表單樣式跨瀏覽器不一致的問題,一直是重設時最容易被漏掉的一塊。

十、Chris Poteet's Reset CSS

* {
    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;
}

用全域選取器搭配一組重建後的基礎間距,程式碼量很小,適合從零開始、想自己掌控每個數值的小型專案。

十一、Tantek Celik Reset CSS

:link, :visited { text-decoration: none }
ul, ol { list-style: none }
h1, h2, h3, h4, h5, h6, pre, code { font-size: 1em; }
ul, ol, li, h1, h2, h3, h4, h5, h6, pre, form, body, html, p, blockquote, fieldset, input {
    margin: 0;
    padding: 0;
}
a img, :link img, :visited img { border: none }
address { font-style: normal }

極度精簡的版本,只有五行宣告。哲學很清楚:只重設那些真的會造成麻煩的項目,其餘交給瀏覽器。程式碼越少,維護成本越低。

十二、Christian Montoya Reset CSS

html, body, form, fieldset {
    margin: 0;
    padding: 0;
    font: 100%/120% Verdana, Arial, Helvetica, sans-serif;
}
h1, h2, h3, h4, h5, h6, p, pre,
blockquote, ul, ol, dl, address {
    margin: 1em 0;
    padding: 0;
}
li, dd, blockquote { margin-left: 1em; }
form label { cursor: pointer; }
fieldset { border: none; }
input, select, textarea {
    font-size: 100%;
    font-family: inherit;
}

除了歸零,還直接定下全站字型與 120% 的行高,並為表單標籤加上手型游標。屬於「重設的同時就把設計決策做完」的風格。

十三、Rudeworks Reset CSS

* {
    margin: 0;
    padding: 0;
    border: none;
}
html {
    font: 62.5% "Lucida Grande", Lucida, Verdana, sans-serif;
    text-shadow: #000 0px 0px 0px;
}
ul { list-style: none; list-style-type: none; }
h1, h2, h3, h4, h5, h6, p, pre,
blockquote, ul, ol, dl, address {
    font-weight: normal;
    margin: 0 0 1em 0;
}
cite, em, dfn { font-style: italic; }
sup {
    position: relative;
    bottom: 0.3em;
    vertical-align: baseline;
}
sub {
    position: relative;
    bottom: -0.2em;
    vertical-align: baseline;
}
li, dd, blockquote { margin-left: 1em; }
code, kbd, samp, pre, tt, var, input[type='text'], textarea {
    font-size: 100%;
    font-family: monaco, "Lucida Console", Courier, mono-space;
}
del { text-decoration: line-through; }
ins, dfn { border-bottom: 1px solid #ccc; }
small, sup, sub { font-size: 85%; }
abbr, acronym {
    text-transform: uppercase;
    font-size: 85%;
    letter-spacing: .1em;
    border-bottom-style: dotted;
    border-bottom-width: 1px;
}
a abbr, a acronym { border: none; }
sup { vertical-align: super; }
sub { vertical-align: sub; }
h1 { font-size: 2em; }
h2 { font-size: 1.8em; }
h3 { font-size: 1.6em; }
h4 { font-size: 1.4em; }
h5 { font-size: 1.2em; }
h6 { font-size: 1em; }
a, a:link, a:visited, a:hover, a:active {
    outline: 0;
    text-decoration: none;
}
a img { border: none; text-decoration: none; }
img { border: none; text-decoration: none; }
label, button { cursor: pointer; }
input:focus, select:focus, textarea:focus { background-color: #FFF; }
fieldset { border: none; }
.clear { clear: both; }
.float-left { float: left; }
.float-right { float: right; }
body { text-align: center; }
#wrapper {
    margin: 0 auto;
    text-align: left;
}

這已經不只是重設,而是「重設 + 基礎排版 + 工具類別」的整合方案。62.5% 的字級設定讓 1em 等於 10px,換算特別直觀;body 置中加 #wrapper 還原的做法,則是舊 IE 時代實現水平置中的經典手法。上標下標也改用 position 微調,比單靠 vertical-align 更可控。

十四、Anieto2K Reset CSS

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,
center, u, b, i {
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font-weight: normal;
    font-style: normal;
    font-size: 100%;
    font-family: inherit;
    vertical-align: baseline
}
body { line-height: 1 }
:focus { outline: 0 }
ol, ul { list-style: none }
table {
    border-collapse: collapse;
    border-spacing: 0
}
blockquote:before, blockquote:after, q:before, q:after { content: "" }
blockquote, q { quotes: "" "" }
input, textarea {
    margin: 0;
    padding: 0
}
hr {
    margin: 0;
    padding: 0;
    border: 0;
    color: #000;
    background-color: #000;
    height: 1px
}

列舉範圍比 Meyer 版本更廣,連 center、u、b、i 這類常被忽略的元素都納入。適合需要極致一致性的老舊專案。

十五、CSSLab 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; }
table {
    border-collapse: separate;
    border-spacing: 0;
}
caption, th, td {
    text-align: left;
    font-weight: normal;
}
a img, iframe { border: none; }
ol, ul { list-style: none; }
input, textarea, select, button {
    font-size: 100%;
    font-family: inherit;
}
select { margin: inherit; }

/* Fixes incorrect placement of numbers in ol's in IE6/7 */
ol { margin-left: 2em; }

/* == clearfix == */
.clearfix:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}
.clearfix { display: inline-block; }
* html .clearfix { height: 1%; }
.clearfix { display: block; }

這一套的附加價值在細節:針對 IE6/7 有序列表編號位置錯亂的 bug 補上 margin-left,並附了一段完整的 clearfix。清浮動是布局中反覆出現的問題,把它放進重設檔裡隨手可用,是很實際的決定。

最後:與其挑一套,不如長出一套

這 15 套方案手法各異,目的卻高度一致——讓更多瀏覽器從同一條起跑線出發,呈現同樣的視覺結果。拿來當參考能省下不少摸索時間,但畢竟都是別人為自己的專案寫的。

更值得做的是理解每一條宣告「在防禦什麼問題」,然後依你實際要支援的瀏覽器範圍與設計需求,留該留的、砍該砍的。一份為自己量身裁剪的重設檔,通常比任何一套現成方案都更好用。

補充一個現實考量:這些方案成文於瀏覽器大戰的年代,其中大量 hack(例如 * html 這種只有 IE6 讀得懂的选择器)在今天已無意義。現代專案的重設重點,已經從「對抗各家預設值」轉向「建立一致的設計基準」——歸零的項目變少,重新定義字型尺度、行高與色彩變數的比例變高。理解這段演進,你就能判斷哪些程式碼該留、哪些該丟。

Copyright © DIV+CSS佈局教程網 All Rights Reserved