Bootstrap 字体图标(Glyphicons)

本章将解说字体图标(Glyphicons),并经过一些实例了解它的运用。Bootstrap 绑缚了 200 多种字体格局的字形。首要让咱们先来了解一下什么是字体图标。


什么是字体图标?

字体图标是在 Web 项目中运用的图标字体。尽管, 需求商业答应,可是您能够经过根据项目的 Bootstrap 来免费运用这些图标。

为了表明对图标作者的感谢,期望您在运用时加上 GLYPHICONS 网站的链接。


获取字体图标

咱们现已在 环境装置 章节下载了 Bootstrap 3.x 版别,并了解了它的目录结构。在 fonts 文件夹内能够找到字体图标,它包含了下列这些文件:

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

相关的 CSS 规矩写在 dist 文件夹内的 css 文件夹内的 bootstrap.cssbootstrap-min.css 文件上。

字体图标列表

点击这儿,检查可用的字体图标列表。


CSS 规矩解说

下面的 CSS 规矩构成 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 规矩实际上是在找到 glyphicons 当地声明 font-family 和方位。

.glyphicon class 声明一个从顶部偏移 1px 的相对方位,呈现为 inline-block,声明字体,规则 font-style 和 font-weight 为 normal,设置行高为 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";
}

用法

如需运用图标,只需求简略地运用下面的代码即可。请在图标和文本之间保存恰当的空间。

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

在线定制字体图标


图标列表

图标 类名 实例
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 测验一下