0

I want to position the navbar-brand and nav-items on 2 different rows.

The navbar brand on the first row and the nav-items on the second row.

Current code:

<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="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNav">
    <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="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
  </div>
</nav>
WebDevBooster
  • 14,674
  • 9
  • 66
  • 70
demoncoder
  • 343
  • 3
  • 6
  • 19

1 Answers1

1

To achieve the desired effect, do this:

  1. Add the flex-column to the navbar like shown in the code snippet below. (This turns the navbar into a flex-column which is what enables the desired stacking behavior in this case.)

  2. If you don't want the items centered and want them left-aligned instead, add the align-items-start class to that as well.

  3. To align the navbar-brand with the items (in this case), add the ml-2 class (margin-left: 2 units) to the navbar-brand.

  4. To ensure that things behave "normally" (= in the usual way) when the navbar is collapsed, you need to modify 2 classes from above. So, if you have navbar-expand-lg as in the example below, you need to change flex-column to flex-lg-column to ensure that the switch to flex-column only happens when the screen is large (lg) or larger. And then change the ml-2 class to ml-lg-2 to ensure that the left margin on the navbar-brand only gets modified for those larger screens and is set to default for smaller screens.

Click "run code snippet" below and expand to full page:

<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 flex-lg-column align-items-start navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand ml-lg-2" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
        <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="#">Features</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Pricing</a>
            </li>
            <li class="nav-item">
                <a class="nav-link disabled" href="#">Disabled</a>
            </li>
        </ul>
    </div>
</nav>
WebDevBooster
  • 14,674
  • 9
  • 66
  • 70