0

The following might look like a duplicate but isn't. My question concerns the current Bootstrap version 4.3, and no previous version. Existing answers to this question either

  • don't work for this version (not useful for up-to-date projects) or
  • are based on already heavily customized Bootstrap (not reproducible).

I have the ungrateful task of customizing Bootstrap, so here's my question: How do I center the navbar-brand link within a Bootstrap 4 (!) navbar?

Here's a simple navbar from the Bootstrap 4.3 docs, which is the latest version at the time of this writing:

<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="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <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>
  </div>
</nav>

Some of the answers I found while googling suggest to add the CSS class mx-auto to the navbar-brand link. This has no effect, though.

Since I'm already here: Does anyone know how to always show the burger menu, i.e., never have the menu expanded? (This is just a side question, I will accept an answer that does not comment on this, too.)

typeduke
  • 6,494
  • 6
  • 25
  • 34

2 Answers2

1

How do I center the navbar-brand link within a Bootstrap 4 (!) navbar?

I'd suggest to position it absolutely.

For example like this:

.navbar {
  position: relative;
}

a.navbar-brand{
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translate(-50%);
}

Demo: https://jsfiddle.net/ytjhew9u/4/


How to always show the burger menu?

Regarding your side question; you need to remove the class navbar-expand-lg from the navbar element.

Demo: https://jsfiddle.net/ytjhew9u/2/

tmuecksch
  • 6,222
  • 6
  • 40
  • 61
1

To be able to make the the hamburger menu show at all times, try this while centering the nav-brand.

comment the .navbar-toggler to return the hamburger to the left.

The html:

<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.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>

  <nav class="navbar fixed-top navbar-dark bg-dark">
   <a class="navbar-brand" href="#">Navbar</a>
   <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsingNavbar">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="navbar-collapse collapse" id="collapsingNavbar">
        <ul class="navbar-nav">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">Home</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#features">Features</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#myAlert" data-toggle="collapse">Wow</a>
            </li>
        </ul>
        <ul class="navbar-nav ml-auto">
            <li class="nav-item">
                <a class="nav-link" href="" data-target="#myModal" data-toggle="modal">About</a>
            </li>
        </ul>
    </div>
</nav>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <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.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

css:

.navbar {
    position: relative;
    min-height: 56px;
}
.navbar-brand {
    position: absolute;
    left: 50%;
    margin-left: -50px !important;  /* 50% of your logo width */
    display: block;
}

.navbar-toggler{
  position: absolute;
  right: 20px;
}
Emmanuel Neni
  • 325
  • 4
  • 11