How to add hamburger menu in bootstrap
Asked Answered
O

3

63

I need some help with bootstrap nav. I want it to be toggled via a hamburger icon on mobile.

Here it is on codepen: http://codepen.io/sadman/pen/hfGwv (link invalid)

.navbar,
.navbar-inverse {
  border-radius: 0;
  border: none;
  margin-bottom: 0;
  min-height: 80px;
}

.nav li {
  display: inline;
  color: white;
}

.navbar-inverse .navbar-nav>li>a {
  color: #ffffff;
  font-family: Lato;
  font-size: 1.7em;
  font-weight: 300;
  padding: 30px 25px 33px 25px;
}

.navbar-inverse .navbar-nav li a:hover {
  background-color: #444444;
  transition: 0.7s all linear;
  height: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">

<nav class="navbar navbar-inverse navbar-static-top" role="navigation">
  <div class="container">
    <ul class="nav navbar-nav">
      <li><a href="index.php">Home</a></li>
      <li><a href="about.php">About</a></li>
      <li><a href="#portfolio">Portfolio</a></li>
      <li><a href="#">Blog</a></li>
      <li><a href="contact.php">Contact</a></li>
    </ul>
  </div>
</nav>
Outbreak answered 11/10, 2014 at 18:8 Comment(0)
C
93

All you have to do is read the code on getbootstrap.com:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">

<nav class="navbar navbar-inverse navbar-static-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><a href="index.php">Home</a></li>
        <li><a href="about.php">About</a></li>
        <li><a href="#portfolio">Portfolio</a></li>
        <li><a href="#">Blog</a></li>
        <li><a href="contact.php">Contact</a></li>
      </ul>
    </div>
  </div>
</nav>
Critchfield answered 11/10, 2014 at 22:40 Comment(3)
is the bs-example-navbar-collapse-1 required? seems like its from cut-n-paste, don't know if it's actually necessary or how it's used if it is.Kei
Use an id you want, it's just and example.Critchfield
Question, what does the span with class = "sr-only" do? I tried removing it and there's no difference.Streaming
E
14

To create icon you can use Glyphicon in Bootstrap:

<a href="#" class="btn btn-info btn-sm">
    <span class="glyphicon glyphicon-menu-hamburger"></span>
</a>

And then control size of icon in css:

.glyphicon-menu-hamburger {
  font-size: npx;
}
Elul answered 20/4, 2016 at 11:17 Comment(3)
This does not work out of the box with Bootstrap 4 (i.e. without including the glyphicons), as glyphicons are no longer packaged with Bootstrap.Kermes
oh, ok. didn't work with Bootstrap long time. i'll edit my answer.Elul
You can do it in Bootstrap 4 without glyphicon: getbootstrap.com/docs/4.4/components/navbar/#togglerStokeontrent
D
0

CSS only (no icon sets) Codepen

.nav-link #navBars {
margin-top: -3px;
padding: 8px 15px 3px;
border: 1px solid rgba(0,0,0,.125);
border-radius: .25rem;
}

.nav-link #navBars input {
display: none;
}

.nav-link #navBars span {
position: relative;
z-index: 1;
display: block;
margin-bottom: 6px;
width: 24px;
height: 2px;
background-color: rgba(125, 125, 126, 1);
border-radius: .25rem;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
   <!-- <a class="navbar-brand" href="#">
      <img src="https://getbootstrap.com/docs/4.0/assets/brand/bootstrap-solid.svg" width="30" height="30" class="d-inline-block align-top" alt="">
      Bootstrap
      </a> -->
   <!-- https://stackoverflow.com/questions/26317679 -->
   <a class="nav-link" href="#">
      <div id="navBars">
         <input type="checkbox" /><span></span>
         <span></span>
         <span></span>
      </div>
   </a>
   <!-- /26317679 -->
   <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav">
         <li class="nav-item active"><a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a></li>
         <li class="nav-item"><a class="nav-link" href="#">Features</a></li>
         <li class="nav-item"><a class="nav-link" href="#">Pricing</a></li>
         <li class="nav-item"><a class="nav-link disabled" href="#">Disabled</a></li>
      </ul>
   </div>
</nav>
Drome answered 15/4, 2020 at 17:21 Comment(1)
The hamburger menu does not respond to click event.Chenab

© 2022 - 2024 — McMap. All rights reserved.