So I want to create a tab system using only CSS.
what I have so far works, but I don’t know how to make one tab visible by default.
The tabs:
<section class="tabs">
<ul>
<li><a href="#tab1">1</a></li>
<li><a href="#tab2">2</a></li>
<li><a href="#tab3">3</a></li>
</ul>
<section id="tab1"> content for 1... </section>
<section id="tab2"> content for 2... </section>
<section id="tab3"> content for 3... </section>
</section>
and the css (the most important part):
.tabs section{
display: none;
}
.tabs section:target{
display: block;
}
So I if I set the section:first-child to block (first tab should be visible by default), then I get two visible sections if there’s a anchor in the URL: the first tab, and the target tab…
How can I overcome this problem?
Well, if you make your default your last tab (
section:last-child), then I think this could work:Using the general sibling selector
~requires that the element precede the siblings it targets, so hence the reason for thelast-childrather thanfirst-childrequirement.EDIT: 11-12-2011, I did find a way for you to highlight your
atags as active! Assuming it meets your particular application. Here is some simple modified code for proof of concept (only tested in FF):HTML
CSS