One question, do you want it to be centered in the page or centered between the elements.
If you want it to be centered between the elements. You can wrap your button and login dropdown in a div (or another element), so that you have 3 children in your nav element.
Then you can add the classes d-flex justify-content-between
to the nav.
Ohh.. also remove the navbar-collapse
from the navbarNavDropdown
, and replace it with d-flex
(d-block works too). It has flex-grow: 1
which makes it hard to for the other element to center because it takes up the space.
For your third question it can also be fixed by wrapping your navbar-toggler
button and the dropdown menu div, in a div with a position-relative
.
Because the dropdown menu is positioned with absolute you can create another context for it with the position relative. So that it will position it self relative to the parent.
<nav class="navbar navbar-expand-sm navbar-dark bg-dark fixed-top d-flex ">
<div class="collapse d-block" id="navbarNavDropdown">
<ul class="navbar-nav me-auto">
<li class="nav-item">
<a class="nav-link" href="#">Menu One</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink2" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Menu Two</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="navbarDropdownMenuLink2">
<li><a class="dropdown-item" href="#">Sub 1</a></li>
<li><a class="dropdown-item" href="#">Sub 2</a></li>
</ul>
</li>
</ul>
</div>
<a class="navbar-brand mx-auto d-block" href="#">Center This Brand</a>
<div class="position-relative">
<button class="navbar-toggler" data-bs-toggle="collapse" data-bs-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="d-flex order-sm-1 ms-auto pe-2">
<ul class="navbar-nav flex-row">
<li class="nav-item mx-2">
<a href="#" data-bs-toggle="dropdown" class="nav-link dropdown-toggle" id="iconToggle">
<i class="fa fa-user fa-lg" ></i>
</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="navbarDropdownMenuLink2">
<li><a class="dropdown-item ms-auto" href="#">Login</a></li>
<li><a class="dropdown-item" href="#">Register</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>