使用Bootstrap 4,如何在右侧对齐navbar-brand?

时间:2018-03-22 17:05:47

标签: css twitter-bootstrap bootstrap-4

我正在使用Bootstrap 4.我希望导航栏品牌项目(它只是一个文本元素)位于最右侧。菜单(正常)默认为左侧。

我已尝试应用ml-automx-automr-autopull-right等。我想要的不是什么。

mx-auto很适合小屏幕。当汉堡包菜单出现时,它将导航栏品牌置于中心位置。但是,当需要常规菜单时,我需要一些有用的东西。

这是我的代码:

<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top main-menu">
  <a href="#" class="navbar-brand navbar-right">BSB Feedback</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainNav" aria-controls="mainNav" aria-expanded="false"
    aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="navbar-collapse collapse" id="mainNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item homeLink">
        <a class="nav-link" href="/">
          <span aria-hidden="true" class=" fa fa-spacer-right fa-home"></span>
        </a>
      </li>
      <li class="nav-item active">
        <a class="nav-link" href="/">Give Feedback</a>
      </li>
      <li class="nav-item ">
        <a class="nav-link" href="/managefeedback/">Manage Feedback</a>
      </li>
    </ul>
  </div>
</nav>

3 个答案:

答案 0 :(得分:3)

您可以使用order-last课程。 然而,您可能希望品牌在移动设备屏幕上仍然是第一个/顶级产品,因此您可以响应地使用订单 ...

<强> navbar-brand order-md-last

https://www.codeply.com/go/Vq7ajCEfsg

<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top main-menu">
  <a href="#" class="navbar-brand order-md-last">BSB Feedback</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainNav" aria-controls="mainNav" aria-expanded="false"
    aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="navbar-collapse collapse" id="mainNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item homeLink">
        <a class="nav-link" href="/">
          <span aria-hidden="true" class=" fa fa-spacer-right fa-home"></span>
        </a>
      </li>
      <li class="nav-item active">
        <a class="nav-link" href="/">Give Feedback</a>
      </li>
      <li class="nav-item ">
        <a class="nav-link" href="/managefeedback/">Manage Feedback</a>
      </li>
    </ul>
  </div>
</nav>

More on Bootstrap ordering

另一种选择是在父导航栏上使用 flex-row-reverse responsively。这将切换品牌和导航链接的顺序,但仅限于非移动菜单。

<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top main-menu flex-md-row-reverse">
 ...
</nav>

而且,如果你想让品牌和交换器以移动设备为中心,你可以将它们包装在另一个div中并仍然以mx-auto为中心:https://www.codeply.com/go/xXBdCHGAAN

相关:
Bootstrap 4 align navbar items to the right

答案 1 :(得分:1)

使用.justify-content-md-end

<nav class="navbar navbar-expand navbar-dark bg-info">
 <div class="container justify-content-md-end">
  <a class="navbar-brand" href="#">Brand</a>
 </div>
</nav>

答案 2 :(得分:0)

  

如何将导航栏品牌放在右边?

向其中添加order-md-last课程(此外还有您尝试过的mx-auto)。

该排序类在中等(md)或更大的屏幕上重新排序元素。在较小的屏幕上,不会进行重新排序,并且会应用mx-auto类。

以下是应用该订单类的代码段:

&#13;
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top main-menu">
    <a href="#" class="navbar-brand mx-auto order-md-last">BSB Feedback</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainNav" aria-controls="mainNav" aria-expanded="false"
            aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="navbar-collapse collapse" id="mainNav">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item homeLink">
                <a class="nav-link" href="/">
                    <span aria-hidden="true" class=" fa fa-spacer-right fa-home"></span>
                </a>
            </li>
            <li class="nav-item active">
                <a class="nav-link" href="/">Give Feedback</a>
            </li>
            <li class="nav-item ">
                <a class="nav-link" href="/managefeedback/">Manage Feedback</a>
            </li>
        </ul>
    </div>
</nav>
&#13;
&#13;
&#13;