Bootstrap navbar align want align to the right

早过忘川 提交于 2019-12-04 16:08:46

问题


I'm trying to align the navbar to the right. When I use the .pull-right class it's pushing the navbar too much to the right: "off the grid".

What am I doing wrong?

Code:

<div class="row">
<div class="span3 top_logo"><img src="images/logo_svart.png" width="177" height="60" alt="BETA Team Performance" /></div>
    <div class="span9">
        <div class="navbar navbar-static-top">
            <div class="navbar-inner">
                <div class="container">
                    <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"><strong>MENY <i class="icon-chevron-down icon-white"></i></strong>
                    </button>
                    <div class="nav-collapse collapse">
                        <ul class="nav pull-right">
                            <li class="active"><a href="#">Start</a></li>
                            ...
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


回答1:


The problem arises because in bootstrap-responsive.css the margin-right is set to 0.

Jsfiddle with a basic mockup See in browser

So you have to provide it with some number to look appropriate, like below:

.navbar .nav.pull-right {
     float: right;
    margin-right: 98px; /*set margin this way in your custom styesheet*/
  }

The html code to create your desired effect can be written in the following way :

<div class="conatiner">
<div class="row-fluid">
    <div class="span2 top_logo">
        <img width="177" height="60" alt="BETA Team Performance" src="http://placehold.it/177x60">
    </div>
    <div class="span10">
        <div class="navbar navbar-static-top">
            <div class="navbar-inner">
                <div class="container">
                    <!-- .btn-navbar is used as the toggle for collapsed navbar content -->
                    <button data-target=".nav-collapse" data-toggle="collapse" class="btn btn-navbar"
                    type="button"><strong>MENY <i class="icon-chevron-down icon-white"></i></strong>

                    </button>
                    <!-- Everything you want hidden at 940px or less, place within here -->
                    <div class="nav-collapse collapse ">
                        <!-- .nav, .navbar-search, .navbar-form, etc -->
                        <ul class="nav pull-right">
                            <li class="active"><a href="#">Home</a>
                            </li>
                            <li><a href="#">Link</a>
                            </li>
                            <li><a href="#">Link</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
      </div>
    </div>
  </div>



回答2:


For anyone arriving here who's using Bootstrap v3, just use the included .navbar-right on the list element:

<div class="collapse navbar-collapse navbar-ex1-collapse">
    <ul class="nav navbar-nav navbar-right">
      <!-- … -->
    </ul>
</div>



回答3:


try putting your pull-right class on the div.collapse. That may do the trick.




回答4:


If you do want to pull something all the way to the right outside the grid (like select language or social buttons) you can create a simple class and add to the <ul class="nav navbar-nav language"> and style it like so .language {right:50px;position:absolute;}. Works really well with using the .navbar-brand as a logo to your far left (from an Art Directors eye, anyways).




回答5:


This here worked for me. Putting pull-right in the div.collapse doesn't work - it has to be in the un-ordered list class.

<div class="nav-collapse collapse navbar-responsive-collapse">

 <ul class="nav navbar-nav pull-right">



回答6:


In Boostrap 4 you can use ml-auto to align the menu to the right:

<div class="navbar-nav ml-auto">


来源:https://stackoverflow.com/questions/15118310/bootstrap-navbar-align-want-align-to-the-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!