Bootstrap Dropdown Not Un-Collapsing Menu

I've looked everywhere, but can't seem to find the answer to my solution. I am following a tutorial on how to make a navbar for my website and I am trying to make it mobile friendly as well. I want the navbar to display a button that un-collapses the menu on a smaller device. But even after I added jQuery, it still doesn't open the menu.

Here is my code that I have, hopefully it is just a simple fix that I am overlooking.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Personal Website</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles/main.css">


<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">       </script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


</head>
<body>

<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <!-- Logo -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="mainNavbar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a href="#" class="navbar-brand">Test</a>
        </div>

        <!-- Menu Items -->
        <div class="collapse navbar-collapse" id="mainNavbar">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a> </li>
                <li><a href="#">About</a> </li>
                <li><a href="#">Contact</a> </li>

                <!-- Drop Down Menu -->
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">My Profile <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">About</a> </li>
                        <li><a href="#">Contact</a> </li>
                    </ul>
                </li>
            </ul>

            <!-- Right Align -->
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Logout</a> </li>
            </ul>
        </div>
    </div>
</nav>
</body>

</html>

Thanks for the help in advance!

Answers 1

  • there is a mistake in your button. you forgot to place the # in your data-target attribute.

    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#mainNavbar">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
    

Related Articles