在上一節中我們介紹了Bootstrap整體架構,本節我們將介紹Bootstrap框架第二部分初始化及依賴項,這部分內容位於源碼的第8~885行,打開源碼這部分內容似乎也不是很難理解。但是請站在一個開發者的角度來面對這段源碼。為什么要這樣寫?如果沒有Bootstrap框架我能寫出類似這樣的框架嗎?
我們先來分析normalize.less編譯后的源碼,我們知道normalize.css是一個專門將不同瀏覽器的默認css特性設置為統一效果的css庫,它和reset.css還是有區別的,normalize.css並不是簡單的重置了所有的樣式,而是有針對的修改,同時也保留了標簽的語義化。
html {
font-family: sans-serif;
-webkit-text-size-adjust: 100%; //防止IOS系統方向改變(特別是手持設備)后字體大小的調整,不禁用用戶縮放。
-ms-text-size-adjust: 100%;
}
body {
margin: 0;
}
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
main,
menu,
nav,
section,
summary {
display: block; //修正IE中未定義的html5塊狀元素
}
audio,
canvas,
progress,
video {
display: inline-block;
vertical-align: baseline;
}
audio:not([controls]) {//否定偽類,匹配所有沒有controls屬性的audio
display: none; //防止現代瀏覽器顯示沒有控制條的音頻。
height: 0; //在iOS5設備中去除多余的高度。
}
[hidden],
template {//template標簽主要用於聲明是“模板元素”。
display: none; //template標簽和有hidden屬性的元素不顯示
}
a {
background-color: transparent;
}
a:active,
a:hover {
outline: 0;
}
abbr[title] {
border-bottom: 1px dotted;//這是縮寫形式,border-bottom-color:initial;
}
b,
strong {
font-weight: bold;
}
dfn { //用來定義一個定義項目。
font-style: italic;
}
h1 {
margin: .67em 0;
font-size: 2em;
}
mark { //部分文本高亮顯示,請在需要突出顯示文本時使用
color: #000;
background: #ff0;
}
small {
font-size: 80%;
}
sub,
sup { //防止sub sup影響行高
position: relative;
font-size: 75%;
line-height: 0;
vertical-align: baseline;
}
sup {
top: -.5em;
}
sub {
bottom: -.25em;
}
img {
border: 0;
}
svg:not(:root) {//非根目錄的svg標簽
overflow: hidden;
}
figure { //標記文檔中的一個圖像
margin: 1em 40px;
}
hr {
height: 0;
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
}
pre {
overflow: auto;
}
code,
kbd,
pre,
samp {
font-family: monospace, monospace;
font-size: 1em;
}
button,
input,
optgroup,
select,
textarea {
margin: 0;
font: inherit;
color: inherit;
}
button {
overflow: visible;
}
button,
select {
text-transform: none;
}
button,
html input[type="button"],
input[type="reset"],
input[type="submit"] {
-webkit-appearance: button;
cursor: pointer;
}
button[disabled],
html input[disabled] {
cursor: default;
}
button::-moz-focus-inner,
input::-moz-focus-inner { //刪除在火狐瀏覽器4及以上中的按鈕的內部填充和邊框
padding: 0;
border: 0;
}
input {
line-height: normal;
}
input[type="checkbox"],
input[type="radio"] {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 0;
}
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
height: auto;
}
input[type="search"] {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
-webkit-appearance: textfield;
}
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-decoration {
-webkit-appearance: none;
}
fieldset {//將表單內的相關元素分組。
padding: .35em .625em .75em;
margin: 0 2px;
border: 1px solid #c0c0c0;
}
legend {
padding: 0;
border: 0;
}
textarea {
overflow: auto;
}
optgroup {//用於把相關的選項組合在一起。
font-weight: bold;
}
table {
border-spacing: 0;
border-collapse: collapse;
}
td,
th {
padding: 0;
}
print.less主要是網頁的打印樣式,在此就不做分析了
glyphicons.less是Bootstrap引入的字體圖標樣式,個人覺得提供的字體圖標樣式太少,而且不太適合國內的網頁特點,所以在定制自己的Bootstrap框架時完全可以將其去掉,或者用其他的字體替換。我個人喜歡使用淘寶的iconfont字體,其使用方式與Bootstrap相同,都是在需要使用圖標的地方加上相應的類名即可。
來分析下部分glyphicons源碼
.glyphicon {
position: relative;
top: 1px;
display: inline-block;
font-family: 'Glyphicons Halflings';
font-style: normal;
font-weight: normal;
line-height: 1;
-webkit-font-smoothing: antialiased; //字體灰度平滑
-moz-osx-font-smoothing: grayscale;// 灰階渲染
}
.glyphicon-asterisk:before {
content: "\2a"; //與引入的字體文件中圖標對應
}
這些字體圖標的本質是使用before偽類,其content后面的數字碼則是與那個圖標一一對應。