user2655858
user2655858

Reputation: 251

Problems with the responsive navbar in Bootstrap 3

I had no problem with the responsive navbar in the last version of Bootstrap, but I cannot get version 3 to work. The toggle button appears properly and everything disappears but the brand, but the button does not respond to clicks. (http://getbootstrap.com/components/#navbar-responsive) Any help would be greatly appreciated! Here is the code so far:

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

    <head>
    <title>Hello World</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="js/bootstrap.min.js"></script>
        <link href="css/bootstrap.css" rel="stylesheet" media="screen">
    </head>

    <body>
        <section>   
            <div class="navbar navbar-fixed-top">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a href= "#" class="navbar-brand">Brand Name</a>
                <div class="nav-collapse navbar-responsive-collapse collapse">
                    <ul class="nav navbar-nav pull-right">
                        <li class="active "><a href="#">Home</a></li>
                        <li><a href="#">Link 1</a></li>
                        <li><a href="#">Link 2</a></li>
                        <li><a href="#">Link 3</a></li>
                        <li><a href="#">Link 4</a></li>
                    </ul>   
                </div>  
            </div>
        </section>

Upvotes: 25

Views: 79116

Answers (7)

lost-and-found
lost-and-found

Reputation: 176

Twitter bootstrap uses the bundled Collapse jquery plugin to toggle the navigation menu on mobile screen form factors. Your question pertains to a slightly old release of bootstrap but still remains relevant – the toggle can be triggered on an element by setting:

data-toggle="collapse" and data-target="#valid_css_selector_of_target_element".

The catch here is the latter – as far as the functioning of Collapse plugin is concerned it doesn't matter whether you have a predefined bootstrap class .navbar-responsive-collapse, the only thing that matters here is that element exists in your html. The existence of a bootstrap class however would very likely affect how it's styled.

The bootstrap documentation at the time of this writing (and I believe since long) shows, via an example, the use of an id attribute to select the target element; which I find a bit better because there can be more than one elements with the same class on the same page, while Ids are unique in an html document. So, in your case:

<div class="collapse navbar-collapse navbar-ex1-collapse"> would look something like <div class="collapse navbar-collapse navbar-ex1-collapse" id="my_nav"> and the toggle button would have an attribute data-target="#my_nav".

Upvotes: 1

Alex W
Alex W

Reputation: 38173

I am using Bootstrap 3.2.0. What fixed this problem for me was changing the version of jQuery I was using. Apparently, jQuery 1.7.1 is not compatible with the latest bootstrap. After switching to jQuery 2.0.2, all was well:

<script type='text/javascript' src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

Upvotes: 0

baron5
baron5

Reputation: 587

This worked for me: I changed .navbar-ex1-collapse to .navbar-collapse.

Upvotes: 0

user3234300
user3234300

Reputation: 21

Bootstrap 3 requires jquery.

--> Best practice to put your scripts at the end too.

<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="js/bootstrap.min.js"></script>

Upvotes: 2

KLEW
KLEW

Reputation: 166

Change this: .navbar-responsive-collapse

to this: .navbar-collapse

Original poster solved his own question. Posting a more concise version here as this helped me. PhilNicholas' version did not work.

Upvotes: 3

Azarsa
Azarsa

Reputation: 1308

make sure link the bootstrap.cs before bootstrap-responsive.css

<link href="css/bootstrap.css" rel="stylesheet" />  
<link href="css/bootstrap-responsive.css" rel="stylesheet" />

https://stackoverflow.com/a/9626716/2127493

Upvotes: 0

Phil Nicholas
Phil Nicholas

Reputation: 3719

I believe the code below will produce the effect you're looking for. Specifically, there is no "navbar-responsive-collapse" class in BS3 (see the "bootstrap.css" file). Beginning with release 3 Twitter Bootstrap is responsive by default, so many of the code flags for responsiveness are now baked into the framework and no longer need to be called explicitly.

Here's a BS3 version of the right-aligned collapsible menu:

<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
    <span class="sr-only">Toggle navigation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
</button>
<a href= "#" class="navbar-brand">Brand Name</a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
    <ul class="nav navbar-nav pull-right">
        <li class="active "><a href="#">Home</a></li>
        <li><a href="#">Link 1</a></li>
        <li><a href="#">Link 2</a></li>
        <li><a href="#">Link 3</a></li>
        <li><a href="#">Link 4</a></li>
    </ul>   
</div>
</div>

Upvotes: 12

Related Questions