Normalize.css makes browsers render all elements more consistently and in line with modern standards. It precisely targets only the styles that need normalizing.
就如上面說的那樣晒旅,Normalize.css
加上注釋僅有450行代碼怨咪,因?yàn)樗墓δ苁侵粚?duì)需要設(shè)置初始化的頁(yè)面標(biāo)簽進(jìn)行初始化两芳,使每個(gè)瀏覽器默認(rèn)樣式顯示一致。
Normalize.css現(xiàn)在已經(jīng)被用于Bootstrap、Pure到千、GitHub包晰、HTML5 Boilerplate、GOV.UK演侯、Rdio姿染、CSS Tricks 以及許許多多其他框架和網(wǎng)站上。
而所謂對(duì)需要設(shè)置初始化的元素是指秒际,每個(gè)瀏覽器的元素默認(rèn)樣式都略有不同悬赏,即同一份樣式表在不同的瀏覽器里顯示會(huì)出現(xiàn)不同程度的變形,我們需要做一些工作娄徊,讓我們的樣式設(shè)計(jì)在幾乎所有瀏覽器里都顯示一致闽颇,這種工作的另一個(gè)名字就是——做兼容。不過normalize只實(shí)現(xiàn)樣式上的兼容寄锐,比較多的是H5新標(biāo)簽的設(shè)定兵多,js的兼容還是要在js做尖啡。
以下代碼均來自normalize.css v7.0.0 ,我補(bǔ)充了一些中文翻譯與標(biāo)簽說明
/**
* 1. Correct the line height in all browsers.
* 使默認(rèn)行高在所有瀏覽器里保持一致
* 2. Prevent adjustments of font size after orientation changes in
* IE on Windows Phone and in iOS.
* 預(yù)防WP手機(jī)與iPhone手機(jī)屏幕方向改變時(shí)字體大小會(huì)自動(dòng)調(diào)整
* 3. Remove the margin in all browsers (opinionated).
* 清除所有瀏覽器預(yù)設(shè)外邊距
*/
html {
line-height: 1.15; /* 1 */
-ms-text-size-adjust: 100%; /* 2 */
-webkit-text-size-adjust: 100%; /* 2 */
}
body {
margin: 0;
}
/**
* Add the correct display in IE 9-.
* 設(shè)置H5新塊元素標(biāo)簽?zāi)茉贗E9或以下版本下以塊元素顯示
*/
article,aside,footer,header,nav,section {
display: block;
}
/**
* Correct the font size and margin on `h1` elements within `section` and
* `article` contexts in Chrome, Firefox, and Safari.
* 使h1標(biāo)簽?zāi)茉趕ection和article元素內(nèi)顯示正確的大小與外邊距
*/
h1 {
font-size: 2em;
margin: 0.67em 0;
}
/* Grouping content
========================================================================== */
/**
* Add the correct display in IE 9-.
* 1. Add the correct display in IE.
* 這兩個(gè)是H5新標(biāo)簽中鼠,用于設(shè)定圖像標(biāo)題與圖像可婶,
* 在某種特定情況下代替p與li標(biāo)簽,同樣需要設(shè)置為塊元素顯示援雇,這里是也是兼容IE9及以下瀏覽器
*/
figcaption,figure,main { /* 1 */
display: block;
}
/**
* Add the correct margin in IE 8.
* 為figure設(shè)定在IE8下正確顯示的外邊距
*/
figure {
margin: 1em 40px;
}
/**
* 1. Add the correct box sizing in Firefox.
* 2. Show the overflow in Edge and IE.
* box-sizing是CSS3新屬性
* content-box -> border和padding不計(jì)算入width之內(nèi)
* 為火狐瀏覽器能正確顯示分割線hr
* 讓分割線在IE下超出部分顯示滾動(dòng)條
*/
hr {
box-sizing: content-box; /* 1 */
height: 0; /* 1 */
overflow: visible; /* 2 */
}
/**
* 1. Correct the inheritance and scaling of font size in all browsers.
* 2. Correct the odd `em` font sizing in all browsers.
* pre標(biāo)簽的一個(gè)常見應(yīng)用就是用來表示計(jì)算機(jī)的源代碼
* 為了該標(biāo)簽在所有瀏覽器里繼承父級(jí)元素字體大小與字體大小的變化
*/
pre {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/* Text-level semantics
========================================================================== */
/**
* 1. Remove the gray background on active links in IE 10.
* 2. Remove gaps in links underline in iOS 8+ and Safari 8+.
* 去除a鏈接在IE10下顯示的灰色背景
* 消除在IOS8+與Safari瀏覽器里下劃線與a鏈接的間隔
*/
a {
background-color: transparent; /* 1 */
-webkit-text-decoration-skip: objects; /* 2 */
}
/**
* 1. Remove the bottom border in Chrome 57- and Firefox 39-.
* 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
* abbr標(biāo)簽表縮寫
* 去除在谷歌瀏覽器5.7版本以下和火狐3.9版本以下顯示的下邊框
* 為webkit內(nèi)核的瀏覽器與IE矛渴,Opera瀏覽器顯示正確的下劃線
*/
abbr[title] {
border-bottom: none; /* 1 */
text-decoration: underline; /* 2 */
text-decoration: underline dotted; /* 2 */
}
/**
* Prevent the duplicate application of `bolder` by the next rule in Safari 6.
* 避免在Safari6下重復(fù)添加文本加粗樣式
*/
b,
strong {
font-weight: inherit;
}
/**
* Add the correct font weight in Chrome, Edge, and Safari.
* 為b和strong標(biāo)簽設(shè)置一致的字體粗細(xì) bolder(加粗)
*/
b,
strong {
font-weight: bolder;
}
/**
* 1. Correct the inheritance and scaling of font size in all browsers.
* 2. Correct the odd `em` font sizing in all browsers.
* 這里也是為了讓code,kbd,samp標(biāo)簽字體大小能跟隨默認(rèn)字體大小,字體設(shè)置為等寬字體
*/
code,kbd,samp {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/**
* Add the correct font style in Android 4.3-.
* 為安卓4.3以下dfn標(biāo)簽設(shè)置傾斜字體樣式
*/
dfn {
font-style: italic;
}
/**
* Add the correct background and color in IE 9-.
* 在IE9以下瀏覽器能顯示mark標(biāo)簽高亮的顏色
*/
mark {
background-color: #ff0;
color: #000;
}
/**
* Add the correct font size in all browsers.
* 設(shè)置small標(biāo)簽內(nèi)字體為默認(rèn)字體大小的80%
*/
small {
font-size: 80%;
}
/**
* Prevent `sub` and `sup` elements from affecting the line height in
* all browsers.
* sub上標(biāo)惫搏,sup下標(biāo)
* 字體大小為父級(jí)元素字體大小的75%具温,行高設(shè)置為不占行高,定位設(shè)置為相對(duì)定位筐赔,
* 水平對(duì)齊方式為基線對(duì)齊
*/
sub,
sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
sub {
bottom: -0.25em;
}
sup {
top: -0.5em;
}
/* Embedded content
========================================================================== */
/**
* Add the correct display in IE 9-.
* 在IE9及以下audio與video標(biāo)簽以行內(nèi)塊顯示
*/
audio,
video {
display: inline-block;
}
/**
* Add the correct display in iOS 4-7.
* 在iOS4-7隱藏控制條
*/
audio:not([controls]) {
display: none;
height: 0;
}
/**
* Remove the border on images inside links in IE 10-.
* 為IE10及以下移除圖片的邊框
*/
img {
border-style: none;
}
/**
* Hide the overflow in IE.
* 如果svg元素不是直接在根元素html上時(shí)铣猩,超出部分隱藏
*/
svg:not(:root) {
overflow: hidden;
}
/* Forms
========================================================================== */
/**
* 1. Change the font styles in all browsers (opinionated).
* 2. Remove the margin in Firefox and Safari.
* 設(shè)置字體大小統(tǒng)一為默認(rèn)字體大小,行高為默認(rèn)行高的1.15倍
* 外邊距為0
*/
button,input,optgroup,select,textarea {
font-family: sans-serif; /* 1 */
font-size: 100%; /* 1 */
line-height: 1.15; /* 1 */
margin: 0; /* 2 */
}
/**
* Show the overflow in IE.
* 1. Show the overflow in Edge.
* 使input與button標(biāo)簽超出部分可見
*/
button,input { /* 1 */
overflow: visible;
}
/**
* Remove the inheritance of text transform in Edge, Firefox, and IE.
* 1. Remove the inheritance of text transform in Firefox.
* 移除在火狐與IE中本文轉(zhuǎn)變大小寫的繼承
* 不轉(zhuǎn)變標(biāo)簽內(nèi)本文的大小寫
*/
button,select { /* 1 */
text-transform: none;
}
/**
* 1. Prevent a WebKit bug where (2) destroys native `audio` and `video`
* controls in Android 4.
* 2. Correct the inability to style clickable types in iOS and Safari.
* 為了避免webkit的漏洞破壞audio與video的控制條在安卓4版本的顯示
* 修復(fù)IOS與Safari下不能點(diǎn)擊的問題
*/
button,html [type="button"], /* 1 */
[type="reset"],
[type="submit"] {
-webkit-appearance: button; /* 2 */
}
/**
* Remove the inner border and padding in Firefox.
* 移除火狐瀏覽器預(yù)設(shè)的邊框與內(nèi)邊距
*/
button::-moz-focus-inner,
[type="button"]::-moz-focus-inner,
[type="reset"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner {
border-style: none;
padding: 0;
}
/**
* Restore the focus styles unset by the previous rule.
* 修復(fù)被選中時(shí)的樣式
*/
button:-moz-focusring,
[type="button"]:-moz-focusring,
[type="reset"]:-moz-focusring,
[type="submit"]:-moz-focusring {
outline: 1px dotted ButtonText;
}
/**
* Correct the padding in Firefox.
* fieldset標(biāo)簽將表單內(nèi)容的一部分打包茴丰,生成一組相關(guān)表單的字段
* 修改火狐瀏覽器的內(nèi)邊距
*/
fieldset {
padding: 0.35em 0.75em 0.625em;
}
/**
* 1. Correct the text wrapping in Edge and IE.
* 2. Correct the color inheritance from `fieldset` elements in IE.
* 3. Remove the padding so developers are not caught out when they zero out
* `fieldset` elements in all browsers.
* legend 元素為 fieldset元素定義標(biāo)題(caption)
* border-box -> border和padding計(jì)算入width之內(nèi)
* white-space換行模式达皿,normal則空白會(huì)被瀏覽器忽略
*/
legend {
box-sizing: border-box; /* 1 */
color: inherit; /* 2 */
display: table; /* 1 */
max-width: 100%; /* 1 */
padding: 0; /* 3 */
white-space: normal; /* 1 */
}
/**
* 1. Add the correct display in IE 9-.
* 2. Add the correct vertical alignment in Chrome, Firefox, and Opera.
* 進(jìn)度條以行內(nèi)塊樣式進(jìn)行顯示,垂直對(duì)齊方式為基線對(duì)齊
*/
progress {
display: inline-block; /* 1 */
vertical-align: baseline; /* 2 */
}
/**
* Remove the default vertical scrollbar in IE.
* 移除IE默認(rèn)的本文輸入框滾動(dòng)條
*/
textarea {
overflow: auto;
}
/**
* 1. Add the correct box sizing in IE 10-.
* 2. Remove the padding in IE 10-.
* 單選復(fù)選框的邊框和內(nèi)邊距計(jì)算入元素寬度之內(nèi)
* 內(nèi)邊距設(shè)置為0
*/
[type="checkbox"],[type="radio"] {
box-sizing: border-box; /* 1 */
padding: 0; /* 2 */
}
/**
* Correct the cursor style of increment and decrement buttons in Chrome.
* 修正在谷歌瀏覽器下的鼠標(biāo)在按鍵中的位移量
*/
[type="number"]::-webkit-inner-spin-button,
[type="number"]::-webkit-outer-spin-button {
height: auto;
}
/**
* 1. Correct the odd appearance in Chrome and Safari.
* 2. Correct the outline style in Safari.
* 修改谷歌與Safari瀏覽器的舊樣式
* outline (輪廓)是繪制于元素周圍的一條線贿肩,位于邊框邊緣的外圍峦椰,可起到突出元素的作用
* 為Safari瀏覽器修改outline樣式
*/
[type="search"] {
-webkit-appearance: textfield; /* 1 */
outline-offset: -2px; /* 2 */
}
/**
* Remove the inner padding and cancel buttons in Chrome and Safari on macOS.
* 移除谷歌與Safari瀏覽器在macOS系統(tǒng)下的默認(rèn)內(nèi)邊距與取消按鈕
*/
[type="search"]::-webkit-search-cancel-button,
[type="search"]::-webkit-search-decoration {
-webkit-appearance: none;
}
/**
* 1. Correct the inability to style clickable types in iOS and Safari.
* 2. Change font properties to `inherit` in Safari.
* 修復(fù)按鈕在IOS與Safari瀏覽器下無(wú)法點(diǎn)擊
* 改變?cè)赟afari瀏覽器下的字體繼承
*/
::-webkit-file-upload-button {
-webkit-appearance: button; /* 1 */
font: inherit; /* 2 */
}
/* Interactive
========================================================================== */
/*
* Add the correct display in IE 9-.
* 1. Add the correct display in Edge, IE, and Firefox.
* 設(shè)置details與menu標(biāo)簽為塊元素顯示
*/
details, /* 1 */
menu {
display: block;
}
/*
* Add the correct display in all browsers.
* summary標(biāo)簽為details標(biāo)簽里的標(biāo)題
* display: list-item表示此元素會(huì)作為列表顯示
*/
summary {
display: list-item;
}
/* Scripting
========================================================================== */
/**
* Add the correct display in IE 9-.
* 在IE9及以下canvas畫布以行內(nèi)塊元素顯示
*/
canvas {
display: inline-block;
}
/**
* Add the correct display in IE.
* template元素是一種用于保存客戶端內(nèi)容的機(jī)制,該內(nèi)容在頁(yè)面加載時(shí)不被渲染汰规,
* 但可以在運(yùn)行時(shí)使用JavaScript進(jìn)行實(shí)例化汤功。所以先隱藏
*/
template {
display: none;
}
/* Hidden
========================================================================== */
/**
* Add the correct display in IE 10-.
* 為IE10及以下添加隱藏屬性
*/
[hidden] {
display: none;
}
總結(jié)
以上就是Normalize.css的全部代碼,我們可以很方便得在項(xiàng)目中引入它溜哮,我們也可以根據(jù)自己的需要對(duì)它進(jìn)行刪改滔金,實(shí)現(xiàn)高度定制化。比如一些我們沒有用上的H5新標(biāo)簽是沒有必要做兼容的茂嗓。
最后餐茵,感謝原作者,他們不僅方便了我們的工作述吸,還給我們上了一課钟病。