DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
CSS Reset 方案對比(上):Eric Meyer、Tantek 與 Montoya
編輯:CSS詳解     

Eric Meyer 的 reset 是流傳最廣、被抄得最多的一份。本文把三套經典腳本並列,順便指出它們各自的取向。

Eric Meyer Reset CSS

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: "" ""; }

取向說明:所有字型屬性都用 inherit 而非直接歸零,語意是「交給父層決定」;body 明確給出白底黑字,避免使用者自訂樣式造成不可預期結果;表格用 separate 而非 collapse,保留各瀏覽器一致的邊框行為。

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

它的哲學是「只處理真正有問題的」,因此連 font-weight 都沒動。好處是檔案極小、副作用少;缺點是 reset 不夠徹底,遇到差異較大的場景仍需補充。

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

這套的風格是「保留合理預設」。標題與段落仍保有 1em 的上下間距、清單項目仍有縮排,只是把數值統一。相比起全面歸零,它讓你在 reset 之後不必重新定義每一個元素的間距,適合不想從零開始建立樣式系統的專案。

三套方案的取向光譜

把 Tantek、Montoya、Eric Meyer 排在一起,剛好是一條從「極簡」到「徹底」的光譜:Tantek 只修明顯的坑;Montoya 保留並統一合理預設;Eric Meyer 全部歸零、完全掌控。

選哪一種取決於你的工作流程。若已有完整設計系統,Eric Meyer 的空白的畫布最合適;若是中小型專案、想少寫一些樣式,Montoya 的折衷更省力。

實務提醒:Eric Meyer 這套中的 :focus{outline:0} 會移除鍵盤操作時的焦點框,對無障礙是明確的傷害。若採用這份 reset,請務必補上 :focus-visible 的替代樣式,或乾脆把這一行刪掉。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved