Skip to content Skip to sidebar Skip to footer

How To Make Ul Tabs With Only Html Css

Trying to figure out how to do this. I have the style but I'd like something to happen after I click the tabs. I would like the div with the tab class names to show and hide when i

Solution 1:

Standard answer: you can't. There is no way to do this with purely HTML/CSS2, unfortunately. We can make drop-downs in CSS with the :hover psuedo-class, but there's no equivalent for clicks. Look into one of these Javascript-based solutions.

Secret answer: CSS3 [kind of] supports this. But you have to create radio buttons [weird], and it's not supported in IE7/8. If you dare...

And if you don't mind using Javascript, here's a quick solution. Reformatted your HTML, first of all. No need to put <h2>s in <div>s, and use <br /> for breaks—that's what it's there for. Also, I changed the tab <div>s to use id's instead of classes. If you have unique identifiers for an element, use id.

<ulclass="tabs"><li><ahref="#tab1">Description</a></li><li><ahref="#tab2">Specs</a></li></ul><divclass="pane"><divid="tab1"><h2>Hello</h2><p>Hello hello hello.</p><p>Goodbye goodbye, goodbye</p></div><divid="tab2"style="display:none;"><h2>Hello2</h2><p>Hello2 hello2 hello2.</p><p>Goodbye2 goodbye2, goodbye2</p></div></div><divclass="content">This should really appear on a new line.</div>

Didn't touch your CSS.

For Javascript, I recommend using jQuery. It really simplifies things. All you need are these lines of code:

$(document).ready(function() {
    $("ul.tabs a").click(function() {
        $(".pane div").hide();
        $($(this).attr("href")).show();
    });
})

Basically, once the page is ready [has loaded], look for every link that's a child of a tabs ul. Attach a function that runs each time this link is clicked. When said link is clicked, hide all the tabs in the .pane div. Then, use the link's href to find the proper tab div and show it.

fiddle: http://jsfiddle.net/uFALn/18/

Solution 2:

Because of the floated <li> elements your <ul> element is zero height.

Try adding ul { overflow: auto; } and div.content { clear: both; } to your CSS

Post a Comment for "How To Make Ul Tabs With Only Html Css"