navbar doesn't expand on mobile with bootstrap5

Viewed 6040

I'm making a website with Bootstrap 5 and my navbar doesn't work. When I'm on mobile it does not expand.

@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
/*
#FF3633 - Vermelho

*/
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}
/* estilo - header */
.navbar {
    background-color: #131313;
    padding: 1rem 8rem;
    z-index: 1000;

}

.navbar .navbar-brand {

    font-size: 1.4rem;
    font-weight: 800;
}

#navbarSupportedContent > ul > li:nth-child(n) > a {
  color: #fff;
  font-size: 1.1rem;
  padding: 0 0.8rem;
}

#navbarSupportedContent > ul > li:nth-child(n) > a:hover {
    color: #FF3633;
}

#navbarSupportedContent button {
    background-color: #FF3633;
    font-weight: 600;
    padding: 0.4rem 1.4rem;
    border-radius: 30px;
    outline: none;
}

#navbarSupportedContent button:hover {
    background-color: #CC3036;
}
<header>
<nav class="navbar navbar-expand-lg navbar-dark fixed-top">
    <div class="container-fluid">
      <a class="navbar-brand" href="#">
        <img src="img/logo-wt.png" width="100" height="30" alt="">
      </a>
      <button class="navbar-toggler" type="button" data-bs-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 mx-auto mb-2 mb-lg-0">
          <li class="nav-item">
            <a class="nav-link" href="#">Quem Somos</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Serviços</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Serviços</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Contato</a>
          </li>

        </ul>
          <button class="btn text-light" type="submit">Contato</button>
        </div>
    </div>

</nav>
<div class="mid">
    <video autoplay muted loop>
        <source class="embed-responsive" src="darkvd.mp4" type="video/mp4">
    </video>
    <div class="hero text-center">
        <h2 class="text-light display-3 fw-bold m-lg-3 mx-3">Suporte Linux e Cloud</h2>
        <p class="text-light mx-auto">Criando um paralelo entre baixo custo, disponibilidade, praticidade e satisfação</p>
        <a class="text-light fw-bold m-lg-3 mx-3 " href="#"> SAIBA MAIS </a> 

    </div>
</div>
<header>
<nav class="navbar navbar-expand-lg navbar-dark fixed-top">
    <div class="container-fluid">
      <a class="navbar-brand" href="#">
        <img src="img/logo-wt.png" width="100" height="30" alt="">
      </a>
      <button class="navbar-toggler" type="button" data-bs-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 mx-auto mb-2 mb-lg-0">
          <li class="nav-item">
            <a class="nav-link" href="#">Quem Somos</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Serviços</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Serviços</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Contato</a>
          </li>

        </ul>
          <button class="btn text-light" type="submit">Contato</button>
        </div>
    </div>

</nav>
<div class="mid">
    <video autoplay muted loop>
        <source class="embed-responsive" src="darkvd.mp4" type="video/mp4">
    </video>
    <div class="hero text-center">
        <h2 class="text-light display-3 fw-bold m-lg-3 mx-3">Suporte Linux e Cloud</h2>
        <p class="text-light mx-auto">Criando um paralelo entre baixo custo, disponibilidade, praticidade e satisfação</p>
        <a class="text-light fw-bold m-lg-3 mx-3 " href="#"> SAIBA MAIS </a> 

    </div>
</div>

button pic

button pic

5 Answers

Bootstrap 5 has namespaced the data attribute names with data-bs-*.
Change the following:

data-target="#navbarSupportedContent"

to this:

data-bs-target="#navbarSupportedContent"

In your button in which you are collapsing the navbar then main attribute tags are not there. Main attributes used are data-toggle="collapse" data-target=".nav-collapse". Second change is that see if you added these links or add bundle from https://getbootstrap.com/docs/5.0/getting-started/introduction/

<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.6.0/dist/umd/popper.min.js" integrity="sha384-KsvD1yqQ1/1+IA7gi3P0tyJcT3vR+NdBTt13hSJ2lnve8agRGXTTyNaBYmCR/Nwi" crossorigin="anonymous"></script>

Hope my answer helps you

I have managed to solve it with js codes from my external js file which I had already included on the page before closing of body tag.

Below here are my js codes which I used to and managed to solve the issue:

$(document).ready(function(){

  //--Navbar_Toggle_icon--//
  NavItemsStatus = '';
  $('.navbar-toggler-icon').mousedown(function(){
    //---Check if the NavItems are opened, if so then close them, otherwise open them--//
    if((NavItemsStatus == "")|| (NavItemsStatus == undefined)){
      NavItemsStatus = 'on';
      //--Do nothing here as the nav items are sliding down after clicked for the first time--// 
    }else if((NavItemsStatus == "off")){
      NavItemsStatus = 'on';
      $("#navbarNavAltMarkup").show(600); //the id which was called on data-bs-target
    }else{
      NavItemsStatus = 'off';
      $("#navbarNavAltMarkup").hide();
      $("#navbarNavAltMarkup").css('height','0px');
    }
  })

});

So for your case the id to use is #navbarSupportedContent.

for the navbar to work you need to include some javascript (even if you will not use it). The provided links in bootstrap -> get started-> JS do not really work.

So to make it fast and easy for you, here is the header where you need to add your title.

<head>
  <meta charset="utf-8">
  <title>TinDog</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
  <link rel="stylesheet" href="css/styles.css">


  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>

There is my universal working solution:

$(document).ready(function(){
  $('a[data-bs-toggle="collapse"]').mouseup(function(){
    var expanded = $(this).attr('aria-expanded');
    if(expanded=="true"){
      setTimeout(() => {
        hide_collapse(this);
      }, 100);
    }
  })
  function hide_collapse(element){
    var aria_control = $(element).attr('aria-controls');
    $("#"+aria_control).removeClass("show");
    $(element).attr('aria-expanded',"false");
  }
});
Related