Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle Bootstrap Tabs to Dropdown in Mobile view

I have a website developing with Bootsrtap 3, I have tab element there. I have to make tabbed items as drop downs when browsing from mobile deveice. Is it possible?

<!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist">
      <li class="active"><a href="#home" role="tab" data-toggle="tab">Home</a></li>
      <li><a href="#profile" role="tab" data-toggle="tab">Profile</a></li>
      <li><a href="#messages" role="tab" data-toggle="tab">Messages</a></li>
      <li><a href="#settings" role="tab" data-toggle="tab">Settings</a></li>
    </ul>
    <!-- Tab panes -->
    <div class="tab-content">
      <div class="tab-pane fade in active" id="home">A</div>
      <div class="tab-pane fade" id="profile">B</div>
      <div class="tab-pane fade" id="messages">C</div>
      <div class="tab-pane fade" id="settings">D</div>
    </div>

I am using the basic Tabs with the bootstrap.

like image 325
Shafeeque Avatar asked Sep 01 '14 06:09

Shafeeque


People also ask

How do I switch between bootstrap tabs?

To make the tabs toggleable, add the data-toggle="tab" attribute to each link. Then add a .tab-pane class with a unique ID for every tab and wrap them inside a <div> element with class .tab-content .

How do I make my nav tab active?

With the help of HTML, CSS, and JavaScript, it is possible to create a navigation menu with a curved active tab. This can be done by using the ::before and ::after pseudo-elements to create the desired shape, and then using JavaScript to add the active class to the element.


Video Answer


2 Answers

The following css applies a basic design to the tabs for devices where device-width >=480px. You can change this limit width for higher phone resolution :

@media screen and (max-width: 480px) {
    .nav {
        padding-left:2px;
        padding-right:2px;
    }
    .nav li {
        display:block !important;
        width:100%;
        margin:0px;
    }
    .nav li.active {
        border-bottom:1px solid #ddd!important;
        margin: 0px;
    }
}

http://jsfiddle.net/e7mzx24v/

Hope it helps

like image 190
Yooz Avatar answered Sep 21 '22 10:09

Yooz


I came here by mistake, but having had a look at the answers, and seen that many people are looking for it, I have posted this answer. There is, in fact, an easy way to do all that.

You can use the bootstrap native classes 'visible-xs' and 'hidden-xs', this will automatically hide or show one of the two controls depending with which device the users are opening the page.

An example is given here:

<div class="tabbable">
    <ul class="mb10 nav nav-pills nav-justified form-tabs hidden-xs">
       <li class="tab-selector active"><a href="#tab1" data-toggle="tab">Tab 1</a></li>
       <li class="tab-selector"><a href="#tab2" data-toggle="tab">Tab 2</a></li>
       <li class="tab-selector"><a href="#tab3" data-toggle="tab">Tab 3</a></li>
       <li class="tab-selector"><a href="#tab4" data-toggle="tab">Tab 4</a></li>
       <li class="tab-selector"><a href="#tab5" data-toggle="tab">Tab 5</a></li>
    </ul>
    <select class="mb10 form-control visible-xs" id="tab_selector">
        <option value="0">Tab 1</option>
        <option value="1">Tab 2</option>
        <option value="2">Tab 3</option>
        <option value="3">Tab 4</option>
        <option value="4">Tab 5</option>
    </select>
    <div class="tab-content">
       <div class="tab-pane active" id="tab1">
            Content tab 1
       </div>
       <div class="tab-pane" id="tab2">
            Content tab 2
       </div>
       <div class="tab-pane" id="tab3">
            Content tab 3
       </div>
       <div class="tab-pane" id="tab4">
            Content tab 4
       </div>
       <div class="tab-pane" id="tab5">
            Content tab 5
       </div>
    </div>
</div>

Place this javascript snippet in the document ready function" and put it at the end of </body> tag and well in the </body> tag and NOT outside of it. Of course you must have jQuery up and running.

$(document).ready(function() {
    $('#tab_selector').on('change', function (e) {
        $('.form-tabs li a').eq($(this).val()).tab('show');
    });
});

A working demo can be found here: https://jsfiddle.net/fr0w59n9/

like image 29
Franco Avatar answered Sep 21 '22 10:09

Franco