现在的网站设计中使用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小时内与您取得联系。