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

Should javascript be enabled or disabled?

Javascript Javascript is the most popular client-end scripting language used by every website.  Javascript provides a way to create custom tools for the websites which are used to improve the overall site quality and UI (User Interface). It's also popular as it allow the audio and video streaming too. Google used the javascript to load the web page contents dynamically i.e., instead of redirecting the user to a new page or refreshing the same page, the contents are updated on the required parts on the web page. This dynamic content loading approach is known as AJAX which has now become much more popular as almost every website is using this, at least, to validate the form. Javascript vs No-Javascript Some people often disable the javascript from their browser settings. If you are one of them and think that javascript is a security threat, then, you are completely wrong. Javascript is a way to improve the website quality according to the client's machine and navigato...

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_...

Java Program to print first n Natural numbers in reverse order

import java.io.*; import java.util.*; public class NaturalRev extends Natural{     Scanner scan=new Scanner(System.in);     int n;     public void input()throws IOException{         System.out.println("Enter limit: ");         n=scan.nextInt();     }     public void compute(){         if(n>1)             System.out.println("First "+n+" natural numbers in reverse order are: ");         else             System.out.println("First "+n+" natural number in reverse order is: ");         for(int i=n; i>=1; i--)             System.out.println(i);     }   ...