7

I've been learning bootstrap 3 and sass for last few days. Anyway, I'm trying to make a navbar that collapses when screen size is below 1000px. And it works, but when I click on toggle button, to show menu dropdown, it opens, then instantly closes.

Here is the code: http://jsbin.com/zahatekisa/edit?html,css,output

.navbar {
  background-color: #202C39;
}
div.navbar-header a.navbar-brand {
  font-family: "Caveat Brush";
  margin-right: 20px;
  font-size: 23px;
  color: #F29559;
}
div.navbar-header a.navbar-brand:hover {
  color: #ffb36b;
}
ul.nav.navbar-nav li a {
  font-family: "PT Sans", "cursive";
  color: #e6e6e6;
}
ul.nav.navbar-nav li a:hover {
  color: #ffffff;
  background-color: #2a394a;
}
ul.navbar-right li p.navbar-text {
  display: block;
}
@media (max-width: 1050px) {
  ul.navbar-right li a {
    float: left;
  }
  ul.navbar-right li p.navbar-text {
    margin-left: 15px;
    position: relative;
    top: -4px;
  }
}
@media (max-width: 1000px) {
  .navbar-header {
    float: none;
  }
  .navbar-toggle {
    display: block;
  }
  .navbar-collapse {
    border-top: 1px solid transparent;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
  }
  .navbar-collapse.collapse {
    display: none !important;
  }
  .navbar-nav {
    float: none !important;
    margin: 7.5px -15px;
  }
  .navbar-nav > li {
    float: none;
  }
  .navbar-nav > li > a {
    padding-top: 10px;
    padding-bottom: 10px;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <!--Making the toggle button-->
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <!--Logo-->
      <a href="#" class="navbar-brand">Lorem ipsum dolor.</a>
    </div>

    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li><a href="#">Lorem</a>
        </li>
        <li><a href="#">Lorem</a>
        </li>
        <li><a href="#">Lorem</a>
        </li>
        <li><a href="#">Lorem</a>
        </li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li>
          <p class="navbar-text">Contact Me:</p>
        </li>
        <li><a href="#"><i class="fa fa-facebook-official" aria-hidden="true"></i></a>
        </li>
        <li><a href="#"><i class="fa fa-twitter-square" aria-hidden="true"></i></a>
        </li>
        <li><a href="#"><i class="fa fa-skype" aria-hidden="true"></i></a>
        </li>
        <li><a href="#"><i class="fa fa-envelope" aria-hidden="true"></i></a>
        </li>
      </ul>
    </div>
  </div>
</nav>
Web Developer
  • 333
  • 4
  • 17
petek
  • 953
  • 4
  • 14
  • 24
  • 1
    Please add meaningful code and a problem description here. Don't just link to the site that needs fixing - otherwise, this question will lose any value to future visitors once the problem is solved or if the site you're linking to is inaccessible. Posting a [Minimal, Complete, and Verifiable example (MCVE)](http://stackoverflow.com/help/mcve) that demonstrates your problem would help you get better answers. For more info, see [Something on my web site doesn't work. Can I just paste a link to it?](http://meta.stackexchange.com/questions/125997/) Thanks! – j08691 Jun 15 '16 at 15:28
  • Post the actual jsbin code, not the output. – Rachel S Jun 15 '16 at 15:29
  • Sorry. Thought you could just look at source. Here is the jsbin with code: http://jsbin.com/zahatekisa/edit?html,css,output – petek Jun 15 '16 at 15:32
  • 1
    Add your code into the post. – max Jun 15 '16 at 15:39

1 Answers1

6

The problem here is this particular style you have, which is forcing the navbar to be hidden regardless of whether it is open or closed:

@media (max-width: 1000px) {
    .navbar-collapse.collapse {
        display: none !important;
    }
}

I can see what you're doing here - you're trying to combine Bootstrap's .collapse class with your own custom addition to the navbar. However, there's a problem with your logic: I suspect you wrote your style under the assumption that Bootstrap toggles .collapse to hide or show collapsed content. This is incorrect. In fact, Bootstrap toggles .in for this - so you should not be hiding the content if the element has the class .in. Simply change your selector to:

@media (max-width: 1000px) {
    .navbar-collapse.collapse:not(.in) {
        display: none !important;
    }
}

Here's the fix in context:

.navbar {
  background-color: #202C39;
}
div.navbar-header a.navbar-brand {
  font-family: "Caveat Brush";
  margin-right: 20px;
  font-size: 23px;
  color: #F29559;
}
div.navbar-header a.navbar-brand:hover {
  color: #ffb36b;
}
ul.nav.navbar-nav li a {
  font-family: "PT Sans", "cursive";
  color: #e6e6e6;
}
ul.nav.navbar-nav li a:hover {
  color: #ffffff;
  background-color: #2a394a;
}
ul.navbar-right li p.navbar-text {
  display: block;
}
@media (max-width: 1050px) {
  ul.navbar-right li a {
    float: left;
  }
  ul.navbar-right li p.navbar-text {
    margin-left: 15px;
    position: relative;
    top: -4px;
  }
}
@media (max-width: 1000px) {
  .navbar-header {
    float: none;
  }
  .navbar-toggle {
    display: block;
  }
  .navbar-collapse {
    border-top: 1px solid transparent;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
  }
  .navbar-collapse.collapse:not(.in) {
    display: none !important;
  }
  .navbar-nav {
    float: none !important;
    margin: 7.5px -15px;
  }
  .navbar-nav > li {
    float: none;
  }
  .navbar-nav > li > a {
    padding-top: 10px;
    padding-bottom: 10px;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <!--Making the toggle button-->
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <!--Logo-->
      <a href="#" class="navbar-brand">Lorem ipsum dolor.</a>
    </div>

    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li><a href="#">Lorem</a>
        </li>
        <li><a href="#">Lorem</a>
        </li>
        <li><a href="#">Lorem</a>
        </li>
        <li><a href="#">Lorem</a>
        </li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li>
          <p class="navbar-text">Contact Me:</p>
        </li>
        <li><a href="#"><i class="fa fa-facebook-official" aria-hidden="true"></i></a>
        </li>
        <li><a href="#"><i class="fa fa-twitter-square" aria-hidden="true"></i></a>
        </li>
        <li><a href="#"><i class="fa fa-skype" aria-hidden="true"></i></a>
        </li>
        <li><a href="#"><i class="fa fa-envelope" aria-hidden="true"></i></a>
        </li>
      </ul>
    </div>
  </div>
</nav>

(Something else worth noting is that you may want to use max-width: 768px rather than max-width: 1000px for your media query, since that's the Bootstrap breakpoint for "tablet" sized devices.)

Web Developer
  • 333
  • 4
  • 17
Serlite
  • 12,130
  • 5
  • 38
  • 49
  • Thanks for answer, this helped me solve this issue with my problem but with minor change in Bootstrap 4. In Bootstrap 4, you have to change ".in" for ".show": @media (max-width: 1000px) { .navbar-collapse.collapse:not(.show) { display: none !important; } } – jic Oct 11 '18 at 14:50