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行代碼,因為它的功能是只對需要設置初始化的頁面標簽進行初始化,使每個瀏覽器默認樣式顯示一致。
Normalize.css現在已經被用于Bootstrap、Pure、GitHub、HTML5 Boilerplate、GOV.UK、Rdio、CSS Tricks 以及許許多多其他框架和網站上。
而所謂對需要設置初始化的元素是指,每個瀏覽器的元素默認樣式都略有不同,即同一份樣式表在不同的瀏覽器里顯示會出現不同程度的變形,我們需要做一些工作,讓我們的樣式設計在幾乎所有瀏覽器里都顯示一致,這種工作的另一個名字就是——做兼容。不過normalize只實現樣式上的兼容,比較多的是H5新標簽的設定,js的兼容還是要在js做。
以下代碼均來自normalize.css v7.0.0 ,我補充了一些中文翻譯與標簽說明
/**
* 1. Correct the line height in all browsers.
* 使默認行高在所有瀏覽器里保持一致
* 2. Prevent adjustments of font size after orientation changes in
* IE on Windows Phone and in iOS.
* 預防WP手機與iPhone手機屏幕方向改變時字體大小會自動調整
* 3. Remove the margin in all browsers (opinionated).
* 清除所有瀏覽器預設外邊距
*/
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-.
* 設置H5新塊元素標簽能在IE9或以下版本下以塊元素顯示
*/
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標簽能在section和article元素內顯示正確的大小與外邊距
*/
h1 {
font-size: 2em;
margin: 0.67em 0;
}
/* Grouping content
========================================================================== */
/**
* Add the correct display in IE 9-.
* 1. Add the correct display in IE.
* 這兩個是H5新標簽,用于設定圖像標題與圖像,
* 在某種特定情況下代替p與li標簽,同樣需要設置為塊元素顯示,這里是也是兼容IE9及以下瀏覽器
*/
figcaption,figure,main { /* 1 */
display: block;
}
/**
* Add the correct margin in IE 8.
* 為figure設定在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不計算入width之內
* 為火狐瀏覽器能正確顯示分割線hr
* 讓分割線在IE下超出部分顯示滾動條
*/
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標簽的一個常見應用就是用來表示計算機的源代碼
* 為了該標簽在所有瀏覽器里繼承父級元素字體大小與字體大小的變化
*/
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標簽表縮寫
* 去除在谷歌瀏覽器5.7版本以下和火狐3.9版本以下顯示的下邊框
* 為webkit內核的瀏覽器與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下重復添加文本加粗樣式
*/
b,
strong {
font-weight: inherit;
}
/**
* Add the correct font weight in Chrome, Edge, and Safari.
* 為b和strong標簽設置一致的字體粗細 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標簽字體大小能跟隨默認字體大小,字體設置為等寬字體
*/
code,kbd,samp {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/**
* Add the correct font style in Android 4.3-.
* 為安卓4.3以下dfn標簽設置傾斜字體樣式
*/
dfn {
font-style: italic;
}
/**
* Add the correct background and color in IE 9-.
* 在IE9以下瀏覽器能顯示mark標簽高亮的顏色
*/
mark {
background-color: #ff0;
color: #000;
}
/**
* Add the correct font size in all browsers.
* 設置small標簽內字體為默認字體大小的80%
*/
small {
font-size: 80%;
}
/**
* Prevent `sub` and `sup` elements from affecting the line height in
* all browsers.
* sub上標,sup下標
* 字體大小為父級元素字體大小的75%,行高設置為不占行高,定位設置為相對定位,
* 水平對齊方式為基線對齊
*/
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標簽以行內塊顯示
*/
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上時,超出部分隱藏
*/
svg:not(:root) {
overflow: hidden;
}
/* Forms
========================================================================== */
/**
* 1. Change the font styles in all browsers (opinionated).
* 2. Remove the margin in Firefox and Safari.
* 設置字體大小統一為默認字體大小,行高為默認行高的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標簽超出部分可見
*/
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中本文轉變大小寫的繼承
* 不轉變標簽內本文的大小寫
*/
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版本的顯示
* 修復IOS與Safari下不能點擊的問題
*/
button,html [type="button"], /* 1 */
[type="reset"],
[type="submit"] {
-webkit-appearance: button; /* 2 */
}
/**
* Remove the inner border and padding in Firefox.
* 移除火狐瀏覽器預設的邊框與內邊距
*/
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.
* 修復被選中時的樣式
*/
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標簽將表單內容的一部分打包,生成一組相關表單的字段
* 修改火狐瀏覽器的內邊距
*/
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元素定義標題(caption)
* border-box -> border和padding計算入width之內
* white-space換行模式,normal則空白會被瀏覽器忽略
*/
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.
* 進度條以行內塊樣式進行顯示,垂直對齊方式為基線對齊
*/
progress {
display: inline-block; /* 1 */
vertical-align: baseline; /* 2 */
}
/**
* Remove the default vertical scrollbar in IE.
* 移除IE默認的本文輸入框滾動條
*/
textarea {
overflow: auto;
}
/**
* 1. Add the correct box sizing in IE 10-.
* 2. Remove the padding in IE 10-.
* 單選復選框的邊框和內邊距計算入元素寬度之內
* 內邊距設置為0
*/
[type="checkbox"],[type="radio"] {
box-sizing: border-box; /* 1 */
padding: 0; /* 2 */
}
/**
* Correct the cursor style of increment and decrement buttons in Chrome.
* 修正在谷歌瀏覽器下的鼠標在按鍵中的位移量
*/
[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系統下的默認內邊距與取消按鈕
*/
[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.
* 修復按鈕在IOS與Safari瀏覽器下無法點擊
* 改變在Safari瀏覽器下的字體繼承
*/
::-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.
* 設置details與menu標簽為塊元素顯示
*/
details, /* 1 */
menu {
display: block;
}
/*
* Add the correct display in all browsers.
* summary標簽為details標簽里的標題
* display: list-item表示此元素會作為列表顯示
*/
summary {
display: list-item;
}
/* Scripting
========================================================================== */
/**
* Add the correct display in IE 9-.
* 在IE9及以下canvas畫布以行內塊元素顯示
*/
canvas {
display: inline-block;
}
/**
* Add the correct display in IE.
* template元素是一種用于保存客戶端內容的機制,該內容在頁面加載時不被渲染,
* 但可以在運行時使用JavaScript進行實例化。所以先隱藏
*/
template {
display: none;
}
/* Hidden
========================================================================== */
/**
* Add the correct display in IE 10-.
* 為IE10及以下添加隱藏屬性
*/
[hidden] {
display: none;
}
總結
以上就是Normalize.css的全部代碼,我們可以很方便得在項目中引入它,我們也可以根據自己的需要對它進行刪改,實現高度定制化。比如一些我們沒有用上的H5新標簽是沒有必要做兼容的。
最后,感謝原作者,他們不僅方便了我們的工作,還給我們上了一課。