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小時內與您取得聯系。