Creating A Vertical Tabs System with jQuery and CSS3

Creating A Vertical Tabs System with jQuery and CSS3
File Size: 2.97 KB KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

A simple tabs system with vertical tabbed navigation, using CSS3 transitions and a little bit of jQuery script to toggle CSS classes.

How to use it:

1. Create tabs and tabbed content following the html structure like so:

<div class="tabs">
  <nav>
    <a>Tab #1</a>
    <a>Tab #2</a>
    <a>Tab #3</a>
  </nav>
  <div class="content">
    <p>Content #1</p>
  </div>
  <div class="content">
    <p>Content #2</p>
  </div>
  <div class="content">
    <p>Content #3</p>
  </div>
</div>

2. The primary CSS / CSS3 styles for the tabs system.

.tabs {
  margin: 0px 20px;
  position: relative;
  background: #EFF1E4;
  box-shadow: 0px 0px 40px rgba(0, 0, 0, 0.2);
  width: 100%;
}

.tabs nav {
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;
  background: #AD9897;
  color: #6C5D5D;
  text-shadow: 1px 1px 1px rgba(255, 255, 255, 0.2);
  width: 150px;
}

.tabs nav a {
  padding: 20px 0px;
  text-align: center;
  width: 100%;
  cursor: pointer;
}

.tabs nav a:hover,
.tabs nav a.selected {
  background: #6C5D5D;
  color: #AD9897;
  text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
}

.tabs .content {
  padding: 20px 0px;
  position: absolute;
  top: 0px;
  left: 150px;
  color: #6C5D5D;
  width: 0px;
  height: 100%;
  overflow: hidden;
  opacity: 0;
  transition: opacity 0.1s linear 0s;
}

.tabs .content.visible {
  padding: 20px;
  width: calc(100% - 150px);
  overflow: scroll;
  opacity: 1;
}

.tabs .content p { padding-bottom: 2px; }

.tabs .content p:last-of-type { padding-bottom: 0px; }

3. Include the jQuery JavaScript library at the bottom of the web page.

<script src="//code.jquery.com/jquery-2.1.4.min.js"></script> 

4. The core JavaScript to toggle CSS class as you switch between tabs.

$(function() {
  $('.tabs nav a').on('click', function() {
    show_content($(this).index());
  });
  
  show_content(0);

  function show_content(index) {
    // Make the content visible
    $('.tabs .content.visible').removeClass('visible');
    $('.tabs .content:nth-of-type(' + (index + 1) + ')').addClass('visible');

    // Set the tab to selected
    $('.tabs nav a.selected').removeClass('selected');
    $('.tabs nav a:nth-of-type(' + (index + 1) + ')').addClass('selected');
  }
});

This awesome jQuery plugin is developed by kevin11. For more Advanced Usages, please check the demo page or visit the official website.