Using JQuery tabs in an HTML 5 page

In this post I will show you how to create a simple tabbed interface using JQuery,HTML 5 and CSS.

Make sure you have downloaded the latest version of JQuery (minified version) from http://jquery.com/download.

Please find here all my posts regarding JQuery.Also have a look at my posts regarding HTML 5.

In order to be absolutely clear this is not (and could not be) a detailed tutorial on HTML 5. There are other great resources for that.Navigate to the excellent interactive tutorials of W3School.

Another excellent resource is HTML 5 Doctor.

Two very nice sites that show you what features and specifications are implemented by various browsers and their versions are http://caniuse.com/ and http://html5test.com/. At this times Chrome seems to support most of HTML 5 specifications.Another excellent way to find out if the browser supports HTML 5 and CSS 3 features is to use the Javascript lightweight library Modernizr.

In this hands-on example I will be using Expression Web 4.0.This application is not a free application. You can use any HTML editor you like.You can use Visual Studio 2012 Express edition. You can download it here. 

Let me move on to the actual example.

This is the sample HTML 5 page

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Liverpool Legends</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
    <link rel="stylesheet" type="text/css" href="style.css">
    <script type="text/javascript" src="jquery-1.8.2.min.js"> </script>
     <script type="text/javascript" src="tabs.js"></script>
    
  </head>
  <body>
    <header>
        <h1>Liverpool Legends</h1>
    </header>
 
    <section id="tabs">
        <ul>
            <li><a href="http://weblogs.asp.net/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts§ionid=1153&postid=9143136#first-tab">Defenders</a></li>
            <li><a href="http://weblogs.asp.net/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts§ionid=1153&postid=9143136#second-tab">Midfielders</a></li>
            <li><a href="http://weblogs.asp.net/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts§ionid=1153&postid=9143136#third-tab">Strikers</a></li>
        </ul>
   <div id="first-tab">
     <h3>Liverpool Defenders</h3>
     <p> The best defenders that played for Liverpool are Jamie Carragher, Sami Hyypia , Ron Yeats and Alan Hansen.</p>
   </div>
   <div id="second-tab">
     <h3>Liverpool Midfielders</h3>
     <p> The best midfielders that played for Liverpool are Kenny Dalglish, John Barnes,Ian Callaghan,Steven Gerrard and Jan Molby.
 
       </p>
   </div>
   <div id="third-tab">
     <h3>Liverpool Strikers</h3>
     <p>The best strikers that played for Liverpool are Ian Rush,Roger Hunt,Robbie Fowler and Fernando Torres.<br/>
      </p>
   </div>

 </div>

</section>
   
   
    <footer>
   
    <p>All Rights Reserved</p>
 
    </footer>
  
  </body>
 
</html> 

This is very simple HTML markup. 

I have styled this markup using CSS.

The contents of the style.css file follow


* {
    margin: 0;
    padding: 0;
}

header

{

font-family:Tahoma;
font-size:1.3em;
color:#505050;
text-align:center;
}


#tabs {
    font-size: 0.9em;
    margin: 20px 0;
}
#tabs ul {
    float: left;
    background: #777;
    width: 260px;
    padding-top: 24px;

}
#tabs li {
    margin-left: 8px;
    list-style: none;
}
* html #tabs li {
    display: inline;
}
#tabs li, #tabs li a {
    float: left;
}
#tabs ul li.active {
    border-top:2px red solid;
    background: #15ADFF;
}
#tabs ul li.active a {
    color: #333333;
}
#tabs div {
    background: #15ADFF;
    clear: both;
    padding: 15px;
    min-height: 200px;
}
#tabs div h3 {
    margin-bottom: 12px;
}
#tabs div p {
    line-height: 26px;
}
#tabs ul li a {
    text-decoration: none;
    padding: 8px;
    color:#0b2f20;
    font-weight: bold;
}

footer
{
background-color:#999;
width:100%;
text-align:center;
font-size:1.1em;
color:#002233;
}

There are some CSS rules that style the various elements in the HTML 5 file. These are straight-forward rules.

The JQuery code lives inside the tabs.js file

$(document).ready(function(){
$('#tabs div').hide();
$('#tabs div:first').show();
$('#tabs ul li:first').addClass('active');
 
$('#tabs ul li a').click(function(){
$('#tabs ul li').removeClass('active');
$(this).parent().addClass('active');
var currentTab = $(this).attr('href');
$('#tabs div').hide();
$(currentTab).show();
return false;
});
});
 

I am using some of the most commonly used JQuery functions like hide , show, addclass , removeClass

I hide and show the tabs when the tab becomes the active tab.

When I view my page I get the following result

 

Hope it helps!!!!!

1 Comment

Comments have been disabled for this content.