如何将我的bootstrap导航栏置于CSS中心?

时间:2016-12-08 05:37:37

标签: html css twitter-bootstrap

我试图让导航栏中的所有链接都居中,如果我有更多链接,它们也会自动居中。

HTML:

 <nav class="navbar navbar-default navbar-fixed-top">
    <div class="container-fluid">
        <ul class="nav navbar-nav">
            <li class="active"><a href="#">HOME</a></li>
            <li><a href="#">ABOUT</a></li>
            <li><a href="#">SHOP</a></li>
        </ul>
    </div>
</nav>

5 个答案:

答案 0 :(得分:0)

试试这个:

制作ul text-align:center;&amp;将display: inline-block;添加到li a

答案 1 :(得分:0)

这可以帮助您了解更多.. http://www.bootply.com/CSI2KcCoEM

.navbar-brand
{
    position: absolute;
    width: 100%;
    left: 0;
    text-align: center;
    margin:0 auto;
}
.navbar-toggle {
    z-index:3;
}
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<nav class="navbar navbar-default" role="navigation">
  <div class="navbar-header">
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </button>    
  </div>
  <div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
      <li class="navbar-left"><a href="#">Left 1</a></li>
      <li class="navbar-left"><a href="#">Left 2</a></li>
      <li class="active"><a href="#">Center 1</a></li>
      <li><a href="#">Center 2</a></li>
      <li><a href="#">Center 3</a></li>
      <li class="navbar-right"><a href="#">Right 2</a></li>
      <li class="navbar-right"><a href="#">Right 1</a></li>
    </ul>
  </div>
</nav>

答案 2 :(得分:0)

你想让导航栏看起来像这样吗?

<div class="container">
        <header>
          <nav class="navbar navbar-inverse">

                <ul class="nav navbar-nav">
                    <li class="active"><a href="#">HOME</a></li>
                    <li><a href="#">ABOUT</a></li>
                    <li><a href="#">SHOP</a></li>
                </ul>
              </nav>
          </header>
      </div>

答案 3 :(得分:0)

您可以将元素设置居中,将uldisplay:inline-block;以及margin:0 auto;的属性text-align:center;添加到其父元素

.navbar-nav{
  float:none !important;
  margin:0 auto;
  display:inline-block;
}

 <nav class="navbar navbar-default navbar-fixed-top">
    <div class="container-fluid" style="text-align:center"> <!--- added text-align center --->
       <ul class="nav navbar-nav">
          <li class="active"><a href="#">HOME</a></li>
          <li><a href="#">ABOUT</a></li>
          <li><a href="#">SHOP</a></li>
       </ul>
    </div>
</nav>

DEMO

答案 4 :(得分:0)

您可以使用:Class="Center-block"

<div class="container-fluid" class="Center-block">
    <ul class="nav navbar-nav">
        <li class="active"><a href="#">HOME</a></li>
        <li><a href="#">ABOUT</a></li>
        <li><a href="#">SHOP</a></li>
    </ul>
</div>