HTML
There is going to be two unordered lists. One for the tab links, and the other for their content.
- <div id="tabs">
- <ul id="tab-links">
- <li id="tab-link-1" class="selected">Tab 1</li>
- <li id="tab-link-2">Tab 2</li>
- <li id="tab-link-3">Tab 3</li>
- </ul>
- <ul id="tab-content">
- <li id="tab-content-1">content for tab-1 here</li>
- <li id="tab-content-2">tab-2 here</li>
- <li id="tab-content-3">third and last one, tab-3</li>
- </ul>
- </div>
CSS
First of all, we define the width of our container and clear the floats.
- #tabs {
- width: 300px;
- }
- #tabs ul {
- clear: both;
- }
Now we turn our first unordered list into a horizontal menu.
- #tab-links {
- list-style: none;
- padding: 0px;
- margin: 0px;
- }
- #tab-links li {
- float: left;
- padding: 10px;
- color: #3b5d14;
- background-color: #b2d281;
- border-right: 1px solid #fff;
- font-size: 18px;
- }
- #tab-links li.selected,
- #tab-content {
- color: #fff;
- background-color: #90b557;
- }
Since we want our content to be available in tabs, it is necessary to hide all the <li> elements except for the first one.
- #tab-content {
- padding: 5px;
- margin: 0px;
- list-style: none;
- font-size: 12px;
- }
- #tab-content-2,
- #tab-content-3 {
- display: none;
- }
JavaScript (jQuery)
Make sure that you add jQuery library to your document first. And then add this few lines of javascript inside your HEAD tag.
- $(document).ready(function() {
- // tab 1
- $('#tab-link-1').click(function() {
- $('#tab-content-1').show();
- $('#tab-content-2, #tab-content-3').hide();
- $(this).attr({class : "selected"});
- $('#tab-link-2, #tab-link-3').attr({class : ""});
- });
- // tab 2
- $('#tab-link-2').click(function() {
- $('#tab-content-2').show();
- $('#tab-content-1, #tab-content-3').hide();
- $(this).attr({class : "selected"});
- $('#tab-link-1, #tab-link-3').attr({class : ""});
- });
- // tab 3
- $('#tab-link-3').click(function() {
- $('#tab-content-3').show();
- $('#tab-content-1, #tab-content-2').hide();
- $(this).attr({class : "selected"});
- $('#tab-link-1, #tab-link-2').attr({class : ""});
- });
- });













