Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap - dropdowns inside navbar but outside nav-collapse

I want to add a dropdown to the navbar, but ensure that when the browser is resized to a narrower width, the dropdown remains visible in the navbar and is not included in the nav-collapse.

The html below works. However, when the page resizes, the dropdown drops onto the next row, and displays expanded in the navbar, leaving me with a very deep navbar.

I'm calling bootstrap-collapse.js and bootstrap-dropdown.js.

Anyone got any ideas?

<div class="navbar navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container">
      <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </a>
      <a class="brand" href="#">Project</a>
      <div class="nav-collapse">
        <ul class="nav">
          <li class="active"><a href="#">Home</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </div><!--/.nav-collapse -->
      <div class="container">
        <ul class="nav pull-right">
            <li class="divider-vertical"></li>
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Sharing<b class="caret"></b></a>
                <ul class="dropdown-menu">
                    <li><a href="#">Twitter</a></li>
                    <li><a href="#">Facebook</a></li>
                    <li><a href="#">Google+</a></li>
                </ul>
            </li>
        </ul>
      </div>
    </div>
  </div>
</div>
like image 406
Tim Threlfall Avatar asked Feb 21 '12 21:02

Tim Threlfall


1 Answers

According to Mark Otto, one of the bootstrap's developers:

All dropdowns within the .nav in the navbar will be affected. Currently we don't offer a way to prevent this behavior.

However, it is possible to have an uncollapsed dropdown in a collapsed navbar. You can accomplish this in one of three ways:

  1. Add a data attribute to the dropdown (data-no-collapse="true") to fix the responsive selectors.
  2. Replace the dropdown’s class (class="dropdown-menu-no-collapse") to rebuild its styles from the ground up.
  3. Add another class to the dropdown (class="dropdown-menu no-collapse) to override the responsive styles.

I wrote a blog post that explains what to do. The first method requires editing bootstrap-responsive.css, and the second requires changing bootstrap.css. You don't have to change any of the bootstrap's CSS for the third method, but it's pretty hard to maintain.

like image 134
Taylor Fausak Avatar answered Nov 04 '22 12:11

Taylor Fausak