-1

I would like the logo text to be on the left, the search bar in the middle and the user links on the right of the navigation bar.

How can I do that using bootstrap5?

<!-- NAVIGATION BAR - FIXED -->
  
  <nav class="navbar navbar-expand-sm navbar-dark bg-dark sticky-top">

    <div class="container-fluid">

<!-- Logo text. -->
            <a class="navbar-brand" href="javascript:void(0)"><h1>Logo text.</h1></a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#mynavbar">
              <span class="navbar-toggler-icon"></span>
            </button>
          
<!-- Search form -->
        <div class="collapse navbar-collapse" id="mynavbar">
            <form class="d-flex">
              <input class="form-control me-2" type="text" placeholder="Search">
              <button class="btn btn-primary" type="button">Search</button>
            </form>

<!-- User links -->
            <ul class="navbar-nav me-auto">
              <li class="nav-item">
                <a class="nav-link" href="javascript:void(0)">Link 1</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="javascript:void(0)">Link 2</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="javascript:void(0)">Link 3</a>
              </li>
            </ul>
        </div>
    </div>
  </nav>
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
daniellambert
  • 109
  • 1
  • 8

1 Answers1

1

Just add:

<nav class="navbar navbar-expand-sm navbar-dark bg-dark sticky-top">
  <div class="container-fluid">    
    <div class="d-flex justify-content-between">
      <!-- Your elements -->
    </div>
  </div>
</nav>

It must work. But if its not, here the full documentation about it: https://getbootstrap.com/docs/5.0/utilities/flex/

Edit:

Here I paste an example. It works for me. What I've done is add some css in order to overwrite some bootstrap default attributes. Indeed, navbar-collapse was doing something, but mainly is DOM structure and boostrap classes.

.navbar-collapse {
  flex: 0 0 auto;
  flex-grow: 0 !important;
}

@media only screen and (max-width: 768px) {
    .navbar-collapse {
      position: absolute;
      width: 100%;
      top: 66px;
      left: 0;
      background: black;
      padding: 15px;
    }
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
<!-- NAVIGATION BAR - FIXED -->
  
  <nav class="navbar navbar-expand-sm navbar-dark bg-dark sticky-top">
    <div class="container-fluid">
      <div class="w-100 d-flex justify-content-between">
        <!-- Logo text. -->
        <a class="navbar-brand" href="javascript:void(0)">
          <h1 class="d-inline-block">Logo text.</h1>
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#mynavbar">
          <span class="navbar-toggler-icon"></span>
        </button>
        <!-- Search form -->
        <div class="collapse navbar-collapse" id="mynavbar">
          <form class="d-flex">
            <input class="form-control me-2" type="text" placeholder="Search">
            <button class="btn btn-primary" type="button">Search</button>
          </form>
          <!-- User links -->
          <ul class="navbar-nav me-auto">
            <li class="nav-item">
              <a class="nav-link" href="javascript:void(0)">Link 1</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="javascript:void(0)">Link 2</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="javascript:void(0)">Link 3</a>
            </li>
        </ul>
      </div>
    </div>
  </div>

Note that I've added a @mediaquery to adjust the behaviour of the menu and it works on mobile and on tablet/desktop.

Toni Bordoy
  • 255
  • 2
  • 7
  • I've tried but it's not working properly. I think part of the issue may be that there is a div.collapse navbar-collapse. – daniellambert Mar 24 '22 at 07:23
  • I've edit my answer. I've test it and let you the code that works for me. – Toni Bordoy Mar 24 '22 at 15:20
  • I still couldn't get it to work with yours but thank you for trying. It turned out to be much simpler than I thought but still took a while to find. I just used ms-auto and me-auto. Bootstrap5 for margin-left and margin-right. – daniellambert Mar 24 '22 at 22:23
  • Sorry, it seems that i must to include the bootstrap library. Now I have edited the answer, and add bootstrap library and include an `!important` in the `flex-grow` attribute in order to force the desired effect. Anyway, if you find a way, glad to help you. – Toni Bordoy Mar 24 '22 at 22:46