Skip to main content

Creating Multiple Tabs in HTML


HTML Document is here:-

<!DOCTYPE html>
<html>
    <head>
        <title>Tabs</title>
        <link rel=alternate href="hello.html" hreflang=en type=text/html title="English HTML">
        <link type="text/css" href="tabs.css" rel="stylesheet" />
        <meta charset="UTF-8"/>
    </head>
    <body style="margin: 0;">
    <center>
        <script src="tabs.js"></script>
        <ul id='tabs'>
            <li><a href="#one">Tab 1</a></li>
            <li><a href="#two">Tab 2</a></li>
            <li><a href="#three">Tab 3</a></li>
            <li><a href="#four">Tab 4</a></li>
            <li><a href="#five">Tab 5</a></li>
        </ul>
        <div id='one' class="tabContent">
            <p>Tab One</p>
        </div>
        <div id='two' class="tabContent">
            <p>Tab Two</p>
        </div>
        <div id='three' class="tabContent">
            <p>Tab Three</p>
        </div>
        <div id='four' class="tabContent">
            <p>Tab Four</p>
        </div>
        <div id='five' class="tabContent">
            <p>Tab Five</p>
        </div>
    </center>
</body>
</html>

 Here is the JavaScript

var tabLinks=new Array();
var contentDivs=new Array();
onload=function init(){
    var tabListItems=document.getElementById('tabs').childNodes;
    for(var i=0; i<tabListItems.length; i++){
        if(tabListItems[i].nodeName=="LI"){
            var tabLink=getFirstChildWithTagName(tabListItems[i], "A");
            var id=getHash(tabLink.getAttribute('href'));
            tabLinks[id]=tabLink;
            contentDivs[id]=document.getElementById(id);
        }
    }
    var i=0;
    for(var id in tabLinks){
        tabLinks[id].onclick=showTab;
        tabLinks[id].onfocus=function(){
            this.blur();
        };
        if(i==0)
            tabLinks[id].className='selected';
        i++;
    }
    var i=0;
    for(var id in contentDivs){
        if(i!=0)
            contentDivs[id].className="tabContent hide";
        i++;
    }
}
function showTab(){
    var selectedId=getHash(this.getAttribute('href'));
    for(var id in contentDivs){
        if(id==selectedId){
            tabLinks[id].className='selected';
            contentDivs[id].className='tabContent';
        } else{
            tabLinks[id].className='';
            contentDivs[id].className='tabContent hide';
        }
    }
    return false;
}
function getFirstChildWithTagName(element, tagName){
    for(var i=0; i<element.childNodes.length; i++){
        if(element.childNodes[i].nodeName==tagName)
            return element.childNodes[i];
    }
    return null;
}
function getHash(url){
    var hashPos=url.lastIndexOf('#');
    return url.substring(hashPos+1);             
}

And Here is the CSS

ul#tabs{
    font-family: Lucida Sans Unicode, Lucida Grande, sans-serif;
    font-size: 1.2em;
    list-style-type: none;
    margin: 0 0 0 0;
    padding: 0;
    width: 750px;
}

ul#tabs li{
    float: left;
    padding: 0;
    margin: 0 2px 0 0;
}

ul#tabs li a{
    color: white;
    margin: 0;
    padding: 5px 20px;
    background-color: #3b5998;
    border-top: 6px #3b5998 solid;
    font-weight: bolder;
    display: block;
}

ul#tabs li a:hover{
    color: white;
    background-color: #294a8f;
    border-top: 6px #294a8f solid;
    text-decoration: none;
}

ul#tabs li a.selected{
    color: white;
    background-color: #294a8f;
    border-top: #294a8f solid;
    border-bottom: 3px #294a8f solid;
    text-decoration: none;
}

div.tabContent{
    width: 710px;
    clear: both;
    border: 10px #294a8f solid;
    padding: 10px;
    margin: 0;
}

div.tabContent.hide{
    display: none;
}

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Java Program to calculate the Strike Rate of a Cricket Batsman

import java.io.*; import java.util.*; public class StrikeRate{     Scanner scan=new Scanner(System.in);     int ballsFaced, runs;     double strikeRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Faced: ");             ballsFaced=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0); ...

WP similar or related posts widget without using any plugin

Hi guys, Recently I was working on some WP website and my client told me that he required a widget for displaying related/similar posts on the single post page. But as his website was already using many plugins, even for some pretty small tasks like this one, I decided not to use another WP plugin (plugins are not good for your WP websites, we will discuss about that on some other post.) I am not explaining the code as it is pretty simple if you are familiar with WP classes. But please let me know if you have any questions related to the PHP code posted below in the comments section or even much better, on Gist. You can add the following code directly in your child theme's functions.php file or you can create a separate file and include this at the bottom of functions.php file. <?php class similar_posts_widget extends WP_Widget {     function __construct()     {         parent::__construct('similar_posts_...