3-25 图标(一)
本节编程练习不计算学习进度,请电脑登录imooc.com操作

图标(一)

200个icon

这里说的图标就是Web制作中常看到的小icon图标,可以说这些小icon图标是一个优秀Web中不可缺少的一部分,起到画龙点睛的效果。在Bootstrap框架中也为大家提供了近200个不同的icon图片,而这些图标都是使用CSS3@font-face属性配合字体来实现的icon效果。

放心使用

在具体介绍Bootstrap的icon图标之前,我们首先要感谢glyphicons.com网站,因为Bootstrap框架中图标都是glyphicons.com这个商业网站提供的,并且免费授权给Bootstrap框架使用,所以大家可以放心使用在自己的项目当中。

Bootstrap框架将内部样式也提取出来:
1、LESS版本:对应源文件为glyphicons.less文件
2、Sass版本:对应源文件为_glyphicons.scss文件
3、编译后的Bootstrap版本:查看bootstrap.css文件第2375行~第2992行

原理分析

Bootstrap框架中的图标都是字体图标,其实现原理就是通过@font-face属性加载了字体。
/*源码请查看bootstrap.css文件第2357行~第2380行*/

@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');
}

大家或许会问,这些字体我去哪里获取。如果你是从前面一直阅读过来,我们在介绍文件结构那一节就已介绍过。在Bootstrap框架中有一个fonts的目录,这个目录中提供的字体文件就是用于制作icon的字体文件。
自定义完字体之后,需要对icon设置一个默认样式,在Bootstrap框架中是通过给元素添加glyphicon类名来实现,然后通过伪元素“:before”的“content”属性调取对应的icon编码:
/*源码请查看bootstrap.css文件第2381行~第2992行*/

.glyphicon {
position: relative;
top: 1px;
display: inline-block;
font-family: 'Glyphicons Halflings';
font-style: normal;
font-weight: normal;
line-height: 1;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.glyphicon-asterisk:before {
content: "\2a";
}
….

任务

本小节没有代码任务,单击“提交”按钮进行下一个小节学习。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>图标</title>
  6. <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <span class="glyphicon glyphicon-search"></span>
  10. <span class="glyphicon glyphicon-asterisk"></span>
  11. <span class="glyphicon glyphicon-plus"></span>
  12. <span class="glyphicon glyphicon-cloud"></span>
  13. </body>
  14. </html>
  1. body{
  2. padding: 100px;
  3. }
下一节