How to implement RTL bootstrap 4 navbar?

10,414

Solution 1

You just need to add a direction to your NAV:

nav {direction: rtl;}

This is an working example based on your CODE https://jsfiddle.net/Mohcinbn/7b6wa8rL/

Solution 2

One approach is using class justify-content-end (Read more here about justify-content-) on the collapsing navbar and play with the order- classes for keeps things nicely ordered when the navbar is collapsed / not collapsed. Check next example:

<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>

UPDATE: Show another alternative with a navbar that never collapses to a button. In this case, you need to use class navbar-expand on the <nav> element in replace of class navbar-expand-sm and avoid to change order of the elements depending on the screen size. Check next example:

<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>

Solution 3

If you just want to invert, so just change the order of the navbar-brand element

In the first example, in <a class="nav-brand"> is before <button> and <ul> with links, so the brand is on the left.

In second example, the <a class="nav-brand"> is after <button> and <ul> with links, so the brand now is on the right.

Examples :

<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>

Here you have more details : https://getbootstrap.com/docs/4.1/components/navbar/#toggler

Share:
10,414
Babr
Author by

Babr

Updated on June 13, 2022

Comments

  • Babr
    Babr almost 2 years

    I'd like to make my bootstrap navbar RTL. That is the logo in the right and the links flow from right to left. I have tried different tricks but none of they works. Here is the code I have right now:

    <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>
    

    How can I fix this code to implement RTL navbar?

  • Babr
    Babr over 5 years
    How to avoid the links being collapsed to button?
  • Shidersz
    Shidersz over 5 years
    If you want the navbar to never collapse, replace class navbar-expand-sm by navbar-expand, but you will have to modify the order classes also. I will update with another sample for this one...
  • Shidersz
    Shidersz over 5 years
    This works nice, but I suggest to made a custom class so you don't affect all <nav> elements, for example: nav.nav-rtl {direction: rtl;} and then apply the class nav-rtl to the <nav> element you want to customize. Anyway, +1!
  • MohcinBN
    MohcinBN over 5 years
    Yeah i understand you, you have a view in the point.. good thinking.. thank you :)
  • kneidels
    kneidels over 4 years
    I have tried their code. First of all, its a little outdated (latest is 4.2.1) and second of all, it does not solve the above issue - when changing the order of the tags is necessary in addition to replacing the CS file.