<h1>Pure CSS3 Tabs using :target</h1>
<div id="tabs">
<span id="tab-1" class="tab-switch"></span>
<a href="#tab-1" class="tab-link">Tab 1</a>
<div class="tab-content">
<h2>Lorem ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias amet commodi cupiditate molestias laboriosam sit! Delectus magni esse magnam libero laboriosam aspernatur voluptates, natus aliquid sed doloremque architecto provident, impedit saepe. Mollitia eum qui, quae laboriosam, magni aut similique asperiores error dicta totam ad, deserunt nemo repudiandae maxime, expedita. Id, numquam, laboriosam. Voluptatibus, saepe! Eligendi assumenda ullam modi explicabo voluptas qui veniam repellat ipsam similique dolore, mollitia quam dolores quae cumque neque officia. Laudantium, ratione sint quo molestiae sapiente dolores, doloremque modi illo quia tempora asperiores ullam perspiciatis fuga quasi maxime nesciunt ab suscipit harum quam. Eligendi dicta suscipit dolorum.</p>
</div>
<span id="tab-2" class="tab-switch"></span>
<a href="#tab-2" class="tab-link">Tab 2</a>
<div class="tab-content">
<h2>Lorem list</h2>
<ul>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li>Aliquam tincidunt mauris eu risus.</li>
<li>Vestibulum auctor dapibus neque.</li>
</ul>
</div>
<span id="tab-3" class="tab-switch"></span>
<a href="#tab-3" class="tab-link">Tab 3</a>
<div class="tab-content">
<h2>Yolo</h2>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div>
</div>
<footer>Supported since IE9. Enjoy!</footer>
/* MAGIC ... IMPORTANT! */
#tabs::after {
display: block;
content: "";
clear: both;
}
.tab-switch {
// prevents scrolling to #target
position: fixed;
&:target {
+ .tab-link {
+ .tab-content {
display: block;
}
}
}
}
.tab-link {
display: inline-block;
}
.tab-content {
box-sizing: border-box;
float: right;
display: none;
width: 100%;
}
/* FANCY STYLES */
body {
font-family: sans-serif;
padding: 10px 100px;
}
h1 {
text-align: center;
}
footer {
margin-top: 20px;
text-align: center;
}
#tabs {
background-color: #1caa5d;
border: 1px solid #1caa5d;
font-size: 0;
}
.tab-link {
margin-left: 20px;
margin-top: 10px;
padding: 20px;
text-decoration: none;
font-size: 18px;
color: white;
&:hover {
background-color: white;
color: black;
}
~ .tab-link {
margin-left: 0;
}
}
.tab-switch {
&:target {
+ .tab-link {
background-color: white;
color: black;
}
}
}
.tab-content {
background-color: white;
padding: 35px;
font-size: 13px;
p {
line-height: 1.65;
}
ul, ol {
padding: 0 0 0 15px;
margin: 0;
line-height: 1.25;
> li {
margin: 5px 0;
}
}
}
View Compiled
/*
NO JS REQUIRED!!!
... well, you could need JS for selecting first tab
... or include its hash in the URL
here ... simple JS to select 1.tab if there is nothing in the URL ... include it before </body>
*/
(function() {
if (!!location.hash) return;
var link = document.querySelector('#tabs > .tab-link');
if (link) link.click();
})();
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.