如何在导航栏中将社交媒体图标拉到右侧?

时间:2016-03-07 15:18:41

标签: css twitter-bootstrap navbar font-awesome

我正在使用bootstrap,下面是我的导航栏的代码。我添加了字体真棒图标,但我希望将其拉到右侧。我似乎无法将它拉到右边,而不会抛弃Home,About和Contact。有什么想法吗?

 <nav class="navbar navbar-inverse navbar-fixed-top">
   <div class="container">
     <div class="navbar-header">
       <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
         <span class="sr-only">Toggle navigation</span>
         <span class="icon-bar"></span>
         <span class="icon-bar"></span>
         <span class="icon-bar"></span>
       </button>
     <div id="navbar" class="navbar-collapse collapse">
       <ul class="nav navbar-nav navbar-left">
         <li class="active"><a href="#">Home</a></li>
         <li><a href="#about">About</a></li>
         <li><a href="#contact">Contact</a></li>
         <li><a href="#"><i class="fa fa-camera-retro fa-lg"></i>
         <li><a href="#"><i class="fa fa-twitter fa-lg"></i>
         <li><a href="#"><i class="fa fa-pinterest fa-lg"></i>
           </ul>
         </li>
       </ul>
     </div><!--/.nav-collapse -->
   </div>
 </nav>

2 个答案:

答案 0 :(得分:6)

Bootstrap 4

Bootstrap 4中的导航栏对齐更容易。您只需使用ml-auto上的navbar-nav和社交图标将其推向右侧......

<nav class="navbar navbar-dark bg-dark fixed-top navbar-expand-md">
    <div class="container">
        <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <span class="sr-only">Toggle navigation</span> ☰
        </button>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li class="active">
                    <a href="#" class="nav-link">Home</a>
                </li>
                <li>
                    <a href="#about" class="nav-link">About</a>
                </li>
                <li>
                    <a href="#contact" class="nav-link">Contact</a>
                </li>
            </ul>
            <ul class="nav navbar-nav ml-auto">
                    <a href="#" class="nav-link"><i class="fa fa-camera-retro fa-lg"></i></a>
                <li>
                    <a href="#" class="nav-link"><i class="fa fa-twitter fa-lg"></i></a>
                <li>
                    <a href="#" class="nav-link"><i class="fa fa-pinterest fa-lg"></i></a>
                </li>
            </ul>
        </div>
        <!--/.nav-collapse -->
    </div>
</nav>

Bootstrap 4 Navbar icons right demo

Bootstrap 3.x (原始答案)

标记有一些问题。从navbar-collapse中取出navbar-header。在<a href>内正确关闭<li>代码。使用navbar-right将图标拉到右侧。

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
        </div>
        <div id="navbar" 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>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#"><i class="fa fa-camera-retro fa-lg"></i></a></li>
                <li><a href="#"><i class="fa fa-twitter fa-lg"></i></a></li>
                <li><a href="#"><i class="fa fa-pinterest fa-lg"></i></a></li>
            </ul>
        </div>
    </div>
</nav>

工作演示:http://codeply.com/go/zb1PvhcJHs

答案 1 :(得分:1)

为什么不这样做呢?制作一个新的导航栏。

<nav class="navbar navbar-inverse navbar-fixed-top">
   <div class="container">
     <div class="navbar-header">
       <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
         <span class="sr-only">Toggle navigation</span>
         <span class="icon-bar"></span>
         <span class="icon-bar"></span>
         <span class="icon-bar"></span>
       </button>
     </div>
     <div id="navbar" class="navbar-collapse collapse">
       <ul class="nav navbar-nav navbar-left">
         <li class="active"><a href="#">Home</a></li>
         <li><a href="#about">About</a></li>
         <li><a href="#contact">Contact</a></li>    
       </ul>
       <ul class="nav navbar-nav navbar-right">
         <li><a href="#"><i class="fa fa-camera-retro fa-lg"></i></a></li>
         <li><a href="#"><i class="fa fa-twitter fa-lg"></i></a></li>
         <li><a href="#"><i class="fa fa-pinterest fa-lg"></i></a></li> 
       </ul>
     </div><!--/.nav-collapse -->
  </div>
</nav>

Demo