由於各大瀏覽器存在相容性差異,同一個 CSS 屬性在不同瀏覽器下的表現未必相同。有經驗的前端設計師,通常都會自訂一份用來重置瀏覽器預設樣式的 CSS 檔案,讓各瀏覽器的最終呈現保持一致。大家最熟悉的莫過於 * { margin:0 0 },不過這只是最簡單的相容性處理,一般情況下僅靠它並不足夠。
以下整理了幾組前端網站常用的 reset.css 程式碼供你參考。你可以直接複製、存成 reset.css 檔案,然後在每個頁面中引入使用。
body,ul,li,p,h1,h2,h3,h4,h5,h6,img,br,hr,table,tr,td,dl,dt,dd,form {
margin: 0;
padding: 0;
}
body {
font-family: Arial,"微軟雅黑";
font-size: 12px;
color: #434343;
}
.clear {
clear: both;
font-size: 0px;
}
ul,li {
list-style: none;
}
img {
border: none;
}
/*一般鏈接*/
a {
text-decoration: none;
color: #555;
}
a:hover {
color: #3366ff;
}
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
background: transparent;
border: 0;
margin: 0;
padding: 0;
vertical-align: baseline;
font-size: 100%;
font: inherit;
-webkit-text-size-adjust: none;
}
body {
line-height: 1;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
object, :focus {
outline: none;
}
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
display: block;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after, q:before, q:after {
content: '';
content: none;
}
a img {
border: none;
}
input,button,textarea,select,optgroup,option {
font-size: 100%;
font: inherit;
}
.al { text-align: left; }
.ar { text-align: right; }
.ac { text-align: center; }
.lc { margin: 0 auto; }
.fl, .il .fl { float: left; }
.fr, .il .fr { float: right; }
.fc, .il .fc { float: none; clear: both; }
.rel { position: relative; }
.abs { position: absolute; }
.il { list-style: none; }
.il li { float: left; }
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, 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;
}
body {
line-height: 1;
color: black;
background: white;
}
ol, ul {
list-style: none;
}
table {
border-collapse: separate;
border-spacing: 0;
}
caption, th, td {
text-align: left;
font-weight: normal;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: "";
content: none;
}
blockquote, q {
quotes: "" "";
}
以上三組 reset.css 各有側重,你可以依專案需求挑選合適的一份,或從中擷取需要的規則加以調整。如果確定用不到某個選擇器的定義,也可以放心將它刪除,讓樣式表保持精簡。