Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap responsive layout in mobile convert to tabs

So as the title suggests, I am currently using Bootstrap. I am working on a responsive layout that in desktop size, is just a normal columned floated layout. Then in mobile device or tablet size, it adds bootstrap tabs. I am not sure quite how to solve this as the bootstrap 3 col-xs floats don't seem to be playing nice with the tabs plugin. I have made a diagram to help explain.

Layout Diagram

Fiddle I've been working with jsfiddle

<div class="container">   
<div class="content">
    <ul class="nav nav-tabs visible-phone">
        <li class="active"><a href="#t1" data-toggle="tab">T1</a></li>
        <li><a href="#t2" data-toggle="tab">T2</a></li>
        <li><a href="#t3" data-toggle="tab">T3</a></li>
    </ul>

    <div class="row tab-content">
        <div class="col-sm-4 tab-pane active" id="enter">
            <div class="inner-right-border">
                <h2>T1 CONTENT</h2>
                <hr />
            </div>
        </div>
        <div class="col-sm-8">
            <div class="tab-pane" id="t2">
                <h2>T2 CONTENT</h2><hr />
            </div>
            <div class="tab-pane" id="t3">
                <h2>T3 CONTENT</h2><hr />
            </div>
        </div>
    </div>
</div>                

Thanks in advance for any help!

like image 227
Jim Avatar asked May 09 '14 03:05

Jim


People also ask

How do I toggle tabs in Bootstrap?

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 .

Is Bootstrap mobile responsive?

Bootstrap includes a responsive, mobile first fluid grid system that appropriately scales up to 12 columns as the device or viewport size increases. It includes predefined classes for easy layout options, as well as powerful mixins for generating more semantic layouts.

How do I make the first tab active in Bootstrap?

You can activate a tab or pill navigation without writing any JavaScript code — simply specify the data-bs-toggle="tab" on each tab, or data-bs-toggle="pill" on each pill, as well as create a . tab-pane with unique ID for every tab and wrap them in . tab-content .


3 Answers

Needed something similar

HTML:

<div class="container">
    <!-- Nav tabs -->
    <ul class="nav nav-tabs device-small" role="tablist">
        <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
        <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
        <li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content">
        <div role="tabpanel" class="tab-pane active col col-xs-12 col-sm-4" id="home">...home...</div>
        <div role="tabpanel" class="tab-pane col-xs-12 col-sm-4" id="profile">...profile...</div>
        <div role="tabpanel" class="tab-pane col-xs-12 col-sm-4" id="messages">...messages...</div>
    </div>

</div>

CSS:

/* small device */
@media (max-width: 767px) {
    .device-big {
        display: none;
    }
    .device-small {
        display: block;
    }
}
/* big device */
@media only screen and (min-width : 768px) {
    .device-big, .tab-content > .tab-pane {
        display: block;
    }
    .device-small {
        display: none;
    }
    .tab-content > .tab-pane {
        display: block;
    }
}

Here is working example: http://jsfiddle.net/bomff3g6/

like image 120
Atul Yadav Avatar answered Oct 13 '22 18:10

Atul Yadav


The CSS code above works, but has an issue with nested tabs. e.g from the initial - future tab one then contains 2 tabs.

I wanted to post this as a comment to the Atul's response, but don't have enough "reputation" points.

This is a slight tweak to allow nested tabs.

@media (max-width: 767px) {
  .device-big {
    display: none;
  }
  .device-small {
    display: block;
  }
}
/* big device */
@media only screen and (min-width : 768px) {
  .tab-content > .tab-pane.device-big {
    display: block;
  }
  .device-small {
    display: none;
  }
}
like image 35
Matt Garner Avatar answered Oct 13 '22 19:10

Matt Garner


Working jsfiddle: https://jsfiddle.net/41s795qn/

A updated bootstrap v4 version from Atul Yadav's answer:

<div class="container-fluid">
    <!-- Nav tabs -->
    <ul class="nav nav-tabs hidden-md-up" role="tablist">
        <li class="nav-item"><a class="nav-link active" href="#home" aria-controls="home" data-toggle="tab">Home</a></li>
        <li class="nav-item"><a class="nav-link" href="#profile" aria-controls="profile" data-toggle="tab">Profile</a></li>
        <li class="nav-item"><a class="nav-link" href="#messages" aria-controls="messages" data-toggle="tab">Messages</a></li>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content row">
        <div role="tabpanel" class="tab-pane active col col-12 col-md-4" id="home">...home...</div>
        <div role="tabpanel" class="tab-pane col-12 col-md-4" id="profile">...profile...</div>
        <div role="tabpanel" class="tab-pane col-12 col-md-4" id="messages">...messages...</div>
    </div>
</div>

CSS:

/* big device */
@media(min-width : 768px) {
    .tab-content > .tab-pane {
        display: block;
    }
}
like image 1
benck Avatar answered Oct 13 '22 20:10

benck