CSS, Responsive | Type: JavascriptResponsive Tabs to Accordian

At chosen break point, tabs become an accordian.

HTML
<ul class="tabs">
    <li class="active">Tab 1</li>
    <li>Tab 2</li>
    <li>Tab 3</li>
</ul>

<div class="tab_container">
    <h3 class="d_active tab_drawer_heading">Tab 1</h3>
    <div id="tab1" class="tab_content">
        <h2>Tab 1 content</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac metus augue.</p>
    </div> <!-- #tab1 -->

    <h3 class="tab_drawer_heading">Tab 2</h3>
    <div id="tab2" class="tab_content">
        <h2>Tab 2 content</h2>
        <p>Nunc dui velit, scelerisque eu placerat volutpat, dapibus eu nisi. Vivamus eleifend vestibulum odio non vulputate.</p>
    </div>< <!-- #tab2 -->

    <h3 class="tab_drawer_heading">Tab 3</h3>
    <div id="tab3" class="tab_content">
        <h2>Tab 3 content</h2>
        <p>Nulla eleifend felis vitae velit tristique imperdiet. Etiam nec imperdiet elit. Pellentesque sem lorem, scelerisque sed facilisis sed, vestibulum sit amet eros.</p>
    </div> <!-- #tab3 -->
</div> <!-- .tab_container -->

CSS
ul.tabs {
    margin: 0;
    padding: 0;
    float: left;
    list-style: none;
    height: 32px;
    border-bottom: 1px solid #333;
    width: 100%;
}

ul.tabs li {
    float: left;
    margin: 0;
    cursor: pointer;
    padding: 0;
    width:32%;
    text-align:center;
    margin-right:2%;
    height: 31px;
    line-height: 31px;
    border-radius:6px 6px 0 0;
    background-color: #666;
    color: #ccc;
    overflow: hidden;
    position: relative;
}

.tab_last { margin-right:0!important; float:right!important;}

ul.tabs li:hover {
    background-color: #ccc;
    color: #333;
}

ul.tabs li.active {
    background-color: #ddd;
    color: #333;
    border-bottom: 1px solid #fff;
    display: block;
}

.tab_container {
    border: 1px solid #333;
    border-top: none;
    clear: both;
    float: left;
    width: 100%;
    background: #fff;
    overflow: auto;
}

.tab_content {
    padding: 20px;
    display: none;
}

.tab_drawer_heading { display: none; }

@media screen and (max-width: 480px) {
    .tabs {
        display: none;
    }
    .tab_drawer_heading {
        display: block;
        background-color: #ccc;
        color: #fff;
        border-top: 1px solid #333;
        margin: 0;
        padding: 5px 20px;
        cursor: pointer;
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
     }
     .d_active {
        background-color: #666;
        color: #fff;
     }
}

JAVASCRIPT
// tabbed content
// http://www.entheosweb.com/tutorials/css/tabs.asp
$(".tab_content").hide();
$(".tab_content:first").show();

/* if in tab mode */
$("ul.tabs li").click(function() {
    $(".tab_content").hide();
    var activeTab = $(this).attr("rel");
    $("#"+activeTab).fadeIn();
    $("ul.tabs li").removeClass("active");
    $(this).addClass("active");
    $(".tab_drawer_heading").removeClass("d_active");
    $(".tab_drawer_heading[rel^='"+activeTab+"']").addClass("d_active");
});

/* if in drawer mode */
$(".tab_drawer_heading").click(function() {
    $(".tab_content").hide();
    var d_activeTab = $(this).attr("rel");
    $("#"+d_activeTab).fadeIn();
    $(".tab_drawer_heading").removeClass("d_active");
    $(this).addClass("d_active");
    $("ul.tabs li").removeClass("active");
    $("ul.tabs li[rel^='"+d_activeTab+"']").addClass("active");
});

/* Extra class "tab_last" to add border to right side of last tab */
    $('ul.tabs li').last().addClass("tab_last");

Reference Links

We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept