如何将Bootstrap 4导航栏元素移动到右边?

时间:2017-10-16 09:43:55

标签: html css bootstrap-4

我想将li元素移到右边。



<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
  </div>
</nav>
&#13;
&#13;
&#13;

我已经尝试过这个css代码,但它打破了切换器和导航栏。

 @media (min-width: 768px) {
  .navbar-nav {
    float: left;
    margin: 0;
  }
  .navbar-nav > li {
    .pull-right();
    &:first-child {
      .pull-left();
    }
  }
  .navbar-nav > li > a {
    padding-top: 15px;
    padding-bottom: 15px;
  }
}

所以我想在不破坏切换器或导航栏的情况下移动li元素。 感谢。

2 个答案:

答案 0 :(得分:2)

添加justify-content-end并移除mr-auto

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
  </div>
</nav>

(我在没有你的CSS的情况下测试了这个解决方案)

答案 1 :(得分:0)

添加此课程

.navbar-nav .nav-link {
  padding-right: 0;
  padding-left: 0;
  text-align: right;
 }

并删除您的课程

 @media (min-width: 768px) {
 .navbar-nav {
  float: left;
  margin: 0;
  }
  .navbar-nav > li {
    .pull-right();
      &:first-child {
    .pull-left();
    }
   }
   .navbar-nav > li > a {
    padding-top: 15px;
    padding-bottom: 15px;
   }
  }