Bootstrap 字體圖標(Glyphicons)

2018-09-15 12:19 更新

Bootstrap 字體圖標(Glyphicons)

本章將講解字體圖標(Glyphicons),并通過一些實例了解它的使用。Bootstrap 捆綁了 200 多種字體格式的字形。首先讓我們先來理解一下什么是字體圖標。


什么是字體圖標?

字體圖標是在 Web 項目中使用的圖標字體。雖然,Glyphicons Halflings 需要商業(yè)許可,但是您可以通過基于項目的 Bootstrap 來免費使用這些圖標。

為了表示對圖標作者的感謝,希望您在使用時加上 GLYPHICONS 網(wǎng)站的鏈接。


獲取字體圖標

我們已經(jīng)在 環(huán)境安裝 章節(jié)下載了 Bootstrap 3.x 版本,并理解了它的目錄結(jié)構(gòu)。在 fonts 文件夾內(nèi)可以找到字體圖標,它包含了下列這些文件:

  • glyphicons-halflings-regular.eot
  • glyphicons-halflings-regular.svg
  • glyphicons-halflings-regular.ttf
  • glyphicons-halflings-regular.woff

相關(guān)的 CSS 規(guī)則寫在 dist 文件夾內(nèi)的 css 文件夾內(nèi)的 bootstrap.cssbootstrap-min.css 文件上。

字體圖標列表

點擊這里,查看可用的字體圖標列表。


CSS 規(guī)則解釋

下面的 CSS 規(guī)則構(gòu)成 glyphicon class。

@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 規(guī)則實際上是在找到 glyphicons 地方聲明 font-family 和位置。

.glyphicon class 聲明一個從頂部偏移 1px 的相對位置,呈現(xiàn)為 inline-block,聲明字體,規(guī)定 font-style 和 font-weight 為 normal,設(shè)置行高為 1。除此之外,使用 -webkit-font-smoothing: antialiased-moz-osx-font-smoothing: grayscale; 獲得跨瀏覽器的一致性。

然后,這里的

.glyphicon:empty {
  width: 1em;
}

是空的 glyphicon。

這里有 200 個 class,每個 class 針對一個圖標。這些 class 的常見格式如下:

.glyphicon-keyword:before {
  content: "hexvalue";
}

比如,使用的 user 圖標,它的 class 如下:

.glyphicon-user:before {
  content: "\e008";
}

用法

如需使用圖標,只需要簡單地使用下面的代碼即可。請在圖標和文本之間保留適當?shù)目臻g。

<span class="glyphicon glyphicon-search"></span>

下面的實例演示了如何使用字體圖標:

<!DOCTYPE html>
<html>
<head>
   <title>Bootstrap 實例 - 如何使用字體圖標</title>
   <link  rel="external nofollow" target="_blank"  rel="stylesheet">
   <script src="http://cdn.bootcss.com/jquery/2.1.1/jquery.min.js" rel="external nofollow" ></script>
   <script src="http://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" rel="external nofollow" ></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>

結(jié)果如下所示:

如何使用字體圖標

帶有導航欄的字體圖標

<!DOCTYPE html>
<html>
  <head>
    <title>導航欄的字體圖標</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link  rel="external nofollow" target="_blank"  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" rel="external nofollow" ></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" rel="external nofollow" ></script>
    <!-- 包含了所有編譯插件 -->
    <script src="http://apps.bdimg.com/libs/bootstrap/3.2.0/js/bootstrap.min.js" rel="external nofollow" ></script>
  </body>
</html>      


定制字體圖標

我們已經(jīng)看到如何使用字體圖標,接下來我們看看如何定制字體圖標。

我們將以上面的實例開始,并通過改變字體尺寸、顏色和應(yīng)用文本陰影來進行定制圖標。

下面是開始的代碼:

<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>

應(yīng)用文本陰影

<button type="button" class="btn btn-primary btn-lg" style="text-shadow: black 5px 3px 3px;">
  <span class="glyphicon glyphicon-user"></span> User
</button>

在線定制字體圖標


圖標列表

圖標 類名 實例
glyphicon glyphicon-asterisk 嘗試一下
glyphicon glyphicon-plus 嘗試一下
glyphicon glyphicon-minus 嘗試一下
glyphicon glyphicon-euro 嘗試一下
glyphicon glyphicon-cloud 嘗試一下
glyphicon glyphicon-envelope 嘗試一下
glyphicon glyphicon-pencil 嘗試一下
glyphicon glyphicon-glass 嘗試一下
glyphicon glyphicon-music 嘗試一下
glyphicon glyphicon-search 嘗試一下
glyphicon glyphicon-heart 嘗試一下
glyphicon glyphicon-star 嘗試一下
glyphicon glyphicon-star-empty 嘗試一下
glyphicon glyphicon-user 嘗試一下
glyphicon glyphicon-film 嘗試一下
glyphicon glyphicon-th-large 嘗試一下
glyphicon glyphicon-th 嘗試一下
glyphicon glyphicon-th-list 嘗試一下
glyphicon glyphicon-ok 嘗試一下
glyphicon glyphicon-remove 嘗試一下
glyphicon glyphicon-zoom-in 嘗試一下
glyphicon glyphicon-zoom-out 嘗試一下
glyphicon glyphicon-off 嘗試一下
glyphicon glyphicon-signal 嘗試一下
glyphicon glyphicon-cog 嘗試一下
glyphicon glyphicon-trash 嘗試一下
glyphicon glyphicon-home 嘗試一下
glyphicon glyphicon-file 嘗試一下
glyphicon glyphicon-time 嘗試一下
glyphicon glyphicon-road 嘗試一下
glyphicon glyphicon-download-alt 嘗試一下
glyphicon glyphicon-download 嘗試一下
glyphicon glyphicon-upload 嘗試一下
glyphicon glyphicon-inbox 嘗試一下
glyphicon glyphicon-play-circle 嘗試一下
glyphicon glyphicon-repeat 嘗試一下
glyphicon glyphicon-refresh 嘗試一下
glyphicon glyphicon-list-alt 嘗試一下
glyphicon glyphicon-lock 嘗試一下
glyphicon glyphicon-flag 嘗試一下
glyphicon glyphicon-headphones 嘗試一下
glyphicon glyphicon-volume-off 嘗試一下
glyphicon glyphicon-volume-down 嘗試一下
glyphicon glyphicon-volume-up 嘗試一下
glyphicon glyphicon-qrcode 嘗試一下
glyphicon glyphicon-barcode 嘗試一下
glyphicon glyphicon-tag 嘗試一下
glyphicon glyphicon-tags 嘗試一下
glyphicon glyphicon-book 嘗試一下
glyphicon glyphicon-bookmark 嘗試一下
glyphicon glyphicon-print 嘗試一下
glyphicon glyphicon-camera 嘗試一下
glyphicon glyphicon-font 嘗試一下
glyphicon glyphicon-bold 嘗試一下
glyphicon glyphicon-italic 嘗試一下
glyphicon glyphicon-text-height 嘗試一下
glyphicon glyphicon-text-width 嘗試一下
glyphicon glyphicon-align-left 嘗試一下
glyphicon glyphicon-align-center 嘗試一下
glyphicon glyphicon-align-right 嘗試一下
glyphicon glyphicon-align-justify 嘗試一下
glyphicon glyphicon-list 嘗試一下
glyphicon glyphicon-indent-left 嘗試一下
glyphicon glyphicon-indent-right 嘗試一下
glyphicon glyphicon-facetime-video 嘗試一下
glyphicon glyphicon-picture 嘗試一下
glyphicon glyphicon-map-marker 嘗試一下
glyphicon glyphicon-adjust 嘗試一下
glyphicon glyphicon-tint 嘗試一下
glyphicon glyphicon-edit 嘗試一下
glyphicon glyphicon-share 嘗試一下
glyphicon glyphicon-check 嘗試一下
glyphicon glyphicon-move 嘗試一下
glyphicon glyphicon-step-backward 嘗試一下
glyphicon glyphicon-fast-backward 嘗試一下
glyphicon glyphicon-backward 嘗試一下
glyphicon glyphicon-play 嘗試一下
glyphicon glyphicon-pause 嘗試一下
glyphicon glyphicon-stop 嘗試一下
glyphicon glyphicon-forward 嘗試一下
glyphicon glyphicon-fast-forward 嘗試一下
glyphicon glyphicon-step-forward 嘗試一下
glyphicon glyphicon-eject 嘗試一下
glyphicon glyphicon-chevron-left 嘗試一下
glyphicon glyphicon-chevron-right 嘗試一下
glyphicon glyphicon-plus-sign 嘗試一下
glyphicon glyphicon-minus-sign 嘗試一下
glyphicon glyphicon-remove-sign 嘗試一下
glyphicon glyphicon-ok-sign 嘗試一下
glyphicon glyphicon-question-sign 嘗試一下
glyphicon glyphicon-info-sign 嘗試一下
glyphicon glyphicon-screenshot 嘗試一下
glyphicon glyphicon-remove-circle 嘗試一下
glyphicon glyphicon-ok-circle 嘗試一下
glyphicon glyphicon-ban-circle 嘗試一下
glyphicon glyphicon-arrow-left 嘗試一下
glyphicon glyphicon-arrow-right 嘗試一下
glyphicon glyphicon-arrow-up 嘗試一下
glyphicon glyphicon-arrow-down 嘗試一下
glyphicon glyphicon-share-alt 嘗試一下
glyphicon glyphicon-resize-full 嘗試一下
glyphicon glyphicon-resize-small 嘗試一下
glyphicon glyphicon-exclamation-sign 嘗試一下
glyphicon glyphicon-gift 嘗試一下
glyphicon glyphicon-leaf 嘗試一下
glyphicon glyphicon-fire 嘗試一下
glyphicon glyphicon-eye-open 嘗試一下
glyphicon glyphicon-eye-close 嘗試一下
glyphicon glyphicon-warning-sign 嘗試一下
glyphicon glyphicon-plane 嘗試一下
glyphicon glyphicon-calendar 嘗試一下
glyphicon glyphicon-random 嘗試一下
glyphicon glyphicon-comment 嘗試一下
glyphicon glyphicon-magnet 嘗試一下
glyphicon glyphicon-chevron-up 嘗試一下
glyphicon glyphicon-chevron-down 嘗試一下
glyphicon glyphicon-retweet 嘗試一下
glyphicon glyphicon-shopping-cart 嘗試一下
glyphicon glyphicon-folder-close 嘗試一下
glyphicon glyphicon-folder-open 嘗試一下
glyphicon glyphicon-resize-vertical 嘗試一下
glyphicon glyphicon-resize-horizontal 嘗試一下
glyphicon glyphicon-hdd 嘗試一下
glyphicon glyphicon-bullhorn 嘗試一下
glyphicon glyphicon-bell 嘗試一下
glyphicon glyphicon-certificate 嘗試一下
glyphicon glyphicon-thumbs-up 嘗試一下
glyphicon glyphicon-thumbs-down 嘗試一下
glyphicon glyphicon-hand-right 嘗試一下
glyphicon glyphicon-hand-left 嘗試一下
glyphicon glyphicon-hand-up 嘗試一下
glyphicon glyphicon-hand-down 嘗試一下
glyphicon glyphicon-circle-arrow-right 嘗試一下
glyphicon glyphicon-circle-arrow-left 嘗試一下
glyphicon glyphicon-circle-arrow-up 嘗試一下
glyphicon glyphicon-circle-arrow-down 嘗試一下
glyphicon glyphicon-globe 嘗試一下
glyphicon glyphicon-wrench 嘗試一下
glyphicon glyphicon-tasks 嘗試一下
glyphicon glyphicon-filter 嘗試一下
glyphicon glyphicon-briefcase 嘗試一下
glyphicon glyphicon-fullscreen 嘗試一下
glyphicon glyphicon-dashboard 嘗試一下
glyphicon glyphicon-paperclip 嘗試一下
glyphicon glyphicon-heart-empty 嘗試一下
glyphicon glyphicon-link 嘗試一下
glyphicon glyphicon-phone 嘗試一下
glyphicon glyphicon-pushpin 嘗試一下
glyphicon glyphicon-usd 嘗試一下
glyphicon glyphicon-gbp 嘗試一下
glyphicon glyphicon-sort 嘗試一下
glyphicon glyphicon-sort-by-alphabet 嘗試一下
glyphicon glyphicon-sort-by-alphabet-alt 嘗試一下
glyphicon glyphicon-sort-by-order 嘗試一下
glyphicon glyphicon-sort-by-order-alt 嘗試一下
glyphicon glyphicon-sort-by-attributes 嘗試一下
glyphicon glyphicon-sort-by-attributes-alt 嘗試一下
glyphicon glyphicon-unchecked 嘗試一下
glyphicon glyphicon-expand 嘗試一下
glyphicon glyphicon-collapse-down 嘗試一下
glyphicon glyphicon-collapse-up 嘗試一下
glyphicon glyphicon-log-in 嘗試一下
glyphicon glyphicon-flash 嘗試一下
glyphicon glyphicon-log-out 嘗試一下
glyphicon glyphicon-new-window 嘗試一下
glyphicon glyphicon-record 嘗試一下
glyphicon glyphicon-save 嘗試一下
glyphicon glyphicon-open 嘗試一下
glyphicon glyphicon-saved 嘗試一下
glyphicon glyphicon-import 嘗試一下
glyphicon glyphicon-export 嘗試一下
glyphicon glyphicon-send 嘗試一下
glyphicon glyphicon-floppy-disk 嘗試一下
glyphicon glyphicon-floppy-saved 嘗試一下
glyphicon glyphicon-floppy-remove 嘗試一下
glyphicon glyphicon-floppy-save 嘗試一下
glyphicon glyphicon-floppy-open 嘗試一下
glyphicon glyphicon-credit-card 嘗試一下
glyphicon glyphicon-transfer 嘗試一下
glyphicon glyphicon-cutlery 嘗試一下
glyphicon glyphicon-header 嘗試一下
glyphicon glyphicon-compressed 嘗試一下
glyphicon glyphicon-earphone 嘗試一下
glyphicon glyphicon-phone-alt 嘗試一下
glyphicon glyphicon-tower 嘗試一下
glyphicon glyphicon-stats 嘗試一下
glyphicon glyphicon-sd-video 嘗試一下
glyphicon glyphicon-hd-video 嘗試一下
glyphicon glyphicon-subtitles 嘗試一下
glyphicon glyphicon-sound-stereo 嘗試一下
glyphicon glyphicon-sound-dolby 嘗試一下
glyphicon glyphicon-sound-5-1 嘗試一下
glyphicon glyphicon-sound-6-1 嘗試一下
glyphicon glyphicon-sound-7-1 嘗試一下
glyphicon glyphicon-copyright-mark 嘗試一下
glyphicon glyphicon-registration-mark 嘗試一下
glyphicon glyphicon-cloud-download 嘗試一下
glyphicon glyphicon-cloud-upload 嘗試一下
glyphicon glyphicon-tree-conifer 嘗試一下
glyphicon glyphicon-tree-deciduous 嘗試一下
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號