skip to Main Content

I am trying to use flexbox to customize a bootstrap 4 navbar. I want the nav-items to fill the full height of the navbar div while keeping their contents centered vertically and the spacing between the 3 horizontally links consistent (using justify-content-around). I have not been successful using the code below. Is it possible?

Any help would be appreciated.

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />

<nav id="mainNav" class="navbar navbar-expand-md navbar-light bg-primary pt-0 pb-0">
  <a class="navbar-brand d-none d-sm-block mr-auto my-2" href="#"><img class="img-fluid" src="https://via.placeholder.com/139x60"></a>

  <div class="collapse navbar-collapse mx-5" id="navbarCollapse">

    <!-- Stetch these virticall -->
    <ul class="navbar-nav nav-fill w-100 align-items-start">
      <li class="nav-item active">
        <a class="nav-link bg-danger" href="#">Shop Now<span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Why Buy From Us</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Contact Us</a>
      </li>

    </ul>
  </div>
  <form class="form-inline mt-2 mt-md-0 mr-auto mr-sm-0 mx-md-auto">
    <div class="input-group">
      <input class="form-control form-control-sm" type="text" placeholder="Search" aria-label="Search">
      <div class="input-group-append">
        <span class="input-group-text"><i class="far fa-search"></i></span>
      </div>
    </div>
  </form>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
</nav>

2

Answers


  1. Chosen as BEST ANSWER

    It appears that in order to get bootstrap navbar links to fill the full height of the navbar and center the text vertically, I needed to add a targetable container inside the .nav-link class to apply align-self: center to.

    I used a span tag and decided to use CSS rather than inline classes to keep the code cleaner and in my case limited it to only the #mainNav ID.

    Wondering if there is an even cleaner way to do this without the extra span tag.

    #mainNav,
    .collapse,
    .nav-link {
      display: flex;
      align-items: stretch;
    }
    
    #mainNav .navbar-nav {
      display: flex;
      justify-content: space-around;
      width: 100%;
    }
    
    #mainNav .nav-item {
      display: flex;
    }
    
    #mainNav .nav-link span {
      align-self: center;
    }
    <html lang="en">
    
    <head>
      <!-- Required meta tags -->
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <!-- Bootstrap CSS -->
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
      <title>full height nav</title>
    
    </head>
    
    <body>
      <div class="container">
        <nav id="topNav" class="navbar navbar-expand navbar-dark text-white bg-primary justify-content-sm-end">
          <ul class="navbar-nav d-flex align-items-end">
            <li class="nav-item d-sm-none">
              <a class="nav-link" href="#">Company Name</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Support</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Product Registration</a>
            </li>
            <li class="nav-item d-none d-sm-block">
              <a class="nav-link" href="#">Contact Us</a>
            </li>
          </ul>
        </nav>
        <nav id="mainNav" class="navbar navbar-expand-md navbar-light pt-0 pb-0">
          <!-- Logo -->
          <a class="navbar-brand d-none d-sm-block my-2 order-md-first" href="#">
            <img class="img-fluid" src="https://via.placeholder.com/139x60.png?text=LOGO">
          </a>
          <!-- Search Bar -->
          <form class="form-inline order-md-last m-0">
            <div class="input-group">
              <input class="form-control form-control-sm" type="text" placeholder="Search" aria-label="Search">
              <div class="input-group-append">
                <span class="input-group-text"><i class="fas fa-search"></i></span>
              </div>
            </div>
          </form>
          <!-- Toggler -->
          <button class="navbar-toggler align-self-center" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
          <!-- Navigation -->
          <div class="collapse navbar-collapse mx-5" id="navbarCollapse">
            <ul class="navbar-nav">
              <li class="nav-item active">
                <a class="nav-link" href="#">
                  <span>Shop Now</span><span class="sr-only">(current)</span>
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">
                  <span>Why Company</span></a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">
                  <span>Buying Guide</span></a>
              </li>
            </ul>
          </div>
        </nav>
      </div>
      <!-- Optional JavaScript -->
      <!-- jQuery first, then Popper.js, then Bootstrap JS -->
      <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    </body>
    
    </html>


  2. Try add class “my-0 p-0” in your brand image

    Then, add approximately “height: 3.7em” to your links and add the “py-3” class to them

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search