* { margin:0; padding:0; }?通配符重置雖然簡潔,但存在三大弊端:
input、button、select 等表單元素失去原生外觀,需額外恢復,得不償失。font)會被意外繼承,導致難以排查的樣式異常。僅針對確有瀏覽器差異的元素重置,兼顧效能與可控性。以下為現代化精簡版 Reset:
body, h1, h2, h3, h4, h5, h6, hr, p,
blockquote, dl, dt, dd, ul, ol, li,
pre, form, fieldset, legend, button, input, textarea,
th, td, img {
margin: 0;
padding: 0;
border: 0;
}
body, button, input, select, textarea {
font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
}
h1, h2, h3, h4, h5, h6 { font-size: 100%; font-weight: normal; }
em, i { font-style: normal; }
ul, ol { list-style: none; }
a { text-decoration: none; color: inherit; }
a:hover { text-decoration: underline; }
table { border-collapse: collapse; border-spacing: 0; }
img { vertical-align: middle; max-width: 100%; height: auto; }
若專案需保留瀏覽器預設樣式(如表單原生外觀、標題層級視覺差異),建議直接引入 Normalize.css。它:
/* 安裝 */ npm install normalize.css
/* 引入 */ @import 'normalize.css';
package-lock.json)。box-sizing: border-box)。科學的 Reset 是穩健樣式架構的基石,選對工具、用對方式,讓跨瀏覽器兼容不再是噩夢。