user46688 user46688 - 5 months ago 43
Javascript Question

Bootstrap 3.3.x, how to get correct order of menu items for navbar when collapsed?

Bootstrap newbie using 3.3.6, and having trouble creating a simple navbar based on the example Twitter provides in their documentation. The code I show below sets up a navbar that has two menu items on the top right corner. For large screens, it looks like:

Brand Menu 1 Menu 2


But when I collapse the navbar for the smallest screen, it shows:

Brand
Menu 2
Menu 1


How do I get the collapsed order to be:

Brand
Menu 1
Menu 2


?

<nav class="navbar navbar-default navbar-static-top navbar-default-siteheader navbar-inner-siteheader">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header" >
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</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 navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"
role="button" aria-haspopup="true" aria-expanded="false">Menu 2<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
</ul>
</li>
</ul>

<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"
role="button" aria-haspopup="true" aria-expanded="false">Menu 1<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Item A</a></li>
<li><a href="#">Item B</a></li>
<li><a href="#">Item C</a></li>
</ul>
</li>
</ul>

</div> <!-- /.navbar-collapse -->
</div> <!-- /.container -->
</nav>

Answer

If you want the 2 dropdown menus to be on the right, you don't have to create 2 navbar-right.

Just put the 2 <li class="dropdown"> into a single <ul class="nav navbar-nav navbar-right">.

Like this :

<nav class="navbar navbar-default navbar-static-top navbar-default-siteheader navbar-inner-siteheader">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header" >
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Brand</a>
        </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 navbar-right">
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" 
                    role="button" aria-haspopup="true" aria-expanded="false">Menu 1<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Item A</a></li>
                        <li><a href="#">Item B</a></li>
                        <li><a href="#">Item C</a></li>
                    </ul>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" 
                     role="button" aria-haspopup="true" aria-expanded="false">Menu 2<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Item 1</a></li>
                        <li><a href="#">Item 2</a></li>
                        <li><a href="#">Item 3</a></li>
                    </ul>
                </li>

            </ul>

        </div>  <!-- /.navbar-collapse -->
    </div> <!-- /.container -->
</nav>

Here's a JsFiddle : Demo

Comments