上一章節咱們對Normalize.css
源碼進行解析,可是因爲其代碼過長致使不宜瀏覽,因此表單Forms
,表格Table
部份內容放在此章節介紹。本章節會完成全部源代碼翻譯整理,最終會整理出Normalize-zh.css
中文版本並開源至Github供你們交流使用。css
回顧:關於CSS Reset 那些事(二)之 Normalize.css 源碼解讀html
Normalize-zh.css
是根據對Normalize.css
的源碼分析後,通過學習與整理,將源代碼中的英文註釋文檔翻譯爲中文版本,方便國內的開發者學習和使用,我深知此版本必定有不少不足,但願能獲得你們的理解和支持,一樣也很願意和你們一塊兒完善。git
現已將源代碼開源至Github
項目地址:https://github.com/Alsiso/normalize-zhgithub
上章節對 html與body元素,HTML5元素,連接,語義化文本,內嵌元素,羣組元素 等源碼內容已經作了解析,這章節繼續完成表單Forms
,表格Table
部分。web
源碼地址:https://github.com/necolas/normalize.css/blob/master/normalize.css
源碼版本:v3.0.3segmentfault
/** * 1. Correct color not being inherited. * Known issue: affects color of disabled elements. * 2. Correct font properties not being inherited. * 3. Address margins set differently in Firefox 4+, Safari, and Chrome. */ button, input, optgroup, select, textarea { color: inherit; /* 1 */ font: inherit; /* 2 */ margin: 0; /* 3 */ }
有一些瀏覽器會把form
表單中的一些元素 textarea
,text
,button
,select
中的字體和字體顏色默認會設置成用戶的字體或者是瀏覽器的字體,並不會從父元素繼承,因此這裏重置了這些元素的默認樣式。瀏覽器
/** * Address `overflow` set to `hidden` in IE 8/9/10/11. */ button { overflow: visible; }
overflow
屬性爲visible在 IE 8/9/10/11裏的button
默認的overflow
是hidden
,這裏統一爲visible
app
/** * Address inconsistent `text-transform` inheritance for `button` and `select`. * All other form control elements do not inherit `text-transform` values. * Correct `button` style inheritance in Firefox, IE 8/9/10/11, and Opera. * Correct `select` style inheritance in Firefox. */ button, select { text-transform: none; }
text-transform
不會繼承的問題/** * 1. Avoid the WebKit bug in Android 4.0.* where (2) destroys native `audio` * and `video` controls. * 2. Correct inability to style clickable `input` types in iOS. * 3. Improve usability and consistency of cursor style between image-type * `input` and others. */ button, html input[type="button"], /* 1 */ input[type="reset"], input[type="submit"] { -webkit-appearance: button; /* 2 */ cursor: pointer; /* 3 */ }
audio
和video
的控制器input
的問題input
的光標樣式這裏將可點擊的按鈕,統一設置鼠標樣式爲pointer
,提升了可用性ide
/** * Re-set default cursor for disabled elements. */ button[disabled], html input[disabled] { cursor: default; }
/** * Remove inner padding and border in Firefox 4+. */ button::-moz-focus-inner, input::-moz-focus-inner { border: 0; padding: 0; }
/** * Address Firefox 4+ setting `line-height` on `input` using `!important` in * the UA stylesheet. */ input { line-height: normal; }
Firefox瀏覽器會默認設置input[type="button"]的行高爲normal !important
,這裏統同樣式源碼分析
/** * It's recommended that you don't attempt to style these elements. * Firefox's implementation doesn't respect box-sizing, padding, or width. * * 1. Address box sizing set to `content-box` in IE 8/9/10. * 2. Remove excess padding in IE 8/9/10. */ input[type="checkbox"], input[type="radio"] { box-sizing: border-box; /* 1 */ padding: 0; /* 2 */ }
content-box
的問題/** * Fix the cursor style for Chrome's increment/decrement buttons. For certain * `font-size` values of the `input`, it causes the cursor style of the * decrement button to change from `default` to `text`. */ input[type="number"]::-webkit-inner-spin-button, input[type="number"]::-webkit-outer-spin-button { height: auto; }
input [type="number"]
在特定高度和 font-size
時,下面一個箭頭光標變成cursor: text
效果 /** * 1. Address `appearance` set to `searchfield` in Safari and Chrome. * 2. Address `box-sizing` set to `border-box` in Safari and Chrome. */ input[type="search"] { -webkit-appearance: textfield; /* 1 */ box-sizing: content-box; /* 2 */ }
appearance
被設置爲searchfield
的問題box-sizing
被設置爲border-box
的問題searchfield
是CSS3的屬性,它可讓一個div
元素看上去像任何元素,可是瀏覽器支持性並很差,
/** * Remove inner padding and search cancel button in Safari and Chrome on OS X. * Safari (but not Chrome) clips the cancel button when the search input has * padding (and `textfield` appearance). */ input[type="search"]::-webkit-search-cancel-button, input[type="search"]::-webkit-search-decoration { -webkit-appearance: none; }
search
的輸入框樣式/** * Define consistent border, margin, and padding. */ fieldset { border: 1px solid #c0c0c0; margin: 0 2px; padding: 0.35em 0.625em 0.75em; }
/** * 1. Correct `color` not being inherited in IE 8/9/10/11. * 2. Remove padding so people aren't caught out if they zero out fieldsets. */ legend { border: 0; /* 1 */ padding: 0; /* 2 */ }
/** * Remove default vertical scrollbar in IE 8/9/10/11. */ textarea { overflow: auto; }
/** * Don't inherit the `font-weight` (applied by a rule above). * NOTE: the default cannot safely be changed in Chrome and Safari on OS X. */ optgroup { font-weight: bold; }
optgroup
元素font-weight
始終爲bold
/** * Remove most spacing between table cells. */ table { border-collapse: collapse; border-spacing: 0; } td, th { padding: 0; }
通過兩個章節對Normalize.css
的源碼進行了學習,清晰的瞭解了它的工做原理,做爲傳統CSS Reset替代者,它當之無愧,爲你們提供一套很完整的跨瀏覽器解決方案。
不過,你是否會有和我同樣的需求,好比開發一個小站,或者一個PC端的系統時,也許只須要一些簡單的基礎模塊,好比我只想要簡單的樣式重置,統一各瀏覽器的效果就好,並不須要HTML5以及CSS3的一些問題修復。
那麼下一章,咱們來介紹,若是制定屬於本身的 CSS基礎代碼庫?