SS Reset是指样式重置,主要用于清除浏览器里面的默认样式,有利于页面布局。以下总结了一些常见网站使用的默认样式,供大家参考。
html {
overflow-x:auto;
overflow-y:scroll;
}
body, dl, dt, dd, ul, ol, li, pre, form, fieldset, input, p, blockquote, th, td {
font-weight:400;
margin:0;
padding:0;
}
h1, h2, h3, h4, h4, h5 {
margin:0;
padding:0;
}
body {
background-color:#FFFFFF;
color:#666666;
font-family:Helvetica,Arial,sans-serif;
font-size:12px;
padding:0 10px;
text-align:left;
}
select {
font-size:12px;
}
table {
border-collapse:collapse;
}
fieldset, img {
border:0 none;
}
fieldset {
margin:0;
padding:0;
}
fieldset p {
margin:0;
padding:0 0 0 8px;
}
legend {
display:none;
}
address, caption, em, strong, th, i {
font-style:normal;
font-weight:400;
}
table caption {
margin-left:-1px;
}
hr {
border-bottom:1px solid #FFFFFF;
border-top:1px solid #E4E4E4;
border-width:1px 0;
clear:both;
height:2px;
margin:5px 0;
overflow:hidden;
}
ol, ul {
list-style-image:none;
list-style-position:outside;
list-style-type:none;
}
caption, th {
text-align:left;
}
q:before, q:after, blockquote:before, blockquote:after {
content:””;
}
body,ol,ul,h 1 ,h 2 ,h 3 ,h 4 ,h 5 ,h 6 ,p,th,td,dl,dd,form,fieldset,legend,input,textarea,
select{
margin:0;
padding:0
}
body{
font : 12px "宋体" , "Arial Narrow" ,HELVETICA;
background:#fff;
-webkit-text-size-adjust: 100%
}
a{
color : #172c45;
text-decoration:none
}
a:hover{
color : #cd0200;
text-decoration:underline
}
em{
font-style:normal
}
li{
list-style:none
}
img{
border:0 ;
vertical-align:middle
}
table{
border-collapse:collapse ;
border-spacing:0 }
p{word-wrap:break-word}
body {
font-family:arial,helvetica,sans-serif;
font-size:13px;
font-size-adjust:none;
font-stretch:normal;
font-style:normal;
font-variant:normal;
font-weight:normal;
line-height:1.4;
text-align:center;
}
body, ul, ol, dl, dd, h1, h2, h3, h4, h5, h6, p, form, fieldset, legend, input, textarea, select, button, th, td {
margin:0;
padding:0;
}
h1, h2, h3, h4, h5, h6 {
font-size:100%;
font-weight:normal;
}
table {
font-size:inherit;
}
input, select {
font-family:arial,helvetica,clean,sans-serif;
font-size:100%;
font-size-adjust:none;
font-stretch:normal;
font-style:normal;
font-variant:normal;
font-weight:normal;
line-height:normal;
}
button {
overflow:visible;
}
th, em, strong, b, address, cite {
font-style:normal;
font-weight:normal;
}
li {
list-style-image:none;
list-style-position:outside;
list-style-type:none;
}
img, fieldset {
border:0 none;
}
ins {
text-decoration:none;
}
html,body,ul,li,ol,dl,dd,dt,p,h1,h2,h3,h4,h5,h6,form,fieldset,legend,img {
margin: 0;
padding: 0
}
fieldset,img {
border: 0
}
img {
display: block
}
address,caption,cite,code,dfn,th,var {
font-style: normal;
font-weight: normal
}
ul,ol {
list-style: none
}
input {
padding-top: 0;
padding-bottom: 0;
font-family: "SimSun","宋体"
}
input::-moz-focus-inner {
border: 0;
padding: 0
}
select,input {
vertical-align: middle
}
select,input,textarea {
font-size: 12px;
margin: 0
}
input[type="text"],input[type="password"],textarea {
outline-style: none;
-webkit-appearance: none
}
textarea {
resize: none
}
table {
border-collapse: collapse
}
* {
margin: 0;
padding: 0
}
em,i {
font-style: normal
}
li {
list-style: none
}
img {
border: 0;
vertical-align: middle
}
button {
cursor: pointer
}
a {
color: #666;
text-decoration: none
}
a:hover {
color: #c81623
}
为 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 方案之前,请确保你深刻理解其工作原理,并充分测试其在你的项目中的效果。
天使用了HTML中表单里的input属性submit,可以提交表单的值,其中name为区分名称的属性,value为提交字段的值。接下来我们说说reset的用法。
先看看效果:
页面点击重制选项后,form表单里填写的内容都清空了
来看看代码是怎么写的:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>男儿当自强</title>
</head>
<body>
<input type="text" name="Hello" value="test">
语文<input type="checkbox" name="">
<br>
<b>洛阳亲友如相问,一片冰心在玉壶</b>
<form>
用户名: <input type="text" name="username" value="请输入用户名"> <br>
密码: <input type="password" name="password" value="请输入密码"> <br>
性别: 男<input type="radio" name="sex" value="man" checked="checked"> 女<input type="radio" name="sex" value="woman"> <br>
爱好: 武术<input type="checkbox" name="checkbox" value="武术" checked="checked"> 唱歌<input type="checkbox" name="checkbox" value="唱歌"> 跳舞<input type="checkbox" name="checkbox" value="跳舞"> <br>
重置按钮: <input type="reset" name="reset" value="重制选项">
</form>
</body>
</html>
还有2个属性可以说说:
button和file
先看下效果:
然后点击试下,并看看代码:
file可以打开本地文件夹:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>男儿当自强</title>
</head>
<body>
<input type="text" name="Hello" value="test">
语文<input type="checkbox" name="">
<br>
<b>洛阳亲友如相问,一片冰心在玉壶</b>
<form>
用户名: <input type="text" name="username" value="请输入用户名"> <br>
密码: <input type="password" name="password" value="请输入密码"> <br>
性别: 男<input type="radio" name="sex" value="man" checked="checked"> 女<input type="radio" name="sex" value="woman"> <br>
爱好: 武术<input type="checkbox" name="checkbox" value="武术" checked="checked"> 唱歌<input type="checkbox" name="checkbox" value="唱歌"> 跳舞<input type="checkbox" name="checkbox" value="跳舞"> <br>
重置按钮: <input type="reset" name="reset" value="重制选项"><br>
按钮: <input type="button" name="button" value="请输入验证码"> <br>
上传文件: <input type="file" name="file" value="请上传文件"> <br>
</form>
</body>
</html>
大家掌握了么,接下来就开始标签的学习了。今天先到这里
*请认真填写需求信息,我们会在24小时内与您取得联系。