html - 如何使 Bootstrap 导航栏居中?

标签 html css twitter-bootstrap

我见过几个尝试使 Bootstrap 导航栏居中的示例,但我无法使它们中的任何一个工作。我已经在 navdivul 上尝试了 style="text-align:center" 但它没有任何影响。

知道我做错了什么吗?

<!DOCTYPE html>
<html>

<head>
  <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

  <!-- Optional theme -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

  <!-- Latest compiled and minified JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>

<body>
  <div class="container">


    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <ul class="nav nav-pills">
          <li><a role="presentation" href="#">Link1</a></li>
          <li><a role="presentation" href="#">Link2</a></li>
          <li><a role="presentation" href="#">Link3</a></li>
          <li><a role="presentation" href="#">Link4</a></li>
        </ul>
      </div>
    </nav>

  </div>
</body>

</html>

https://plnkr.co/edit/EDf9leSPZVoS4l9dQCfv?p=preview

最佳答案

问题是你的“nav nav-pills li”在你的 css 中有一个默认的“float:left”。这使得你的文本对齐永远不会起作用,因为 float 总是会覆盖任何其他文本属性。

解决此问题的一种方法是复制您的 css 文件中的下一个声明:

.nav {
   padding-left: 0;
   margin-bottom: 0;
   list-style: none;
   text-align: center;
}
.nav-pills>li {
   float: none !important;
   display: inline-block;
}

希望这能行得通

关于html - 如何使 Bootstrap 导航栏居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36104886/

相关文章:

javascript - 在另一个内容上设置 Div 位置

javascript - MegaMenu 在打开另一个菜单项时不会关闭子菜单

html - div/text 中的表格忽略右填充的 CSS 问题

html - 3 级 CSS3-only 菜单不打开第 3 级

html - 当我在 chrome 开发工具中点击切换设备时,我不能让容器响应

html - CSS - 带边框的 UL 内联 LI 改变 block (v) 内联?

html - Bootstrap CSS : div with image is overlapping div with text

css - 棘手的背景颜色 : Possible with CSS?

javascript - bootstrap selectjs 不适用于动态附加值

html - 具有可变高度的 Bootstrap div