如何在Bootstrap 4中将导航栏链接居中

时间:2018-09-11 03:32:57

标签: css bootstrap-4 navbar

我有一个导航栏,我想将链接居中。我尝试添加mx-auto和text-center。什么都没用。我的代码在下面吗?

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark text-center">
  <div class="container text-center">
    <a class="navbar-brand text-center" href="#"></a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarResponsive">
      <ul class="navbar-nav ml-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">About</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

1 个答案:

答案 0 :(得分:3)

将mx-auto放在ul中,它将使链接居中

<nav class="navbar navbar-expand-lg navbar-dark bg-dark text-center">
  ...
      <ul class="navbar-nav ml-auto mx-auto">
        ...
      </ul>
    </div>
  </div>
</nav>

以下是您的示例中的codepen