如何实现RTL bootstrap 4 navbar?

时间:2018-12-26 18:54:25

标签: css twitter-bootstrap bootstrap-4 navbar

我想使我的引导导航栏成为RTL。那是右边的徽标,链接从右到左流动。我尝试了不同的技巧,但没有一个起作用。这是我现在拥有的代码:

<nav class="navbar fixed-top navbar-expand-sm navbar-dark bg-dark float-right">

    <!-- Brand -->
    <a class="navbar-brand" href="#">Logo</a>

    <!-- Links -->
    <div class="collapse navbar-collapse" id="nav-content">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link" href="#">Link 1</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link 2</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link 3</a>
        </li>
      </ul>
    </div>

</nav>

如何修复此代码以实现RTL导航栏?

5 个答案:

答案 0 :(得分:3)

您只需要向NAV添加方向:

nav {direction: rtl;}

这是一个基于您的CODE的有效示例 https://jsfiddle.net/Mohcinbn/7b6wa8rL/

答案 1 :(得分:1)

一种方法是在折叠导航栏上使用类justify-content-end(此处有关justify-content- 的更多信息),并使用order- 导航栏折叠/未折叠时,用于的类可使事情井井有条。检查下一个示例:

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<nav class="navbar fixed-top navbar-expand-sm navbar-dark bg-dark">

<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".collapse">
    <span class="navbar-toggler-icon"></span>
</button>

<a href="#" class="navbar-brand order-sm-2 order-1">Logo</a>

<div class="navbar-collapse collapse order-2 order-sm-1 justify-content-end">
    <ul class="navbar-nav">
        <li class="nav-item">
             <a class="nav-link" href="#">Link 1</a>
        </li>
        <li class="nav-item">
             <a class="nav-link" href="#">Link 2</a>
        </li>
        <li class="nav-item">
             <a class="nav-link" href="#">Link 3</a>
        </li>
    </ul>
</div>

</nav>


更新:显示导航栏,该导航栏永远不会折叠到按钮上。在这种情况下,您需要在navbar-expand元素上使用类<nav>来代替类navbar-expand-sm,并避免根据屏幕尺寸更改元素的顺序。检查下一个示例:

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<nav class="navbar fixed-top navbar-expand navbar-dark bg-dark">

<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".collapse">
    <span class="navbar-toggler-icon"></span>
</button>

<a href="#" class="navbar-brand order-2">Logo</a>

<div class="navbar-collapse collapse order-1 justify-content-end">
    <ul class="navbar-nav">
        <li class="nav-item">
             <a class="nav-link" href="#">Link 1</a>
        </li>
        <li class="nav-item">
             <a class="nav-link" href="#">Link 2</a>
        </li>
        <li class="nav-item">
             <a class="nav-link" href="#">Link 3</a>
        </li>
    </ul>
</div>

</nav>

答案 2 :(得分:0)

如果您只想求反,则只需更改navbar-brand元素的顺序

在第一个示例中,<a class="nav-brand">中的<button><ul>中带有链接,因此品牌在左侧。

在第二个示例中,<a class="nav-brand"><button><ul>之后,并带有链接,因此品牌现在在右边。

示例:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<!-- Logo on the left -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar on left</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
      <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>
  </nav>
  
<!-- Logo on the right -->

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

  <div class="collapse navbar-collapse" id="navbarTogglerDemo03">
    <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
      <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>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
    <a class="navbar-brand" href="#">Navbar on right</a>

</nav>

这里有更多详细信息:https://getbootstrap.com/docs/4.1/components/navbar/#toggler

答案 3 :(得分:0)

基于@Guilherme的回答,它对我有用。他的回答对我不起作用,因为在打开切换按钮时,“ navbar-toggler”按钮仍在左侧,而“ Brand”名称则在下方。所以这个答案也将解决这些问题。

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar on right</a>
  <div class="collapse navbar-collapse text-right" id="navbarTogglerDemo03">
    <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
      <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>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>

</nav>

答案 4 :(得分:-1)

使用样式属性不足以用于RTL语言,而且如果您对CSS代码不熟悉,那么自行更改引导程序将是一项艰巨的任务!

我建议您使用从引导程序派生的从右到左框架,例如:

RTLCSS引导程序(WebSite-Github