Normalize.css學(xué)習(xí)筆記

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)被用于BootstrapPure到千、GitHub包晰、HTML5 BoilerplateGOV.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)簽是沒有必要做兼容的茂嗓。

最后餐茵,感謝原作者,他們不僅方便了我們的工作述吸,還給我們上了一課钟病。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市刚梭,隨后出現(xiàn)的幾起案子肠阱,更是在濱河造成了極大的恐慌,老刑警劉巖朴读,帶你破解...
    沈念sama閱讀 222,681評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件屹徘,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡衅金,警方通過查閱死者的電腦和手機(jī)噪伊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門簿煌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人鉴吹,你說我怎么就攤上這事姨伟。” “怎么了豆励?”我有些...
    開封第一講書人閱讀 169,421評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵夺荒,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我良蒸,道長(zhǎng)技扼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,114評(píng)論 1 300
  • 正文 為了忘掉前任嫩痰,我火速辦了婚禮剿吻,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘串纺。我一直安慰自己丽旅,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,116評(píng)論 6 398
  • 文/花漫 我一把揭開白布纺棺。 她就那樣靜靜地躺著魔招,像睡著了一般。 火紅的嫁衣襯著肌膚如雪五辽。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,713評(píng)論 1 312
  • 那天外恕,我揣著相機(jī)與錄音杆逗,去河邊找鬼。 笑死鳞疲,一個(gè)胖子當(dāng)著我的面吹牛罪郊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播尚洽,決...
    沈念sama閱讀 41,170評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼悔橄,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了腺毫?” 一聲冷哼從身側(cè)響起癣疟,我...
    開封第一講書人閱讀 40,116評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎潮酒,沒想到半個(gè)月后睛挚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,651評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡急黎,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,714評(píng)論 3 342
  • 正文 我和宋清朗相戀三年扎狱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了侧到。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,865評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡淤击,死狀恐怖匠抗,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情污抬,我是刑警寧澤汞贸,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站壕吹,受9級(jí)特大地震影響著蛙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜耳贬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,211評(píng)論 3 336
  • 文/蒙蒙 一踏堡、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧咒劲,春花似錦顷蟆、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至蛔屹,卻和暖如春削樊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背兔毒。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工漫贞, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人育叁。 一個(gè)月前我還...
    沈念sama閱讀 49,299評(píng)論 3 379
  • 正文 我出身青樓迅脐,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親豪嗽。 傳聞我的和親對(duì)象是個(gè)殘疾皇子谴蔑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,870評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 13,761評(píng)論 1 92
  • 轉(zhuǎn)載請(qǐng)聲明 原文鏈接地址 關(guān)注公眾號(hào)獲取更多資訊 第一部分 HTML 第一章 職業(yè)規(guī)劃和前景 職業(yè)方向規(guī)劃定位...
    程序員poetry閱讀 16,562評(píng)論 32 459
  • ?前端面試題匯總 一龟梦、HTML和CSS 21 你做的頁(yè)面在哪些流覽器測(cè)試過隐锭?這些瀏覽器的內(nèi)核分別是什么? ...
    Simon_s閱讀 2,221評(píng)論 0 8
  • 昨天小孩的語(yǔ)文老師發(fā)了一張A4的作業(yè)紙,其實(shí)都是平常小孩要掌握的字詞计贰。初中老師現(xiàn)在對(duì)字詞成榜、作文已經(jīng)不再抓了,...
    D016pudding閱讀 458評(píng)論 1 1
  • P870DM蹦玫,藍(lán)天制造赎婚,雙卡機(jī)刘绣,可配備兩塊獨(dú)立顯卡,如GTX980M挣输,組成SLI,但是纬凤,像mGTX980這樣的顯卡...
    renault_photo閱讀 9,319評(píng)論 0 1