Custom tab menu using simple JavaScript & CSS
October 15, 2016 | 3068 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>Ut cursus nisi luctus sem sollicitudin porta. </h2>
      <p>Nullam urna mi, euismod nec lacus eget, viverra dictum augue. Donec ut tellus risus. Vivamus dignissim, augue vitae fermentum finibus, ipsum mi efficitur sapien, id feugiat nulla felis id massa. Morbi cursus euismod turpis pretium convallis. Praesent laoreet nunc in sem convallis volutpat. </p>         
    </div>
    <!--end of tab one--> 
    <div  class="tab " data-id="tab2">
      <h2>Euismod nec lacus eget, viverra dictum augue. </h2>
      <p>Maecenas vitae ornare est. Praesent suscipit tellus ante, sed elementum enim commodo a. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent id ex sollicitudin. </p>           
    </div>
    <!--end of tab two--> 
    <div  class="tab " data-id="tab3">
      <h2>Eed elementum enim commodo a </h2>
      <p>Vel dolor eu efficitur. Ut convallis sit amet diam nec vulputate. Aenean elementum ac magna vitae eleifend. Vestibulum felis est, dictum sed lacus vitae, egestas fermentum nisl. Praesent commodo massa sit amet ante dignissim pulvinar. Nulla semper orci arcu, id venenatis leo posuere ultricies. </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');
  });
});




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