Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Collapse buttons in navbar with Bootstrap 3.0

I'm using Bootstrap 3.0 to show a navbar with some links and a search field. When i go to "mobile version" i have a button that collapses the navbar and show all the content: the links and the search field. Now I need two buttons, one to collapse only the links of the navbar and the other button to collapse only the search field. How can i make this?

like image 881
Martín Avatar asked Sep 16 '13 05:09

Martín


People also ask

How do I collapse the navbar in Bootstrap 3?

You can include form controls by using . navbar-form on the <form> element. This Bootstrap class adjusts the vertical alignment and collapses the form in smaller viewports. Forms within navbars also require one of the alignment classes to align the form controls within the navbar (see alignment below).

How do I create a collapsing navigation bar in Bootstrap?

To create a collapsible navigation bar, use a button with class="navbar-toggler", data-toggle="collapse" and data-target="#thetarget" . Then wrap the navbar content (links, etc) inside a div element with class="collapse navbar-collapse" , followed by an id that matches the data-target of the button: "thetarget".

How do you keep multiple collapses open in Bootstrap 4?

Assuming that you're using Bootstrap 4, you can simply remove the data-parent attribute from the element with the collapse class. This subscribes the collapse to events on #accordionExample , which is the main accordion element, via the data-parent attribute.


1 Answers

Separate the links and the search form into two navbar-collapse elements and have each button target the corresponding one:

Sample markup:

<nav class="navbar navbar-default" role="navigation">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#search">Toggle search</button>
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#links">Toggle links</button> 
        <a class="navbar-brand" href="#">Brand</a>
    </div>
    <ul class="collapse navbar-collapse nav navbar-nav" id="links">
        <!--Links go here-->
    </ul>
    <div class="collapse navbar-collapse" id="search">
        <form class="navbar-form navbar-left" role="search">
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Search">
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
        </form>
    </div>
</nav>

You would then just need to fix some conflicting styles between .navbar-nav and .navbar-collapse by adding this to your CSS:

@media (max-width:767px) {
    .navbar-nav.navbar-collapse {
        margin-left: 0;
        margin-right: 0;
        padding-left: 0;
        padding-right: 0;
    }
}

Here's a demo fiddle

like image 197
omma2289 Avatar answered Sep 19 '22 20:09

omma2289