Bootstrap 3 网格内的内联导航栏

Bootstrap 3 inline navbar inside grid

我制作了简单的 bootstrap 导航栏,但是如何让它适应网格系统,我想成为 col-md-8 或其他因为我是这个网格系统的新手,我知道如何做简单的东西但不是这个。

<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <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="#">Flatie</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">    
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Link</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Action</a></li>
                        <li><a href="#">Another action</a></li>
                        <li><a href="#">Something else here</a></li>
                        <li class="divider"></li>
                        <li><a href="#">Separated link</a></li>
                    </ul>
                </li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>

这里是JsFiddle

将其包装在 div 行 class 中,或者更好地将所有代码包装在 div 行 class 容器中。如下所示:

<div class="container">
     <div class="row">
      <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
       <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
         <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="#">Flatie</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">    
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Link</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Action</a></li>
                        <li><a href="#">Another action</a></li>
                        <li><a href="#">Something else here</a></li>
                        <li class="divider"></li>
                        <li><a href="#">Separated link</a></li>
                    </ul>
                </li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
  </nav>

</div><!--end of row-->
<div class="row">add other contain</div>

</div><!--end of container-->

好的,我做到了,我把容器包成一排,我给容器一个 class col-md-8 并居中。 在 css 中,id 以居中的方式执行此操作:

.centered {
    margin: 0 auto;
    float: none
}

margin 0 自动居中 float none 是因为 col-md-8 有 float left.

默认情况下,导航元素为全宽,但如果您想将其包含在网格系统中,则根据网格系统将导航元素包含在具有 类 的元素中,即 col-md-8、col-xs -6 等等

例如

  '<div class="col-md-8"> <nav class="navbar navbar-default navbar-fixed-top" role="navigation">'...

   ... Your nav menu Code

  '</nav></div><div class="col-md-4"></div>'