各瀏覽器對 margin、padding、標題字級、清單樣式的預設值都不一樣,這是版面在不同瀏覽器裡歪掉的最主要原因。先把這些預設樣式清乾淨,後續寫樣式才有可預期的一致性。以下是一份可直接取用的重置樣式。
@charset "utf-8";
body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, pre,
dl, dt, dd, ul, ol, li, th, td, div, span, img,
fieldset, lengend, button, input, select, textarea {
margin: 0;
padding: 0;
}
/* color & background */
html { color: #000; background: #FFF; }
/* font */
body, button, input, select, textarea { /* for ie */
font: 12px/18px Arial, Verdana, "Microsoft YaHei";
}
/* fix center */
html, body { height: 100%; margin: 0 auto; }
/* h1~h6 */
h1 { font-size: 18px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
h4, h5, h6 { font-size: 100%; }
/* a */
a { text-decoration: none; }
a:hover { text-decoration: underline; }
/* list */
ul, ol { list-style: none; }
/* img border */
fieldset, img { border: 0; }
/* table */
table { border-collapse: collapse; border-spacing: 0; font: inherit; }
/* margin */
.alpha { margin-left: 0; }
.omega { margin-right: 0; }
/* float & clear */
.left { float: left; }
.right { float: right; }
.clear { clear: both; display: block; }
/* clearfix */
.clearfix:after { content: "."; display: block; }
.clearfix { display: inline-table; }
/* Hides from IE-mac */
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* End hide from IE-mac */
*+html .clearfix { min-height: 1%; } /* IE7 */
幾點使用提醒:這份重置是相對溫和的版本,只歸零與統一,不預設太多觀點;重置之後,所有樣式都要自己寫,因此建議搭配自己常用的基底 class(如 clearfix、.left/.right)一起維護。若使用 normalize.css 這類「保留有用預設、只修掉 bug」的方案,也是不錯的替代路線,兩者理念不同但目的相同:讓你在意的是設計,而不是瀏覽器預設值。