上一章節介紹了CSS Reset的歷史,最後對Normalize.css作了簡單的瞭解,因此從這節開始對源碼進行註釋翻譯與解讀,儘量從最根本性的問題了解它在幫咱們作什麼?css
回顧:關於CSS Reset 那些事(一)之 歷史演變與Normalize.csshtml
前面講到的分模塊解讀,就是先黏貼一段源碼,而後根據官方提供的註釋進行翻譯整理,儘量提供案例解析,而後再次進行整理總結,若是你有疑問,能夠留言一塊兒交流。git
源碼地址:https://github.com/necolas/normalize.css/blob/master/normalize.css
源碼版本:v3.0.3github
/** * 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 */ }
第2個問題場景是這樣,蘋果IOS設備調整後會自動調整文字的大小,按照蘋果的意圖是爲了提高用戶體驗,好比豎屏狀態下是14px
,轉換爲橫屏時就變成了20px
,把text-size-adjust:100%
就不會調整字體大小了。web
若是把值設置爲'text-size-adjust:none'
,那麼就會致使用戶沒法放大縮小字體了。canvas
/** * Remove default margin. */ body { margin: 0; }
/** * 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; }
block
的元素details
和 summary
定義爲 block
的元素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 */ }
inline-block
元素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
元素給以隱藏在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; }
hidden
屬性不起做用的問題template
元素template
標籤用於HTML模板,你們應該都是用過HTML模版開發頁面,這個標籤是按照實際需求添加的,可是模板又不能在界面上顯示,因此這裏統一了樣式,兼容舊瀏覽器。svg
/** * Remove the gray background color from active links in IE 10. */ a { background-color: transparent; }
/** * Improve readability of focused elements when they are also in an * active/hover state. */ a:active, a:hover { outline: 0; }
outline
焦點框,同時保證使用鍵盤能夠顯示焦點框,這個操做針對全部瀏覽器/** * 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; }
Firefox 3+, Safari 和 Chrome 給b
和strong
設置的屬性是bolder
,而不是bold
,這裏統一了樣式。
/** * Address styling not present in Safari and Chrome. */ dfn { font-style: italic; }
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; }
section
和article
內的h1
字體大小/** * Address styling not present in IE 8/9. */ mark { background: #ff0; color: #000; }
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; }
sub
和sup
影響行高sup
和sub
兩個標籤是用來表示上標和下標,據HTML標準中對small
,sub
和sup
的大小要求都是smaller
,可是如上所示normalize.css
給small
設的是80%,而sub
和sup
倒是75%,因此爲了保持一致,且不影響其餘元素的行高,把二者的line-height
設爲0
,而後設置它的垂直以baseline開始,設置top
和bottom
手動設置二者偏移量
/** * Remove border when inside `a` element in IE 8/9/10. */ img { border: 0; }
a
內部img
元素默認的邊框在舊版本的瀏覽器中,圖片默認會有一個奇醜無比的藍色邊框,這這裏進行清除,統同樣式。
/** * Correct overflow not hidden in IE 9/10/11. */ svg:not(:root) { overflow: hidden; }
overflow
的怪異表現/** * Address margin not present in IE 8/9 and Safari. */ figure { margin: 1em 40px; }
figure
是HTML5的新標籤,用作文檔插圖,但它在 IE 8/9 and Safari 中的默認margin
失效,這裏作了統一設置。
/** * Address differences between Firefox and other browsers. */ hr { box-sizing: content-box; height: 0; }
在 Firefox 中,hr
元素的默認樣式不少,和其它瀏覽器主要的差別有兩點:
1.設置了height
爲2px
;
2.box-sizing
爲border-box
;
此樣式對這兩個問題進行重置,進行統一
/** * Contain overflow in all browsers. */ pre { overflow: auto; }
大部分瀏覽器的pre
在overflow
的時候會直接溢出去,這裏加上overflow:auto
讓它出現滾動條
/** * Address odd `em`-unit font size rendering in all browsers. */ code, kbd, pre, samp { font-family: monospace, monospace; font-size: 1em; }
此章節咱們對Normalize.css中設置的 html與body元素,HTML5元素,連接,語義化文本,內嵌元素,羣組元素 的源碼進行詳細的解讀,發現正如其說的同樣,它不只僅幫咱們修復了瀏覽器的默認bug,還保留了許多標籤的默認值,尤爲是語義化標籤,堅持他們存在的意義。
因爲源碼部分過長,因此對於源碼的解析會分爲兩節,下一節咱們繼續來介紹:
表單:表單每每存在不少問題,如常見的各類不繼承問題,這這裏都會獲得修復
表格:表格的默認邊距和邊框真的很醜,須要修復修復
下一節會完成全部模塊對normalize.css源碼解讀,屆時會整理一份normalize-zh.css
中文註釋的版本上傳至Github,供你們參考使用,敬請期待...