Bootstrap 4 float-right无法使用导航栏

时间:2017-10-06 01:59:07

标签: css twitter-bootstrap twitter-bootstrap-4

这是代码。我需要将导航栏(没有徽标)移动到右侧。

所以我将float-right添加到<div class="collapse navbar-collapse float-right" id="navbarSupportedContent">

但它没有用。

完整代码

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
      <a class="navbar-brand" href="#">Popular Quotes Collection</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 float-right" 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="#">Submit a Quote</a>
          </li>
        </ul>
      </div>
    </nav>

3 个答案:

答案 0 :(得分:7)

只需添加:

  • .justify-content-end.navbar-collapse

  • 删除浮动,默认情况下,bootstrap 4是flexbox,因此您可以利用此功能。

像这样:

<div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">

<强> Working Resizable Fiddle

enter image description here

答案 1 :(得分:0)

你不能使用float因为bs4使用display:flex; 你可以使用class:&#39; justify-content-between&#39;或者在此页面上了解有关bootstrap4 flex class的更多信息https://getbootstrap.com/docs/4.0/utilities/flex/

答案 2 :(得分:0)

您根本不需要应用float-right,而是navbar-toggler-right

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <a class="navbar-brand" href="#">Popular Quotes Collection</a>
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon">Toggle</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="#">Submit a Quote</a>
      </li>
    </ul>
  </div>
</nav>

希望这有帮助! :)