在引导程序中居中导航按钮

时间:2015-11-12 03:21:36

标签: html css twitter-bootstrap

我试图抓住一些网页设计,但我无法弄清楚如何让项目中的导航按钮居中。我希望从Home到Page 3的按钮居中,并且想知道是否有一种简单的方法可以单独在HTML文件中执行此操作而无需单独的CSS类。 enter image description here

    <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">Website Name</a>
    </div>
    <div>
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">Page 1</a></li>
        <li><a href="#">Page 2</a></li>
        <li><a href="#">Page 3</a></li>
      </ul>
    </div>
  </div>
</nav>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

3 个答案:

答案 0 :(得分:1)

在Bootstrap.CSS中进行一些更改 可能是这样......

.nav navbar-nav
{
    position: absolute;
    width: 100%;
    left: 0;
    text-align: center;
    margin: auto;
}

点击这里=&gt; bootply

答案 1 :(得分:0)

使用此css将导航按钮置于中心位置。

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

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

DEMO

答案 2 :(得分:0)

您可以通过调整一些CSS属性来实现此目的。基本上从navbar-nav中移除浮动并使文本居中。但是如果你真的希望它们居中,你必须通过为它分配一个宽度并将一个负边距加到navbar-brand的宽度的一半来考虑navbar-nav类。

参见工作示例片段。

@media (min-width: 768px) {
  .navbar.navbar-default {
    height: 50px;
  }
  .navbar.navbar-default .container-fluid,
  .navbar.navbar-default .container {
    text-align: center;
  }
  .navbar.navbar-default .navbar-nav {
    display: inline-block;
    float: none;
    margin-left: -75px;
  }
  .navbar.navbar-default .navbar-brand {
    width: 150px;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-nav" aria-expanded="false"> <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="#">Website Name</a>

    </div>
    <div class="collapse navbar-collapse" id="bs-nav">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a>

        </li>
        <li><a href="#">Page 1</a>

        </li>
        <li><a href="#">Page 2</a>

        </li>
        <li><a href="#">Page 3</a>

        </li>
      </ul>
    </div>
  </div>
</nav>

移动中心

.navbar.navbar-default .container-fluid,
.navbar.navbar-default .container {
  text-align: center;
}
@media (min-width: 768px) {
  .navbar.navbar-default {
    height: 50px;
  }
  .navbar.navbar-default .navbar-nav {
    display: inline-block;
    float: none;
    margin-left: -75px;
  }
  .navbar.navbar-default .navbar-brand {
    width: 150px;
  }
}
@media (max-width: 767px) {
  .navbar-default .navbar-brand.navbar-brand-centered {
    position: absolute;
    left: 50%;
    display: block;
    width: 150px;
    text-align: center;
    top: 0;
  }
  .navbar.navbar-default > .container .navbar-brand.navbar-brand-centered,
  .navbar.navbar-default > .container-fluid .navbar-brand.navbar-brand-centered {
    margin-left: -75px;
    margin-top: 0;
    margin-bottom: 0;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-nav" aria-expanded="false"> <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 navbar-brand-centered" href="#">Website Name</a>

    </div>
    <div class="collapse navbar-collapse" id="bs-nav">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a>

        </li>
        <li><a href="#">Page 1</a>

        </li>
        <li><a href="#">Page 2</a>

        </li>
        <li><a href="#">Page 3</a>

        </li>
      </ul>
    </div>
  </div>
</nav>