小兔网

简介

在本教程中,您将学习如何使用 Bootstrap 2.0 工具包来创建图标。Bootstrap 图标由 Glyphicons 提供。如果您在项目中使用这些图标,请确保提供 Glyphicons 的链接。

作为 CSS Sprite 使用

所有的图标图像都捆绑到一起,并通过 background-position CSS 属性使用。图像文件是 glyphicons-halflings.png,位于 Bootstrap 主文件夹下的 img 文件夹。图标的样式以及背景位置是在 bootstrap.css 的行号 1168 到 1544 中规定。请注意,为避免冲突,所有的图标 class 都以 "icon-" 为前缀。Bootstrap 2.0 中定义了 120 个图标 class。

如何在您自己的站点或 app 中使用图标

下面是在站点或 app 中使用图标的通用语法:

 <i class="icon_class_name"></i> 

其中 "icon_class_name" 是在 bootstrap.css 中定义的图标 class 的名称(例如:icon-music、icon-star、icon-user 等)。

如果您想要使用白色图标,然后添加一个额外的 icon-white class,如下所示:

<i class="icon_class_name icon-white"></i>  </i>

如果您想要在使用图标时带上一些字符串,记得在 之后添加一些空格。在按钮中使用图标或者一些导航链接中可能会出现这种情况。

实例:一个搜索表单

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>使用Bootstrap 版本2.0 中搜索图标的实例</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Example of using icons in search form - Bootstrap version 2.0 from w3cschool.cc">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="https://zhishitu.com/" rel="stylesheet">
    <style type="text/css">
        form {
            margin-top: 50px;
        }
    </style>
    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
    <script src="/images/2021/06/11/202106110607182550850.js"></script>
    <![endif]-->

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="https://zhishitu.com/">
    <link rel="apple-touch-icon" href="https://zhishitu.com/">
    <link rel="apple-touch-icon" sizes="72x72" href="https://zhishitu.com/">
    <link rel="apple-touch-icon" sizes="114x114" href="https://zhishitu.com/">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="span12">
            <form class="well form-search">
                <input type="text" class="input-medium search-query">
                <button type="submit" class="btn"><i class="icon-search"></i> 搜索</button>
            </form>
        </div>
    </div>
    <footer>
        <p>&copy; Company 2013</p>
    </footer>
</div>
</body>
</html>

在线查看

在不同的浏览器窗口查看上面实例。

实例:在导航中使用图标

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Bootstrap 版本 2.0 在导航中使用图标</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Example icons in navigation with Bootstrap version 2.0 from w3cschool.cc">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="https://zhishitu.com/" rel="stylesheet">
    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
    <script src="/images/2021/06/11/202106110607182550850.js"></script>
    <![endif]-->

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="https://zhishitu.com/">
    <link rel="apple-touch-icon" href="https://zhishitu.com/">
    <link rel="apple-touch-icon" sizes="72x72" href="https://zhishitu.com/">
    <link rel="apple-touch-icon" sizes="114x114" href="https://zhishitu.com/">
</head>

<body>
<div class="navbar navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </a>
            <a class="brand" href="https://zhishitu.com/">runoob</a>
            <div class="nav-collapse">
                <ul class="nav">
                    <li class="active"><a href="https://zhishitu.com/"><i class="icon-user icon-white"></i> Home</a></li>
                    <li><a href="https://zhishitu.com/"><i class="icon-user icon-white"></i> User</a></li>
                    <li><a href="https://zhishitu.com/"><i class="icon-download icon-white"></i> Downlaod</a></li>
                    <li><a href="https://zhishitu.com/"><i class="icon-upload icon-white"></i> Upload</a></li>
                    <li><a href="https://zhishitu.com/"><i class="icon-play-circle icon-white"></i> Play Circle</a></li>
                    <li><a href="https://zhishitu.com/"><i class="icon-bookmark icon-white"></i> Bookmark</a></li>
                    <li><a href="https://zhishitu.com/"><i class="icon-gift icon-white"></i> Gift</a></li>
                </ul>
            </div><!--/.nav-collapse -->
        </div>
    </div>
</div>

<hr>
</div> <!-- /container -->

<!-- Le javascript
================================================== -->
<!-- 放置在文档的页面底部加快加载速度 -->
<script src="/images/2021/06/11/202106110607182559332.js"></script>
<script src="/images/2021/06/11/202106110607182562113.js"></script>
<script src="/images/2021/06/11/202106110607182564214.js"></script>
<script src="/images/2021/06/11/202106110607182566525.js"></script>
<script src="/images/2021/06/11/202106110607182569056.js"></script>
<script src="/images/2021/06/11/202106110607182572397.js"></script>
<script src="/images/2021/06/11/202106110607182574738.js"></script>
<script src="/images/2021/06/11/202106110607182576959.js"></script>
<script src="/images/2021/06/11/2021061106071825793910.js"></script>
<script src="/images/2021/06/11/2021061106071825822411.js"></script>
<script src="/images/2021/06/11/2021061106071825842012.js"></script>
<script src="/images/2021/06/11/2021061106071825867113.js"></script>
<script src="/images/2021/06/11/2021061106071825891214.js"></script>

</body>
</html>

在线查看

在不同的浏览器窗口查看上面实例。

实例:在按钮和按钮组中使用图标

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Bootstrap 版本 2.0 在按钮和按钮组中使用图标</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Example of using icons in buttons and button groups - Bootstrap version 2.0 from w3cschool.cc">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="https://zhishitu.com/" rel="stylesheet">
    <style type="text/css">
        form {
            margin-top: 50px;
        }
    </style>
    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
    <script src="/images/2021/06/11/202106110607182550850.js"></script>
    <![endif]-->

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="https://zhishitu.com/">
    <link rel="apple-touch-icon" href="https://zhishitu.com/">
    <link rel="apple-touch-icon" sizes="72x72" href="https://zhishitu.com/">
    <link rel="apple-touch-icon" sizes="114x114" href="https://zhishitu.com/">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="span4">
            <div class="btn-toolbar" style="margin-bottom: 9px">
                <div class="btn-group">
                    <a class="btn" href="https://zhishitu.com/"><i class="icon-align-left"></i></a>
                    <a class="btn" href="https://zhishitu.com/"><i class="icon-align-center"></i></a>
                    <a class="btn" href="https://zhishitu.com/"><i class="icon-align-right"></i></a>
                    <a class="btn" href="https://zhishitu.com/"><i class="icon-align-justify"></i></a>
                </div>
                <div class="btn-group">
                    <a class="btn btn-primary" href="https://zhishitu.com/"><i class="icon-user icon-white"></i> User</a>
                    <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="https://zhishitu.com/"><span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="https://zhishitu.com/"><i class="icon-pencil"></i> Edit</a></li>
                        <li><a href="https://zhishitu.com/"><i class="icon-trash"></i> Delete</a></li>
                        <li><a href="https://zhishitu.com/"><i class="icon-ban-circle"></i> Ban</a></li>
                        <li class="divider"></li>
                        <li><a href="https://zhishitu.com/"><i class="i"></i> Make admin</a></li>
                    </ul>
                </div>
            </div>
            <p>
                <a class="btn" href="https://zhishitu.com/"><i class="icon-refresh"></i> Refresh</a>
                <a class="btn btn-success" href="https://zhishitu.com/"><i class="icon-shopping-cart icon-white"></i> Checkout</a>
                <a class="btn btn-danger" href="https://zhishitu.com/"><i class="icon-trash icon-white"></i> Delete</a>
            </p>
            <p>
                <a class="btn btn-large" href="https://zhishitu.com/"><i class="icon-comment"></i> Comment</a>
                <a class="btn btn-small" href="https://zhishitu.com/"><i class="icon-cog"></i> Settings</a>
                <a class="btn btn-small btn-info" href="https://zhishitu.com/"><i class="icon-info-sign icon-white"></i> More Info</a>
            </p>
        </div>
    </div>
</div>
<!-- Le javascript
================================================== -->
<!-- 放置在文档的页面底部加快加载速度 -->
<script src="/images/2021/06/11/202106110607182559332.js"></script>
<script src="/images/2021/06/11/202106110607182562113.js"></script>
<script src="/images/2021/06/11/202106110607182564214.js"></script>
<script src="/images/2021/06/11/202106110607182566525.js"></script>
<script src="/images/2021/06/11/202106110607182569056.js"></script>
<script src="/images/2021/06/11/202106110607182572397.js"></script>
<script src="/images/2021/06/11/202106110607182574738.js"></script>
<script src="/images/2021/06/11/202106110607182576959.js"></script>
<script src="/images/2021/06/11/2021061106071825793910.js"></script>
<script src="/images/2021/06/11/2021061106071825822411.js"></script>
<script src="/images/2021/06/11/2021061106071825842012.js"></script>
<script src="/images/2021/06/11/2021061106071825867113.js"></script>
<script src="/images/2021/06/11/2021061106071825891214.js"></script>
</body>
</html>

在线查看

在不同的浏览器窗口查看上面实例。

点击这里,下载本教程中使用到的所有 HTML、CSS、JS 和图片文件。