Bootstrap 圖標(biāo)

2018-11-08 16:54 更新

Bootstrap 圖標(biāo)

簡(jiǎn)介

在本教程中,您將學(xué)習(xí)如何使用 Bootstrap 2.0 工具包來(lái)創(chuàng)建圖標(biāo)。Bootstrap 圖標(biāo)由 Glyphicons 提供。如果您在項(xiàng)目中使用這些圖標(biāo),請(qǐng)確保提供 Glyphicons 的鏈接。

作為 CSS Sprite 使用

所有的圖標(biāo)圖像都捆綁到一起,并通過(guò) background-position CSS 屬性使用。圖像文件是 glyphicons-halflings.png,位于 Bootstrap 主文件夾下的 img 文件夾。圖標(biāo)的樣式以及背景位置是在 bootstrap.css 的行號(hào) 1168 到 1544 中規(guī)定。請(qǐng)注意,為避免沖突,所有的圖標(biāo) class 都以 "icon-" 為前綴。Bootstrap 2.0 中定義了 120 個(gè)圖標(biāo) class。

如何在您自己的站點(diǎn)或 app 中使用圖標(biāo)

下面是在站點(diǎn)或 app 中使用圖標(biāo)的通用語(yǔ)法:

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

其中 "icon_class_name" 是在 bootstrap.css 中定義的圖標(biāo) class 的名稱(chēng)(例如:icon-music、icon-star、icon-user 等)。

如果您想要使用白色圖標(biāo),然后添加一個(gè)額外的 icon-white class,如下所示:

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

如果您想要在使用圖標(biāo)時(shí)帶上一些字符串,記得在 之后添加一些空格。在按鈕中使用圖標(biāo)或者一些導(dǎo)航鏈接中可能會(huì)出現(xiàn)這種情況。

實(shí)例:一個(gè)搜索表單

實(shí)例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>使用Bootstrap 版本2.0 中搜索圖標(biāo)的實(shí)例</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.cn">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="../bootstrap/twitter-bootstrap-v2/docs/assets/css/bootstrap.css" 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="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="../bootstrap/twitter-bootstrap-v2/docs/examples/images/favicon.ico">
    <link rel="apple-touch-icon" href="../bootstrap/twitter-bootstrap-v2/docs/examples/images/apple-touch-icon.png">
    <link rel="apple-touch-icon" sizes="72x72" href="../bootstrap/twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="114x114" href="../bootstrap/twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-114x114.png">
</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>Copyright &copy; 2016 w3cschool.cn</p>
    </footer>
</div>
</body>
</html>

在線查看

在不同的瀏覽器窗口查看上面實(shí)例。

實(shí)例:在導(dǎo)航中使用圖標(biāo)

實(shí)例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Bootstrap 版本 2.0 在導(dǎo)航中使用圖標(biāo)</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.cn">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="../bootstrap/twitter-bootstrap-v2/docs/assets/css/bootstrap.css" rel="stylesheet">
    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
    <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="../bootstrap/twitter-bootstrap-v2/docs/examples/images/favicon.ico">
    <link rel="apple-touch-icon" href="../bootstrap/twitter-bootstrap-v2/docs/examples/images/apple-touch-icon.png">
    <link rel="apple-touch-icon" sizes="72x72" href="../bootstrap/twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="114x114" href="../bootstrap/twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-114x114.png">
</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="#">W3Cschool</a>
            <div class="nav-collapse">
                <ul class="nav">
                    <li class="active"><a href="#"><i class="icon-user icon-white"></i> Home</a></li>
                    <li><a href="#about"><i class="icon-user icon-white"></i> User</a></li>
                    <li><a href="#about"><i class="icon-download icon-white"></i> Downlaod</a></li>
                    <li><a href="#about"><i class="icon-upload icon-white"></i> Upload</a></li>
                    <li><a href="#about"><i class="icon-play-circle icon-white"></i> Play Circle</a></li>
                    <li><a href="#about"><i class="icon-bookmark icon-white"></i> Bookmark</a></li>
                    <li><a href="#about"><i class="icon-gift icon-white"></i> Gift</a></li>
                </ul>
            </div><!--/.nav-collapse -->
        </div>
    </div>
</div>

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

<!-- Le javascript
================================================== -->
<!-- 放置在文檔的頁(yè)面底部加快加載速度 -->
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/jquery.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-transition.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-alert.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-modal.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-dropdown.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-scrollspy.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-tab.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-tooltip.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-popover.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-button.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-collapse.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-carousel.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-typeahead.js"></script>

</body>
</html>

在線查看

在不同的瀏覽器窗口查看上面實(shí)例。

實(shí)例:在按鈕和按鈕組中使用圖標(biāo)

實(shí)例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Bootstrap 版本 2.0 在按鈕和按鈕組中使用圖標(biāo)</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.cn">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="../bootstrap/twitter-bootstrap-v2/docs/assets/css/bootstrap.css" 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="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="../bootstrap/twitter-bootstrap-v2/docs/examples/images/favicon.ico">
    <link rel="apple-touch-icon" href="../bootstrap/twitter-bootstrap-v2/docs/examples/images/apple-touch-icon.png">
    <link rel="apple-touch-icon" sizes="72x72" href="../bootstrap/twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="114x114" href="../bootstrap/twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-114x114.png">
</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="#"><i class="icon-align-left"></i></a>
                    <a class="btn" href="#"><i class="icon-align-center"></i></a>
                    <a class="btn" href="#"><i class="icon-align-right"></i></a>
                    <a class="btn" href="#"><i class="icon-align-justify"></i></a>
                </div>
                <div class="btn-group">
                    <a class="btn btn-primary" href="#"><i class="icon-user icon-white"></i> User</a>
                    <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#"><i class="icon-pencil"></i> Edit</a></li>
                        <li><a href="#"><i class="icon-trash"></i> Delete</a></li>
                        <li><a href="#"><i class="icon-ban-circle"></i> Ban</a></li>
                        <li class="divider"></li>
                        <li><a href="#"><i class="i"></i> Make admin</a></li>
                    </ul>
                </div>
            </div>
            <p>
                <a class="btn" href="#"><i class="icon-refresh"></i> Refresh</a>
                <a class="btn btn-success" href="#"><i class="icon-shopping-cart icon-white"></i> Checkout</a>
                <a class="btn btn-danger" href="#"><i class="icon-trash icon-white"></i> Delete</a>
            </p>
            <p>
                <a class="btn btn-large" href="#"><i class="icon-comment"></i> Comment</a>
                <a class="btn btn-small" href="#"><i class="icon-cog"></i> Settings</a>
                <a class="btn btn-small btn-info" href="#"><i class="icon-info-sign icon-white"></i> More Info</a>
            </p>
        </div>
    </div>
</div>
<!-- Le javascript
================================================== -->
<!-- 放置在文檔的頁(yè)面底部加快加載速度 -->
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/jquery.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-transition.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-alert.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-modal.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-dropdown.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-scrollspy.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-tab.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-tooltip.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-popover.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-button.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-collapse.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-carousel.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-typeahead.js"></script>
</body>
</html>

在線查看

在不同的瀏覽器窗口查看上面實(shí)例。

點(diǎn)擊這里,下載本教程中使用到的所有 HTML、CSS、JS 和圖片文件。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)