CSS 将 Bootstrap 导航对齐到中心

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21980660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 01:57:29  来源:igfitidea点击:

Align Bootstrap Navigation to Center

csstwitter-bootstraptwitter-bootstrap-3

提问by trrrrrrm

I'm having a problem with bootstrap 3 navigation, i have some content on the left, center and right. it seems there is no option for centering the navigation.

我的 bootstrap 3 导航有问题,我在左边、中间和右边都有一些内容。似乎没有将导航居中的选项。

http://jsfiddle.net/29tQt/embedded/result/

http://jsfiddle.net/29tQt/embedded/result/

I'm trying to get the links titled "center" to be in the center of the page, how can i achieve that ?

我试图让标题为“中心”的链接位于页面的中心,我怎样才能做到这一点?

<nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <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="#">Brand</a>

        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Center</a>
                </li>
                <li><a href="#">Center</a>
                </li>
                <li><a href="#">Center</a>
                </li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Right</a>
                </li>
                <li><a href="#">Right</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

回答by trrrrrrm

Thank you all for your help, I added this code and it seems it fixed the issue:

谢谢大家的帮助,我添加了这段代码,似乎解决了这个问题:

.navbar .navbar-nav {
    display: inline-block;
    float: none;
}

.navbar .navbar-collapse {
    text-align: center;
}

Source

来源

Center content in responsive bootstrap navbar

响应式引导程序导航栏中的中心内容

回答by Mohit Pandey

Add 'justified' class to 'ul'.

将“justified”类添加到“ul”。

<ul class="nav navbar-nav justified">

CSS:

CSS:

.justified {
    position:absolute;
    left:50%;
}

Now, calculate its 'margin-left' in order to align it to center.

现在,计算其 'margin-left' 以将其与center.

// calculating margin-left to align it to center;
var width = $('.justified').width();
$('.justified').css('margin-left', '-' + (width / 2)+'px');

JSFiddle Code

JSF 中间代码

JSFiddle Embedded Link

JSFiddle 嵌入式链接

回答by Kawinesh SK

.navbar-nav {
   float: left;
   margin: 0;
   margin-left: 40%;
}

.navbar-nav.navbar-right:last-child {
   margin-right: -15px;
   margin-left: 0;
}

Updated Fiddle

更新的小提琴

Since You Have used the floatproperty we don't have many options except to adjust it manually.

由于您已经使用了该float属性,因此除了手动调整它之外,我们没有太多选择。

回答by Piyush Marvaniya

Try this css

试试这个css

.clearfix:before, .clearfix:after, .container:before, .container:after, .container-fluid:before, .container-fluid:after, .row:before, .row:after, .form-horizontal .form-group:before, .form-horizontal .form-group:after, .btn-toolbar:before, .btn-toolbar:after, .btn-group-vertical > .btn-group:before, .btn-group-vertical > .btn-group:after, .nav:before, .nav:after, .navbar:before, .navbar:after, .navbar-header:before, .navbar-header:after, .navbar-collapse:before, .navbar-collapse:after, .pager:before, .pager:after, .panel-body:before, .panel-body:after, .modal-footer:before, .modal-footer:after {
    content: " ";
    display: table-cell;
}

ul.nav {
    float: none;
    margin-bottom: 0;
    margin-left: auto;
    margin-right: auto;
    margin-top: 0;
    width: 240px;
}