UPDATE: In 2020, this method is still effective for linking tabs; however, if you need multiple tab sets on one page, consider using @chulian's approach with input[type=radio]
instead of :target
.
You can find an archive of the defunct html5rockstars.com demo here:
https://web.archive.org/web/20121118201357/http://htmlrockstars.com/blog/using-css-to-create-a-tabbed-content-area-no-js-required/
A more comprehensive explanation of the same technique can be found here:
http://www.sitepoint.com/css3-tabs-using-target-selector/
The key concept is utilizing the CSS3 :target
selector to display the currently selected tab. While this works well for a single set of tabs on a page and fully supports browser back buttons, it requires specific conditions to function correctly. Here's an example structure:
<ul id="menu">
<li><a href="#tab1">First tab</a></li>
<li><a href="#tab2">Second tab</a></li>
<li><a href="#tab3">Third tab</a></li>
</ul>
<div id="tab1" class="tab-content">Content of first tab</div>
<div id="tab2" class="tab-content">Content of second tab</div>
<div id="tab3" class="tab-content">Content of third tab</div>
In your stylesheet, define the following rules:
.tab-content {
display: none;
}
.tab-content:target {
display: block;
}
However, keep in mind that no content will show until a tab link is clicked unless explicitly directed to a specific tab (e.g., page.html#tab1
). The suggested workaround by the second link involves the following adjustments:
.tab-content {
z-index: 0;
background-color: white;
position: absolute;
top: 100px;
width: 100%;
height: 300px;
}
.tab-content:first-child {
z-index: 1;
}
.tab-content:target {
z-index: 2;
}
This solution may seem unconventional as it relies on absolute positioning.
Alternatively, if having the default tab placed last in the HTML structure isn't an issue, you can opt for this layout:
<ul id="menu">
<li><a href="#tab1">First tab</a></li>
<li><a href="#tab2">Second tab</a></li>
<li><a href="#tab3">Third tab</a></li>
</ul>
<div class="tab-folder">
<div id="tab2" class="tab-content">Content of second tab</div>
<div id="tab3" class="tab-content">Content of third tab</div>
<div id="tab1" class="tab-content">Content of first tab</div>
</div>
CSS styling for this structure would be:
.tab-folder > .tab-content:target ~ .tab-content:last-child, .tab-folder > .tab-content {
display: none;
}
.tab-folder > :last-child, .tab-folder > .tab-content:target {
display: block;
}
Considered by many as the most elegant solution, especially when accessibility and CSS support are crucial factors to consider.