Bootstrap 字体图标

前言

  • Bootstrap 捆绑了 200 多种字体格式的字形。

  • 字体图标 (Glyphicons) 是在 Web 项目中使用的图标字体。虽然,Glyphicons Halflings 需要商业许可,但是可以通过基于项目的 Bootstrap 来免费使用这些图标。为了表示对图标作者的感谢,希望在使用时加上 GLYPHICONS 网站的链接。

  • 在 Bootstrap 目录结构的 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 文件上。

1、CSS 规则解释

  • 下面的 CSS 规则构成 .glyphicon class。

  • 实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    @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; 获得跨浏览器的一致性。

  • 然后,这里的

    1
    2
    3
    .glyphicon:empty {
    width: 1em;
    }
  • 是空的 glyphicon。

  • 这里有 200 个 class,每个 class 针对一个图标。这些 class 的常见格式如下:

    1
    2
    3
    .glyphicon-keyword:before {
    content: "hexvalue";
    }
  • 比如,使用的 user 图标,它的 class 如下:

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

2、用法

  • 如需使用图标,只需要简单地使用下面的代码即可。在图标和文本之间保留适当的空间。

    1
    <span class="glyphicon glyphicon-图标名称"></span>
  • 实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <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>

    <br /><br />

    <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>
  • 效果 🔗

3、带有字体图标的导航栏

  • 实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <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"></span> Home</a></li>
    <li><a href="#shop"><span class="glyphicon glyphicon-shopping-cart"></span> Shop</a></li>
    <li><a href="#support"><span class="glyphicon glyphicon-headphones"></span> Support</a></li>
    </ul>
    </div>
    </div>
    </div>
  • 效果 🔗

4、定制字体图标

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

    1
    2
    3
    <button type="button" class="btn btn-primary btn-lg">
    <span class="glyphicon glyphicon-user"></span> User
    </button>
  • 效果

4.1 定制字体尺寸

  • 通过增加或减少图标的字体尺寸,可以让图标看起来更大或更小。

  • 实例

    1
    2
    3
    <button type="button" class="btn btn-primary btn-lg" style="font-size: 60px;">
    <span class="glyphicon glyphicon-user"></span> User
    </button>
  • 效果

4.2 定制字体颜色

  • 实例

    1
    2
    3
    <button type="button" class="btn btn-primary btn-lg" style="color: rgb(212, 106, 64);">
    <span class="glyphicon glyphicon-user"></span> User
    </button>
  • 效果

4.3 应用文本阴影

  • 实例

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

4.4 在线定制字体图标

5、图标列表

文章目录
  1. 1. 前言
  2. 2. 1、CSS 规则解释
  3. 3. 2、用法
  4. 4. 3、带有字体图标的导航栏
  5. 5. 4、定制字体图标
    1. 5.1. 4.1 定制字体尺寸
    2. 5.2. 4.2 定制字体颜色
    3. 5.3. 4.3 应用文本阴影
    4. 5.4. 4.4 在线定制字体图标
  6. 6. 5、图标列表
隐藏目录