如何在bootstrap导航栏中添加自己的图标

时间:2014-03-24 18:09:00

标签: html css twitter-bootstrap icons navbar

在项目上工作,我在导航栏上开始了。我想知道是否有可能在每个菜单栏旁边添加我自己的图标?例如,此图标:https://www.dropbox.com/sh/0be3c7ub4245kd6/zNMrvFi6Wq

像这样:

enter image description here

代码:

<!-- Fixed navbar -->
    <div class="navbar navbar-default navbar-fixed-top" 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="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>

          </ul>

        </div><!--/.nav-collapse -->
      </div>
    </div>

    <div class="container">

      <!-- Main component for a primary marketing message or call to action -->
      <div class="jumbotron">
        <h1>Navbar example</h1>
        <p>This example is a quick exercise to illustrate how the default, static and fixed to top navbar work. It includes the responsive CSS and HTML, so it also adapts to your viewport and device.</p>
        <p>To see the difference between static and fixed top navbars, just scroll.</p>
        <p>
          <a class="btn btn-lg btn-primary" href="../../components/#navbar" role="button">View navbar docs &raquo;</a>
        </p>
      </div>

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

4 个答案:

答案 0 :(得分:13)

您可以使用字体真棒或引导字形图标。只需在glyphicon的文本之前添加这样的内容:

<span class="glyphicon glyphicon-search"></span>

或者这个字体真棒

<span class="fa fa-search"></span>

或者您可以创建图像图标并为图标创建css类。

Your updated demo

答案 1 :(得分:7)

这是一种不添加任何额外标记的方法,但您需要为每个锚链接添加唯一的类。

CSS:

.navbar-default .navbar-nav>.active>a:before,
.navbar-nav>li>a:before {
    background-repeat: no-repeat;
    background-position: 0 top;
    content: "";
    display: inline-block;
    height: 29px;
    margin-right: 10px;
    vertical-align: middle;
    width: 29px;
}

.navbar-default .navbar-nav>.active>a.home:before,
.navbar-nav>li>a.home:before { background-image: url('https://dl.dropboxusercontent.com/sh/0be3c7ub4245kd6/18Xnfucw14/home.png'); }

.navbar-default .navbar-nav>.active>a.about:before,
.navbar-nav>li>a.about:before {  background-image: url('https://dl.dropboxusercontent.com/sh/0be3c7ub4245kd6/DhXiON5YiL/kveld.png'); }

.navbar-default .navbar-nav>.active>a.contact:before,
.navbar-nav>li>a.contact:before {  background-image: url('https://dl.dropboxusercontent.com/sh/0be3c7ub4245kd6/_fidqgglaS/sol.png'); }

您可以在此处查看结果:http://jsfiddle.net/2WvpV/

你需要玩边距和填充。此外,这并不完美,因为你应该使用相对大小而不是像素。

答案 2 :(得分:2)

Ok @TrevorOrr回答Bootstrap标准字形,但请记住在<span>标记内包含<a>标记。例如:

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  <div class="navbar-collapse collapse">
    <ul class="nav navbar-nav navbar-left">
      <li><a href="#">Envelope icon: <span class="glyphicon glyphicon-envelope"></span></a></li> 
    </ul>
  </div>
</nav>    

如果您未在锚点中包含<span>,则不会显示字形。

答案 3 :(得分:0)

我遇到了类似的问题,但这里选择的解决方案无效。我必须将两个答案混合起来才能使其工作,即将背景属性应用于嵌套在<span class="icone icone-custom pull-right">中的<a>。没有:before