skip to Main Content

I am new to coding. I am unable to create a navbar icon that changes on small screen when it is clicked.

I have tried using a bootstrap navbar, when I click the navbar on small screen, it only shows the navbar list but the icon doesn’t change.
actual result

I expect to have the navbar icon to change when clicked on small screen as in the image below.
expected outcome

Here is my code

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

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous" />

  <link rel="stylesheet" href="styles.css" />
</head>

<body>
  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <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">
        </li>
        <li class="nav-item active">
          <a class="nav-link" href="#">Office <span class="sr-only">(current)</span></a
            >
          </li>
          <li class="nav-item active">
            <a class="nav-link" href="#"
              >Windows<span class="sr-only">(current)</span></a
            >
          </li>
          <li class="nav-item active">
            <a class="nav-link" href="#"
              >Xbox<span class="sr-only">(current)</span></a
            >
          </li>
          <li class="nav-item active">
            <a class="nav-link" href="#"
              >Support<span class="sr-only">(current)</span></a
            >
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
        </li>
      </ul>

      <ul class="navbar-nav mr-auto">
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">All Microsoft</a
            >

            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
              <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
    </div>
    </li>
    <li>
      <a href="#" class="user"><i class="fas fa-search"></i></a>
    </li>
    <li>
      <a href="#" class="user"><i class="fas fa-shopping-cart"></i></a>
    </li>
    <li>
      <a href="#" class="user"><i class="far fa-user-circle"></i></a>
    </li>
    </ul>
    </div>
  </nav>

  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</body>

</html>

I need help with this, I am new to Dev and Stackoverflow.

3

Answers


  1. It’s not a default behavior of bootstrap navbar. Add cross icon in your html code near the navbar-toggler-icon

    For example:

    <span class="navbar-toggler-icon"></span>
    <span class="navbar-close-icon">X</span>
    

    Add some styles for it:

    .navbar-toggler-icon {
      display: none;
    }
    .navbar-close-icon {
      display:inline-block;
      width: 30px;
      height: 30px;
      line-height: 30px;
    }
    .navbar-toggler.collapsed .navbar-close-icon {
      display: none;
    }
    .navbar-toggler.collapsed .navbar-toggler-icon {
      display: inline-block;
    }
    

    And add class collapsed for <button class="navbar-toggler navbar-toggler-right"> by default

    Full example:

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <link
          rel="stylesheet"
          href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
          crossorigin="anonymous"
        />
        <link
          rel="stylesheet"
          href="https://use.fontawesome.com/releases/v5.7.0/css/all.css"
          integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ"
          crossorigin="anonymous"
        />
        <style>
        .navbar-toggler-icon {
          display: none;
        }
        .navbar-close-icon {
          display:inline-block;
          width: 30px;
          height: 30px;
          line-height: 30px;
        }
        .navbar-toggler.collapsed .navbar-close-icon {
          display: none;
        }
        .navbar-toggler.collapsed .navbar-toggler-icon {
          display: inline-block;
        }
        </style>
    
        <link rel="stylesheet" href="styles.css" />
      </head>
      <body>
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
          <button
            class="navbar-toggler collapsed"
            type="button"
            data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent"
            aria-expanded="false"
            aria-label="Toggle navigation"
          >
            <span class="navbar-toggler-icon"></span>
            <span class="navbar-close-icon">X</span>
          </button>
    
          <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav mr-auto">
              <li class="nav-item active">
              </li>
              <li class="nav-item active">
                <a class="nav-link" href="#"
                  >Office <span class="sr-only">(current)</span></a
                >
              </li>
              <li class="nav-item active">
                <a class="nav-link" href="#"
                  >Windows<span class="sr-only">(current)</span></a
                >
              </li>
              <li class="nav-item active">
                <a class="nav-link" href="#"
                  >Xbox<span class="sr-only">(current)</span></a
                >
              </li>
              <li class="nav-item active">
                <a class="nav-link" href="#"
                  >Support<span class="sr-only">(current)</span></a
                >
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
              </li>
            </ul>
    
            <ul class="navbar-nav mr-auto">
              <li class="nav-item dropdown">
                <a
                  class="nav-link dropdown-toggle"
                  href="#"
                  id="navbarDropdown"
                  role="button"
                  data-toggle="dropdown"
                  aria-haspopup="true"
                  aria-expanded="false"
                  >All Microsoft</a
                >
    
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                  <a class="dropdown-item" href="#">Action</a>
                  <a class="dropdown-item" href="#">Another action</a>
                  <div class="dropdown-divider"></div>
                  <a class="dropdown-item" href="#">Something else here</a>
                </div>
              </li>
              <li>
                <a href="#" class="user"><i class="fas fa-search"></i></a>
              </li>
              <li>
                <a href="#" class="user"><i class="fas fa-shopping-cart"></i></a>
              </li>
              <li>
                <a href="#" class="user"><i class="far fa-user-circle"></i></a>
              </li>
            </ul>
          </div>
        </nav>
    
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
      </body>
    </html>
    Login or Signup to reply.
  2. To change the navbar icon when it is clicked on small screen, you can add some JavaScript to toggle a class on the navbar-toggler-icon element.

    <script>
      var navbarTogglerIcon = document.getElementById("navbar-toggler-icon");
      navbarTogglerIcon.addEventListener("click", function() {
        navbarTogglerIcon.classList.toggle("close");
      });
    </script>
    .navbar-toggler-icon.close {
      background-image: url('close-icon.png');
      /* add any other styles you want */
    }
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span id="navbar-toggler-icon" class="navbar-toggler-icon"></span>
    </button>
    Login or Signup to reply.
  3. This is not supported by default in Bootstrap 4. You will have to write some additional CSS to display the respective icons when the .navbar-toggler is or isn’t .collapsed.

    .close {
      display: none;
    }
    
    .navbar-toggler:not(.collapsed)>.navbar-toggler-icon {
      display: none;
    }
    
    .navbar-toggler:not(.collapsed)>.close {
      display: inline-block;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Document</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
      <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous" />
    
      <link rel="stylesheet" href="styles.css" />
    </head>
    
    <body>
      <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
        <span class="close px-2 py-1">X</span>
          </button>
    
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
            </li>
            <li class="nav-item active">
              <a class="nav-link" href="#">Office <span class="sr-only">(current)</span></a
                >
              </li>
              <li class="nav-item active">
                <a class="nav-link" href="#"
                  >Windows<span class="sr-only">(current)</span></a
                >
              </li>
              <li class="nav-item active">
                <a class="nav-link" href="#"
                  >Xbox<span class="sr-only">(current)</span></a
                >
              </li>
              <li class="nav-item active">
                <a class="nav-link" href="#"
                  >Support<span class="sr-only">(current)</span></a
                >
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
            </li>
          </ul>
    
          <ul class="navbar-nav mr-auto">
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">All Microsoft</a
                >
    
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                  <a class="dropdown-item" href="#">Action</a>
              <a class="dropdown-item" href="#">Another action</a>
              <div class="dropdown-divider"></div>
              <a class="dropdown-item" href="#">Something else here</a>
        </div>
        </li>
        <li>
          <a href="#" class="user"><i class="fas fa-search"></i></a>
        </li>
        <li>
          <a href="#" class="user"><i class="fas fa-shopping-cart"></i></a>
        </li>
        <li>
          <a href="#" class="user"><i class="far fa-user-circle"></i></a>
        </li>
        </ul>
        </div>
      </nav>
    
      <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    </body>
    
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search