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 click the tabs. I'm assuming how that would work. Right now when I click the tabs nothing happens.
Here's my HTML
<style type="text/css">
ul.tabs {
display: table;
list-style-type: none;
margin: 0;
padding: 0;
}
ul.tabs>li {
float: left;
padding: 10px;
background-color: lightgray;
}
ul.tabs>li:hover {
background-color: lightgray;
}
ul.tabs>li.selected {
background-color: lightgray;
}
div.content {
border: 1px solid black;
}
ul { overflow: auto; }
div.content { clear: both; }
</style>
<body>
<ul class="tabs">
<li><a href="#tab1">Description</a></li>
<li><a href="#tab2">Specs</a></li>
</ul>
<div class="pane">
<div class="tab1">
<div><h2>Hello</h2></div>
<div />
<div>Hello hello hello.</div>
<div />
<div>Goodbye goodbye, goodbye</div>
<div />
<div />
</div>
<div class="tab2" style="display:none;">
<div><h2>Hello2</h2></div>
<div />
<div>Hello2 hello2 hello2.</div>
<div />
<div>Goodbye2 goodbye2, goodbye2</div>
<div />
</div>
</div>
<div class="content">
This should really appear on a new line.
</div>
</body>
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, useid
.Didn't touch your CSS.
For Javascript, I recommend using jQuery. It really simplifies things. All you need are these lines of code:
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'shref
to find the proper tab div and show it.fiddle: http://jsfiddle.net/uFALn/18/
Thanks benesch. It helped me too.
One can also add
return false
to prevent that jerky jump to the anchor. For instance:Because of the floated
<li>
elements your<ul>
element is zero height.Try adding
ul { overflow: auto; }
anddiv.content { clear: both; }
to your CSS