将导航中的Font Awesome图标垂直居中

时间:2016-08-05 21:36:23

标签: html css twitter-bootstrap font-awesome centering

我试图让图标垂直居中,以便与导航链接的其余部分保持一致,但无论我尝试什么,我似乎无法让它发挥作用。

HTML

<nav class="navbar navbar-inverse">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <img src="img/logo.png" class="navbar-brand" href="index.html">
        </div>
        <div class="collapse navbar-collapse" id="nav">
          <ul class="nav navbar-nav navbar-left">
            <li><span class="fa fa-phone"> +62 202 555 0117</span></li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Work</a></li>
            <li><a href="#">Blog</a></li>
            <li><a href="#">Contact</a></li>
          </ul>
        </div>
      </div>
    </nav>

CSS

nav.navbar {
padding: 12px;
}
span.fa.fa-phone {
    color: #fff;
    vertical-align: middle;
}

1 个答案:

答案 0 :(得分:2)

  1. Navbar的链接具有padding: 15px属性。因此,您需要将此属性应用于电话号码。

  2. 为此目的制作特殊课程。使用

    <li class="phone"><span class="fa fa-phone"></span> +62 202 555 0117</li>

    而不是<li><span class="fa fa-phone"> +62 202 555 0117</span></li>

  3. 使用<a class="navbar-brand" href="index.html"><img src="img/logo.png" alt=""></a>
    而不是<img src="img/logo.png" class="navbar-brand" href="index.html">

  4. 切换按钮必须使用id中的<div class="collapse navbar-collapse" id="nav">作为data-target属性的值。

  5. 请查看结果。这是你想要实现的目标吗?

    &#13;
    &#13;
    @import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
    @import url('https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css');
    
    .navbar {
      padding: 12px;
    }
    .nav .phone {
      color: #fff;
      padding: 15px;
    }
    &#13;
    <nav class="navbar navbar-inverse">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#nav" aria-expanded="false">
            <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="index.html"><img src="img/logo.png" alt=""></a>
        </div>
        <div class="collapse navbar-collapse" id="nav">
          <ul class="nav navbar-nav navbar-left">
            <li class="phone"><span class="fa fa-phone"></span> +62 202 555 0117</li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Work</a></li>
            <li><a href="#">Blog</a></li>
            <li><a href="#">Contact</a></li>
          </ul>
        </div>
      </div>
    </nav>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    &#13;
    &#13;
    &#13;