0

I was just wondering about the best way to define a CSS rule for tabs. There are many methods online but I wanted to achieve it in the most efficient way. How do you go about doing this? My code is here: http://jsfiddle.net/mgd5P/

HTML

<ul id="tabs"> <li><a href="#" title="tab1">TTS</a></li> <li><a href="#" title="tab2">PMW</a></li> </ul> <div id="content"> <div id="tab1">One - content</div> <div id="tab2">Two - content</div> </div>​ 

JS

$(document).ready(function() { $("#content div").hide(); // Initially hide all content $("#tabs li:first").attr("id","current"); // Activate first tab $("#content div:first").fadeIn(); // Show first tab content $('#tabs a').click(function(e) { e.preventDefault(); $("#content div").hide(); //Hide all content $("#tabs li").attr("id",""); //Reset id's $(this).parent().attr("id","current"); // Activate this $('#' + $(this).attr('title')).fadeIn(); // Show content for current tab }); })();​ 
4
  • Your fiddle was not properly showing tabs because it does not include the jquery ui which is separate from the standard jquery framework. When you include the ui, it will bring its own styling depending on which theme you picked. If you need to override the standard jquery styling, use firebug to see what classes are applied and remove or override them Commented Dec 1, 2012 at 12:25
  • @WebChemist I don't want to use jQuery UI I want to style my own from scratch Commented Dec 1, 2012 at 12:34
  • 1
    So you want to reinvent the wheel? Why do that when the ui package will handle the js functionality of making the tabs actually work, then you just override the ui theme css as needed. Commented Dec 1, 2012 at 12:38
  • 1
    Yes, @WebChemist is right. No need to work hard on something which is already working. You just need to modify the jquery ui css as per your requirements. jsfiddle.net/dZySA That's it ;) Commented Dec 1, 2012 at 12:42

1 Answer 1

3

Here is a pure CSS approach to making tabs (learnt this at csstricks)

  1. Use radio buttons as your tab menu
  2. Use the :checked hook to style the selected tab's content as displayed
  3. Hide the actual radio buttons and style their labels as the tabs. Clicks on the label affect the control specified in the for attribute, so clicking the tabs still affects the radio buttons.

Here is a demonstration: http://jsfiddle.net/abwyU/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.