現在的網站設計中使用reset.css用重置整個站點的標簽的CSS屬性的做法很常見,但有時候我們已經為了reset而reset,我們經常看到這樣的reset代碼
其實大部分CSS reset是沒必要的,多寫了只會增加瀏覽器在渲染頁面是的負擔,當然有同學會說CSS reset還是有其意義的,雖然不得承認這一點,但是我們可以通過了解一些標簽的CSS屬性的默認值來避免過度的reset。
為了避免不重復使用reset,得熟悉標簽屬性默認值,
大部分的CSS reset都是針對padding、border、margin,下圖是chrome的常用標簽的這三個屬性的默認值。
文/丁向明
做一個有博客的web前端自媒體人,專注web前端開發,關注用戶體驗,加我qq/微信交流:6135833
http://dingxiangming.com
HTML標簽在瀏覽器里有默認的樣式,例如 p 標簽有上下邊距,strong標簽有字體加粗樣式,em標簽有字體傾斜樣式。不同瀏覽器的默認樣式之間也會有差別,例如ul默認帶有縮進的樣式,在IE下,它的縮進是通過margin實現的,而Firefox下,它的縮進是由padding實現的。在切換頁面的時候,瀏覽器的默認樣式往往會給我們帶來麻煩,影響開發效率。所以解決的方法就是一開始就將瀏覽器的默認樣式全部去掉,更準確說就是通過重新定義標簽樣式。"覆蓋"瀏覽器的CSS默認屬性。最最簡單的說法就是把瀏覽器提供的默認樣式覆蓋掉!這就是CSS reset。
CSS reset 可以將瀏覽器默認的樣式清除掉,這樣做會使我們的 CSS 或 html 標簽更加方便準確。假如我們不初始化 CSS 樣式屬性,將會增大 CSS 代碼量,所以使用初始化文件會為我們節約網頁代碼,節約網頁下載時間;還會使得我們開發網頁內容時更加方便簡潔,不用考慮很多。為什么我們不建議使用通配符?
我們有的時候會使用* {padding:0;margin:0;}來清除瀏覽器的一些樣式,這種方式很實用,*號匹配所有元素,省去了一個一個寫元素名稱的麻煩。但是它在性能方面是會對頁面造成影響的,試想一下,你的頁面非常豐富,嵌套多、元素多,這時候,*號會去每一個元素都去渲染一遍。而我們通常只需要對表格、列表、標題等標簽進行初始化樣式。
愛的JAVA醬們: 更多技術交流者或想獲取JAVA資料請掃描二維碼加微信(858568103)
為 Web 開發人員,我們希望用戶無論使用哪種瀏覽器瀏覽網頁,HTML 元素的外觀都是一樣的。遺憾的是,由于瀏覽器的運行方式,情況并非如此。每個瀏覽器都有自己的默認樣式表,其中規定了一些適用于元素的最基本規則。在大多數情況下,這些默認樣式是有用的。但是,由于這些默認值的不同,有時會在特定瀏覽器中出現表現不一致的現象,影響用戶的瀏覽體驗。
為了避免這些不一致,CSS Reset 由此而來。本文先用部分章節介紹 CSS Reset的歷史,后面重點介紹推薦的 Modern CSS Reset。
本節按時間線來整體回顧 CSS Reset 的發展歷程。
最基本的全局重置方法是使用通配符選擇器將所有元素的 padding 和 margins 重置為零。根據瀏覽大多數網站 CSS 樣式得出的結論,下面應該是最常用的 CSS 重置方法,簡單粗暴。
* {
padding: 0;
margin: 0;
}
2007 年,根據杰夫-斯塔爾(Jeff Starr)收集的大量不同的 CSS 重置方法。最早的應該是 Tantek ?elik 的 undohtml.css(http://tantek.com/log/2004/undohtml.css)。Tantek 的 CSS Reset 可移除一些比較突出和不需要的默認瀏覽器樣式。特別是,它能去除鏈接和點擊鏈接上通常不受歡迎的下劃線和邊框,消除大多數塊元素的邊距和填充,甚至將通用字體大小設為 1em。
Tips:從樣式中可以看到 CSS Reset 沒有包含對 HTML5 新增的語義化標簽。
/* undohtml.css */
/* (CC) 2004 Tantek Celik. Some Rights Reserved. */
/* http://creativecommons.org/licenses/by/2.0 */
/* This style sheet is licensed under a Creative Commons License. */
/* Purpose: undo some of the default styling of common (X)HTML browsers */
/* link underlines tend to make hypertext less readable,
because underlines obscure the shapes of the lower halves of words */
:link,:visited { text-decoration:none }
/* no list-markers by default, since lists are used more often for semantics */
ul,ol { list-style:none }
/* avoid browser default inconsistent heading font-sizes */
/* and pre/code too */
h1,h2,h3,h4,h5,h6,pre,code { font-size:1em; }
/* remove the inconsistent (among browsers) default ul,ol padding or margin */
/* the default spacing on headings does not match nor align with
normal interline spacing at all, so let's get rid of it. */
/* zero out the spacing around pre, form, body, html, p, blockquote as well */
/* form elements are oddly inconsistent, and not quite CSS emulatable. */
/* nonetheless strip their margin and padding as well */
ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,body,html,p,blockquote,fieldset,input
{ margin:0; padding:0 }
/* whoever thought blue linked image borders were a good idea? */
a img,:link img,:visited img { border:none }
/* de-italicize address */
address { font-style:normal }
/* more varnish stripping as necessary... */
Eric Meyer 的 CSS Reset 應該是當前最流行的。它的內容與 Tantek 的不同(包含了一些新增的 HTML5 元素),但實現目的都一樣:移除默認樣式。您可能會認出這個著名的代碼塊,它在開發者的 DevTools 風格面板中隨處可見。該 CSS Reset的優點是簡單易用,但是它也有一些缺點,例如會影響一些網頁元素的默認樣式,例如 form 元素。
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
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, 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,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
多年后,隨著 HTML5 變得越來越真實,Richard Clark的 HTML5 Reset等重置技術也開始流行起來。它仍然是Meyer Reset的修改版,保留了Meyer Reset 重置的精神。
Richard Clark 的 HTML5 Reset 在 Meyer Reset的基礎上新增了 mark、hr、ins等元素的樣式重置,同時指定了 input、select 等表單元素垂直居中。
/*
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com
Twitter: @rich_clark
*/
html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
margin:0;
padding:0;
border:0;
outline:0;
font-size:100%;
vertical-align:baseline;
background:transparent;
}
body {
line-height:1;
}
article,aside,details,figcaption,figure,
footer,header,hgroup,menu,nav,section {
display:block;
}
nav ul {
list-style:none;
}
blockquote, q {
quotes:none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content:'';
content:none;
}
a {
margin:0;
padding:0;
font-size:100%;
vertical-align:baseline;
background:transparent;
}
/* change colours to suit your needs */
ins {
background-color:#ff9;
color:#000;
text-decoration:none;
}
/* change colours to suit your needs */
mark {
background-color:#ff9;
color:#000;
font-style:italic;
font-weight:bold;
}
del {
text-decoration: line-through;
}
abbr[title], dfn[title] {
border-bottom:1px dotted;
cursor:help;
}
table {
border-collapse:collapse;
border-spacing:0;
}
/* change border colour to suit your needs */
hr {
display:block;
height:1px;
border:0;
border-top:1px solid #cccccc;
margin:1em 0;
padding:0;
}
input, select {
vertical-align:middle;
}
normalize.css 代表了 CSS Reset 第一個有意義的轉變,來感受下它的與眾不同:
例如下面這部分代碼,用完備的注釋來說明包含此段 CSS Reset 的原因。
/**
* 1. Remove the bottom border in Chrome 57-
* 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
*/
abbr[title] {
border-bottom: none; /* 1 */
text-decoration: underline; /* 2 */
text-decoration: underline dotted; /* 2 */
}
當前,normalize.css 很受開發者的喜愛,它已經發布了 8.0.1 版本,超過51K個 Github stars。但是最近圈子里又出現了 Modern CSS Reset 很受開發者的喜愛。
2023年9月18日,安迪-貝爾(Andy Bell)--英國設計師、前端開發員和設計公司創始人,發布了一篇博文介紹了Modern CSS Reset。 整個 Reset 的源碼比較簡單:
/* Box sizing rules */
*,
*::before,
*::after {
box-sizing: border-box;
}
/* Prevent font size inflation */
html {
-moz-text-size-adjust: none;
-webkit-text-size-adjust: none;
text-size-adjust: none;
}
/* Remove default margin in favour of better control in authored CSS */
body, h1, h2, h3, h4, p,
figure, blockquote, dl, dd {
margin: 0;
}
/* Remove list styles on ul, ol elements with a list role, which suggests default styling will be removed */
ul[role='list'],
ol[role='list'] {
list-style: none;
}
/* Set core body defaults */
body {
min-height: 100vh;
line-height: 1.5;
}
/* Set shorter line heights on headings and interactive elements */
h1, h2, h3, h4,
button, input, label {
line-height: 1.1;
}
/* Balance text wrapping on headings */
h1, h2,
h3, h4 {
text-wrap: balance;
}
/* A elements that don't have a class get default styles */
a:not([class]) {
text-decoration-skip-ink: auto;
color: currentColor;
}
/* Make images easier to work with */
img,
picture {
max-width: 100%;
display: block;
}
/* Inherit fonts for inputs and buttons */
input, button,
textarea, select {
font: inherit;
}
/* Make sure textareas without a rows attribute are not tiny */
textarea:not([rows]) {
min-height: 10em;
}
/* Anything that has been anchored to should have extra scroll margin */
:target {
scroll-margin-block: 5ex;
}
通讀了 Modern CSS Reset 之后,發現一些比較有意思的點,分析如下:
1)盒模型重置:
/* Box sizing rules */
*,
*::before,
*::after {
box-sizing: border-box;
}
這條規則將所有元素和偽元素設置為 border-box,而不是默認的 content-box。現在我們更注重讓瀏覽器通過靈活的布局、流動的字體和空間來完成更多工作,因此這條規則已經不像以前那么有用了。·
border-box 和 content-box 是 box-sizing 屬性的兩個值。與 content-box 不同,border-box 值表示元素的尺寸還包括邊框和填充。
2)列表樣式重置:
/* Remove list styles on ul, ol elements with a list role, which suggests default styling will be removed */
ul[role='list'],
ol[role='list'] {
list-style: none;
}
增加按條件的列表樣式重構,不至于覆蓋全量列表,按需重置。
3)body樣式重置:
/* Set core body defaults */
body {
min-height: 100vh;
line-height: 1.5;
}
對 body 元素,引入了現代單位 vh,并設置了最小高度為當前視口高度。
4)標題樣式重置:
/* Balance text wrapping on headings */
h1, h2,
h3, h4 {
text-wrap: balance;
}
這條規比較特殊,但新的 text-wrap 屬性會讓標題看起來很可愛。可能部分UI同學會覺得這不合適,所以你可能想刪除這一條。
4)標題樣式重置:
/* Anything that has been anchored to should have extra scroll margin */
:target {
scroll-margin-block: 5ex;
}
最后,如果一個元素是錨定的,那么用滾動邊距在其上方增加一點空間是有意義的,只有當該元素是目標元素時才會考慮到滾動邊距。這只是一個小小的調整,卻能帶來很多用戶體驗!如果你有一個固定的頁眉,可能需要調整一下。
沒有哪種 CSS Reset 最好,選擇哪種取決于我們的項目需求和個人偏好。
如果你在項目中盡量多的保留默認樣式,小懶在這里強烈推薦你選擇 normalize.css,如果你更傾向于完全自定義樣式,Meyer 的 CSS Reset 可能更適合你,如果你想改變默認的盒模型計算方式,box-sizing: border-box 是一個不錯的選擇。
在選擇任何 CSS Reset 方案之前,請確保你深刻理解其工作原理,并充分測試其在你的項目中的效果。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。