Custom tab menu using simple JavaScript & CSS
October 15, 2016 | 4404 Views

Custom tab menu using simple JavaScript & CSS

Learn One of the simplest way of doing the tab navigation menu for your next project using simple JavaScript code and CSS

Common Issues

Adding the tab menu for your website may be a prolonged process, because you need to search and download the plugin's for the tab menu and integrate them into your website.

The plugin's for tab menu or some other ways of doing the tab menu may contains lengthy JavaScript code. In this tab menu tutorial the JavaScript code is simplified to achieve the same.

Creating The Structure - Using Html

The HTML code to achieve the basic structure of the tab menu is shown below (example).

 <div class="tab-container">
  <div class="tab-menu">
     <ul>
        <li> <a href="#" class="tab-a active-a" data-id="tab1">TAB-1 </a> </li>
         <li> <a href="#" class="tab-a" data-id="tab2">TAB-2 </a> </li>
         <li> <a href="#" class="tab-a" data-id="tab3">TAB-3 </a> </li>
     </ul>
  </div>
  <!--end of tab-menu-->
    <div  class="tab tab-active" data-id="tab1">
      <h2>Heading of tab one. </h2>
      <p>Content of the tab one goes here </p>         
    </div>
    <!--end of tab one--> 
    <div  class="tab " data-id="tab2">
      <h2>Heading of tab two. </h2>
      <p>Content of the tab two goes here </p>           
    </div>
    <!--end of tab two--> 
    <div  class="tab " data-id="tab3">
      <h2>Heading of tab three. </h2>
      <p>Content of the tab three goes here </p>         
    </div>
    <!--end of tab three--> 
 </div>
 <!--end of container-->

Add Styling - Using Css

The following are the Css to achieve the styles for the tab menu. Here the .tab-a is the tab button and the .tab is the container of the all tabs in your design. both the .tab-a and the .tab have one more secret class to make the tab and tab menu active and inactive.

body{
  margin: 0;
  padding: 0;
  height: 100%;
  background-color: #c7eae0;
  font-family: 'sans-serif', sans-serif;
}
.tab-container{
  margin: 5% 10%;
  background-color: #c1e3d9;
  padding: 3%;
  border-radius: 4px;
}
.tab-menu{}
.tab-menu ul{
  margin: 0;
  padding: 0;
}
.tab-menu ul li{
  list-style-type: none;
  display: inline-block;
}
.tab-menu ul li a{
  text-decoration: none;
  color: rgba(0,0,0,0.4);
  background-color: #b4cbc4;
  padding: 7px 25px;
  border-radius: 4px;
}
.tab-menu ul li a.active-a{
  background-color: #588d7d;
  color: #ffffff;
}
.tab{
  display: none;
}
.tab h2{
  color: rgba(0,0,0,.7);
}
.tab p{
  color: rgba(0,0,0,0.6);
  text-align: justify;
}
.tab-active{
  display: block;
}

Handling The Event - Using JavaScript

The following are the JavaScript code to achieve the tab navigation menu for your website.

$(document).ready(function(){ 
  /*creating click event for each tab menu*/
  $(document.body).on("click",".tab-a",function(){        
    $(".tab").removeClass('tab-active');
    /*adding the active class for the current selected tab*/
     $(".tab[data-id='"+$(this).attr('data-id')+"']").addClass("tab-active");
     $(".tab-a").removeClass('active-a');
     /*adding the active class for the current selected tab menu*/
     $(this).parent().find(".tab-a").addClass('active-a');
  });
});

Live demo

See the Pen Custom TAB using simple JavaScript by themeswild (@themeswild) on CodePen.





Free html templates

Download from a vast library of Open Source HTML & CSS templates and use them for free. No licenses required. Apart from that, do you need to build a whole website from scratch? Write to us anytime and we will get it sorted.

Contact Us

More Links