runoops.com

Bootstrap 字体图标(Glyphicons)

本章将讲解字体图标(Glyphicons),并通过一些实例了解它的使用。Bootstrap 捆绑了 200 多种字体格式的字形。首先让我们先来理解一下什么是字体图标。


什么是字体图标?

字体图标是在 Web 项目中使用的图标字体。虽然,Glyphicons Halflings 需要商业许可,但是您可以通过基于项目的 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.css 和 bootstrap-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>

下面的实例演示了如何使用字体图标:

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

带有字体图标的导航栏

<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="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<!-- 包含了所有编译插件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>

定制字体图标

我们已经看到如何使用字体图标,接下来我们看看如何定制字体图标。

我们将以上面的实例开始,并通过改变字体尺寸、颜色和应用文本阴影来进行定制图标。

下面是开始的代码:

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