0

I am trying to make a Navbar in Bootstrap 5. I would like to have the Logo (In my case this is just some fancy text) on the left. I would then like the menu centered and my search field on the right. The issue I seem to be having is when I have managed to get them lined up like this, the menu is going in the center of the search and logo divs, which due to logo size etc hasn't necessarily been the middle of my screen like I want. I have been at it for days trying different things on her but just can't seem to get it right. I also want the mobile menu to collapse and include the search. The mobile menu should then have the logo aligned in the center and the collapse button just below it also centered.

The latest code I have tried is this:

index.html:

{% load static %}

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{% block title %}{% endblock %}Life With Vicky</title>
    <meta name="description" content="{% block meta_description %}{%endblock %}">
    {% block canonical %}{%endblock %}
    {% block opengraph %}{%endblock %}
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
    <link rel="stylesheet" type='text/css' href="{% static 'css/main.css' %}">
</head>

<body>
    <nav class="navbar navbar-dark navbar-expand-md py-3 w-100" style="background-color:#FCC1C5">
        <div class="container-fluid">
            <a class="navbar-brand d-flex align-items-center" href="{% url 'home' %}">
                <span>
                    Life With Vicky
                </span>
            </a>
            <button class="navbar-toggler ml-auto" data-bs-toggle="collapse" data-bs-target="#navcol-6"><span
                    class="visually-hidden">Toggle navigation</span><span class="navbar-toggler-icon"></span></button>
            <div id="navcol-6" class="collapse navbar-collapse flex-grow-0 order-md-first">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item"><a class="nav-link {% if active_tab == 'home' %}active{% endif %}"
                            href="{% url 'home' %}">Home</a></li>
                    <li class="nav-item"><a class="nav-link {% if active_tab == 'blog' %}active{% endif %}"
                            href="{% url 'blog' %}">Blog</a></li>
                    <li class="nav-item"><a class="nav-link {% if active_tab == 'about' %}active{% endif %}"
                            href="{% url 'about' %}">About</a></li>
                    <li class="nav-item"><a class="nav-link {% if active_tab == 'recommendations' %}active{% endif %}"
                            href="{% url 'recommendations' %}">Recommendations</a></li>
                    <li class="nav-item"><a class="nav-link {% if active_tab == 'contact' %}active{% endif %}"
                            href="{% url 'contact' %}">Contact</a></li>
                </ul>
                <form class="d-flex" role="search">
                    <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
                    <button class="btn btn-outline-light" type="submit">Search</button>
                </form>
            </div>
        </div>
    </nav>
    {% block content %}
    {% endblock %}
    <footer class="py-3 bg-grey">
        <p class="m-0 text-dark text-center ">Copyright &copy; Life With Vicky</p>
    </footer>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2"
        crossorigin="anonymous"></script>
</body>

</html>

The templating stuff is just because I am doing this with Django.

main.css:

@font-face {
    font-family: 'vi-my-ha-hoa';
    src: url('../fonts/vi-my-ha-hoa.ttf') format("truetype");
}

.navbar-brand {
    font-family: 'vi-my-ha-hoa';
    font-size: 3rem;
    word-spacing: 0.4rem;
}

.carousel-image {
    height: 400px;
    object-fit: cover !important;
}

.carousel-item>div {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.navbar-dark {
    --bs-navbar-color: #ffffff;
    --bs-navbar-hover-color: #ffffff;
    --bs-navbar-active-color: #fff;
}
isherwood
  • 58,414
  • 16
  • 114
  • 157
Adam Birds
  • 394
  • 3
  • 21

1 Answers1

-1

I haven't found a satisfactory easy way to have a truly centered navbar. I wish bootstrap provided a default way to do so. Below is an approach that works by defining the left and right aligned elements with the "col" class (flex-basis grow) and leaving the center alone, based on the answer given at Keep the middle item centered when side items have different widths. However, the navbar brand collapses with everything else. (see codepen implementation: https://codepen.io/sam-miller-the-flexboxer/pen/bGLmXzz). For other, perhaps more flexible, approaches including using absolute positioning the center, see https://codepen.io/yesiamrocks/pen/MVOWXZ . The examples use Bootstrap 4, so you may have to update some of the class names to Bootstrap 5. For example, mr-auto (margin right) and ml-auto (margin left) would have to be changed to me-auto (margin end) and ms-auto (margin start)

  <nav class="navbar navbar-expand-sm bg-light">
     <div class="container-xxl">
      <button class="navbar-toggler ms-auto" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerflex-1" aria-controls="navbarTogglerflex-1" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse align-items-baseline" id="navbarTogglerflex-1">
        <div class="col">
          <a class="navbar-brand" href="#">
            <i class="bi bi-bootstrap-fill h2 align-middle"></i>
            Bootstrap
          </a>
        </div>
        <ul class="navbar-nav mb-2 mb-sm-0">
          <li class="nav-item">
            <a class="nav-link active" aria-current="page" href="#">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>
          <li class="nav-item">
            <a class="nav-link disabled">Disabled</a>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Dropdown link
            </a>
            <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <li><a class="dropdown-item" href="#">Action</a></li>
              <li><a class="dropdown-item" href="#">Another action</a></li>
              <li><a class="dropdown-item" href="#">Something else here</a></li>
            </ul>
          </li>
        </ul>
        <div class="col">
          <form class="ms-sm-auto w-75 " role="search">
            <div class="input-group input-group-sm mb-3 flex-nowrap">
              <input type="text" class="form-control" placeholder="Search">
              <button type="button" class="btn btn-secondary">
                <i class="bi bi-search"></i>
                <span class="visually-hidden">Search</span>
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </nav>
Sam Miller
  • 95
  • 1
  • 6