關於CSS Reset 那些事(二)之 Normalize.css 源碼解讀

前言

上一章節介紹了CSS Reset的歷史,最後對Normalize.css作了簡單的瞭解,因此從這節開始對源碼進行註釋翻譯與解讀,儘量從最根本性的問題了解它在幫咱們作什麼?css

回顧:關於CSS Reset 那些事(一)之 歷史演變與Normalize.csshtml

Normalize 源碼解讀

前面講到的分模塊解讀,就是先黏貼一段源碼,而後根據官方提供的註釋進行翻譯整理,儘量提供案例解析,而後再次進行整理總結,若是你有疑問,能夠留言一塊兒交流。git

源碼地址:https://github.com/necolas/normalize.css/blob/master/normalize.css
源碼版本:v3.0.3github

html與body 元素

/**
 * 1. Set default font family to sans-serif.
 * 2. Prevent iOS and IE text size adjust after device orientation change,
 *    without disabling user zoom.
 */
html {
  font-family: sans-serif; /* 1 */
  -ms-text-size-adjust: 100%; /* 2 */
  -webkit-text-size-adjust: 100%; /* 2 */
}
  1. 設置全局的字體爲sans-serif,關於中文字體的設置可參考 Amaze UI
  2. 防止 iOS 橫屏字號放大,同時保證在PC上 zoom 功能正常

第2個問題場景是這樣,蘋果IOS設備調整後會自動調整文字的大小,按照蘋果的意圖是爲了提高用戶體驗,好比豎屏狀態下是14px,轉換爲橫屏時就變成了20px,把text-size-adjust:100%就不會調整字體大小了。web

若是把值設置爲'text-size-adjust:none',那麼就會致使用戶沒法放大縮小字體了。canvas

/**
 * Remove default margin.
 */

body {
  margin: 0;
}
  • 修復瀏覽器默認邊距,統一效果

HTML5 元素 display definitions

/**
 * Correct `block` display not defined for any HTML5 element in IE 8/9.
 * Correct `block` display not defined for `details` or `summary` in IE 10/11
 * and Firefox.
 * Correct `block` display not defined for `main` in IE 11.
 */

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
main,
menu,
nav,
section,
summary {
  display: block;
}
  • 修復 IE 8/9,HTML5新元素不能正確顯示的問題,定義爲block的元素
  • 修復 IE 10/11,detailssummary 定義爲 block 的元素
  • 修復 IE 11,main定義爲 block 的元素

這個問題想必你們都已經很是清楚,當低版本瀏覽器遇到不識別的元素時,會默認把他們當成內聯元素(inline),這裏從新定義成爲block元素。segmentfault

/**
 * 1. Correct `inline-block` display not defined in IE 8/9.
 * 2. Normalize vertical alignment of `progress` in Chrome, Firefox, and Opera.
 */

audio,
canvas,
progress,
video {
  display: inline-block; /* 1 */
  vertical-align: baseline; /* 2 */
}
  1. 修復 IE 8/9,HTML5新元素不能正確顯示的問題,定義爲inline-block元素
  2. 修復Chrome, Firefox, 和Opera的progress元素沒有以baseline垂直對齊

progress是HTML5的新標籤,能夠定義進度條,可是它Chrome, Firefox, 和Opera並無已baseline垂直對齊。瀏覽器

/**
 * Prevent modern browsers from displaying `audio` without controls.
 * Remove excess height in iOS 5 devices.
 */
audio:not([controls]) {
  display: none;
  height: 0;
}
  • 對不支持controls屬性的瀏覽器,audio元素給以隱藏
  • 移除iOS5設備中多餘的高度

在IE8以前的瀏覽器是不支持controls屬性,這裏的辦法是直接隱藏該元素ide

/**
 * Address `[hidden]` styling not present in IE 8/9/10.
 * Hide the `template` element in IE 8/9/10/11, Safari, and Firefox < 22.
 */

[hidden],
template {
  display: none;
}
  • 修復 IE 7/8/9,Firefox 3 和 Safari 4 中hidden屬性不起做用的問題
  • 在 IE,Safari,Firefox 22- 中隱藏template元素

template標籤用於HTML模板,你們應該都是用過HTML模版開發頁面,這個標籤是按照實際需求添加的,可是模板又不能在界面上顯示,因此這裏統一了樣式,兼容舊瀏覽器。svg

連接 Links

/**
 * Remove the gray background color from active links in IE 10.
 */

a {
  background-color: transparent;
}
  • 去掉 IE 10+ 點擊連接時的灰色背景
/**
 * Improve readability of focused elements when they are also in an
 * active/hover state.
 */

a:active,
a:hover {
  outline: 0;
}
  • 去掉點擊時的outline焦點框,同時保證使用鍵盤能夠顯示焦點框,這個操做針對全部瀏覽器

語義化文本標籤 Text-level semantics

/**
 * Address styling not present in IE 8/9/10/11, Safari, and Chrome.
 */

abbr[title] {
  border-bottom: 1px dotted;
}
  • 修正abbr元素在 Firefox 外其餘瀏覽器沒有下劃線的問題

語義abbr標籤是表示簡稱或縮寫,自身的title屬性是完整版,可是此標籤在Firefox下默認有下邊框,而其餘瀏覽器中沒有,這裏統一了樣式。

/**
 * Address style set to `bolder` in Firefox 4+, Safari, and Chrome.
 */

b,
strong {
  font-weight: bold;
}
  • Firefox3+,Safari4/5 和 Chrome 中統一設置爲粗體

Firefox 3+, Safari 和 Chrome 給bstrong設置的屬性是bolder,而不是bold,這裏統一了樣式。

/**
 * Address styling not present in Safari and Chrome.
 */

dfn {
  font-style: italic;
}
  • 修正 Safari5 和 Chrome 中沒有樣式的問題

dfn 標籤可標記那些對特殊術語或短語的定義,在Safari 和Chrome 裏不是斜體,在這裏統一了樣式。

/**
 * Address variable `h1` font-size and margin within `section` and `article`
 * contexts in Firefox 4+, Safari, and Chrome.
 */

h1 {
  font-size: 2em;
  margin: 0.67em 0;
}
  • 修復 Firefox 4+,Safari 5 和 Chrome 中sectionarticle內的h1字體大小
/**
 * Address styling not present in IE 8/9.
 */

mark {
  background: #ff0;
  color: #000;
}
  • 修復 IE 6/9, Safari 5 和 Chrome中樣式不呈現的問題

mark標籤用來突出顯示部分文本,設置後會有一個高亮背景,可是此標籤是HTML5中的新標籤,在低版本瀏覽器並不識別,因此須要重置樣式。


/** * Address inconsistent and variable font size in all browsers. */ small { font-size: 80%; }
  • 在全部瀏覽器中統一small的字體大小

small標籤在 HTML 4.01 就已經存在,HTML5 中加強了它的寓意,表示旁註信息,不過此標籤在各個瀏覽器中呈現的字體大小不同,在這裏作了統一

/**
 * Prevent `sub` and `sup` affecting `line-height` in all browsers.
 */

sub,
sup {
  font-size: 75%;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
}

sup {
  top: -0.5em;
}

sub {
  bottom: -0.25em;
}
  • 防止全部瀏覽器中的subsup影響行高

supsub兩個標籤是用來表示上標和下標,據HTML標準中對smallsubsup的大小要求都是smaller,可是如上所示normalize.csssmall設的是80%,而subsup倒是75%,因此爲了保持一致,且不影響其餘元素的行高,把二者的line-height設爲0,而後設置它的垂直以baseline開始,設置topbottom手動設置二者偏移量

內嵌元素 Embedded content

/**
 * Remove border when inside `a` element in IE 8/9/10.
 */

img {
  border: 0;
}
  • 去除 IE6-9 和 Firefox 3 中a內部img元素默認的邊框

在舊版本的瀏覽器中,圖片默認會有一個奇醜無比的藍色邊框,這這裏進行清除,統同樣式。

/**
 * Correct overflow not hidden in IE 9/10/11.
 */

svg:not(:root) {
  overflow: hidden;
}
  • 修復 IE9 中的overflow的怪異表現

羣組元素 Grouping content

/**
 * Address margin not present in IE 8/9 and Safari.
 */

figure {
  margin: 1em 40px;
}
  • 修復 IE 8/九、Safari中margin失效

figure 是HTML5的新標籤,用作文檔插圖,但它在 IE 8/9 and Safari 中的默認margin失效,這裏作了統一設置。

/**
 * Address differences between Firefox and other browsers.
 */

hr {
  box-sizing: content-box;
  height: 0;
}
  • 修正 Firefox 和其餘瀏覽器之間的差別

在 Firefox 中,hr元素的默認樣式不少,和其它瀏覽器主要的差別有兩點:
1.設置了height2px;
2.box-sizingborder-box;
此樣式對這兩個問題進行重置,進行統一

/**
 * Contain overflow in all browsers.
 */

pre {
  overflow: auto;
}
  • 標籤設置滾動條,內容溢出時出現

大部分瀏覽器的preoverflow的時候會直接溢出去,這裏加上overflow:auto讓它出現滾動條

/**
 * Address odd `em`-unit font size rendering in all browsers.
 */

code,
kbd,
pre,
samp {
  font-family: monospace, monospace;
  font-size: 1em;
}
  • 用於修復 Safari 5 和 Chrome 中奇怪的字體設置,統一字體樣式

總結

此章節咱們對Normalize.css中設置的 html與body元素,HTML5元素,連接,語義化文本,內嵌元素,羣組元素 的源碼進行詳細的解讀,發現正如其說的同樣,它不只僅幫咱們修復了瀏覽器的默認bug,還保留了許多標籤的默認值,尤爲是語義化標籤,堅持他們存在的意義。

因爲源碼部分過長,因此對於源碼的解析會分爲兩節,下一節咱們繼續來介紹:

表單:表單每每存在不少問題,如常見的各類不繼承問題,這這裏都會獲得修復
表格:表格的默認邊距和邊框真的很醜,須要修復修復

下一節會完成全部模塊對normalize.css源碼解讀,屆時會整理一份normalize-zh.css中文註釋的版本上傳至Github,供你們參考使用,敬請期待...

相關文章
相關標籤/搜索