咱們已經在 環境安裝 章節下載了 Bootstrap 3.x 版本,並理解了它的目錄結構。在 fonts 文件夾內能夠找到字體圖標,它包含了下列這些文件:css
相關的 CSS 規則寫在 dist 文件夾內的 css 文件夾內的 bootstrap.css 和 bootstrap-min.css 文件上。html
點擊這裏,查看可用的字體圖標列表。html5
下面的 CSS 規則構成 glyphicon class。jquery
@font-face { font-family: 'Glyphicons Halflings'; src: url('../fonts/glyphicons-halflings-regular.eot'); src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../fonts/glyphicons-halflings-regular.woff') format('woff'), url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg'); } .glyphicon { position: relative; top: 1px; display: inline-block; font-family: 'Glyphicons Halflings'; -webkit-font-smoothing: antialiased; font-style: normal; font-weight: normal; line-height: 1; -moz-osx-font-smoothing: grayscale; }
因此 font-face 規則其實是在找到 glyphicons 地方聲明 font-family 和位置。web
.glyphicon class 聲明一個從頂部偏移 1px 的相對位置,呈現爲 inline-block,聲明字體,規定 font-style 和 font-weight 爲 normal,設置行高爲 1。除此以外,使用 -webkit-font-smoothing: antialiased 和 -moz-osx-font-smoothing: grayscale; 得到跨瀏覽器的一致性。bootstrap
而後,這裏的瀏覽器
.glyphicon:empty {
width: 1em;
}
是空的 glyphicon。網絡
這裏有 200 個 class,每一個 class 針對一個圖標。這些 class 的常見格式以下:app
.glyphicon-keyword:before { content: "hexvalue"; }
好比,使用的 user 圖標,它的 class 以下:svg
.glyphicon-user:before { content: "\e008"; }
如需使用圖標,只須要簡單地使用下面的代碼便可。請在圖標和文本之間保留適當的空間。
<span class="glyphicon glyphicon-search"></span>
下面的實例演示瞭如何使用字體圖標:
<!DOCTYPE html> <html> <head> <title>Bootstrap 實例 - 如何使用字體圖標</title> <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <script src="/scripts/jquery.min.js"></script> <script src="/bootstrap/js/bootstrap.min.js"></script> </head> <body> <p> <button type="button" class="btn btn-default"> <span class="glyphicon glyphicon-sort-by-attributes"></span> </button> <button type="button" class="btn btn-default"> <span class="glyphicon glyphicon-sort-by-attributes-alt"></span> </button> <button type="button" class="btn btn-default"> <span class="glyphicon glyphicon-sort-by-order"></span> </button> <button type="button" class="btn btn-default"> <span class="glyphicon glyphicon-sort-by-order-alt"></span> </button> </p> <button type="button" class="btn btn-default btn-lg"> <span class="glyphicon glyphicon-user"></span> User </button> <button type="button" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-user"></span> User </button> <button type="button" class="btn btn-default btn-xs"> <span class="glyphicon glyphicon-user"></span> User </button> </body> </html>
結果以下所示:
<!DOCTYPE html> <html> <head> <title>導航欄的字體圖標</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Bootstrap --> <link href="http://apps.bdimg.com/libs/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"> <style> body { padding-top: 50px; padding-left: 50px; } </style> <!--[if lt IE 9]> <script src="http://apps.bdimg.com/libs/html5shiv/3.7/html5shiv.min.js"></script> <![endif]--> </head> <body> <div class="navbar navbar-fixed-top navbar-inverse" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Project name</a> </div> <div class="collapse navbar-collapse"> <ul class="nav navbar-nav"> <li class="active"><a href="#"><span class="glyphicon glyphicon-home">Home</span></a></li> <li><a href="#shop"><span class="glyphicon glyphicon-shopping-cart">Shop</span></a></li> <li><a href="#support"><span class="glyphicon glyphicon-headphones">Support</span></a></li> </ul> </div><!-- /.nav-collapse --> </div><!-- /.container --> </div> <!-- jQuery (Bootstrap 插件須要引入) --> <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> <!-- 包含了全部編譯插件 --> <script src="http://apps.bdimg.com/libs/bootstrap/3.2.0/js/bootstrap.min.js"></script> </body> </html>
定製字體圖標
咱們已經看到如何使用字體圖標,接下來咱們看看如何定製字體圖標。
咱們將以上面的實例開始,並經過改變字體尺寸、顏色和應用文本陰影來進行定製圖標。
下面是開始的代碼:
<button type="button" class="btn btn-primary btn-lg"> <span class="glyphicon glyphicon-user"></span> User </button>
效果以下所示:
經過增長或減小圖標的字體尺寸,您能夠讓圖標看起來更大或更小。
<button type="button" class="btn btn-primary btn-lg" style="font-size: 60px"> <span class="glyphicon glyphicon-user"></span> User </button>
<button type="button" class="btn btn-primary btn-lg" style="color: rgb(212, 106, 64);"> <span class="glyphicon glyphicon-user"></span> User </button>
<button type="button" class="btn btn-primary btn-lg" style="text-shadow: black 5px 3px 3px;"> <span class="glyphicon glyphicon-user"></span> User </button>