Skip to content

Instantly share code, notes, and snippets.



Last active Sep 3, 2020
What would you like to do?
Pure CSS Tabs without javascript and anchor support
<!-- Features:
* Tabs with anchor (browser history support)
* Tab anchor can be used to scroll or not
* Color selected tab menu
* Select the first tab by default (and also color its tab menu)
* Uses a simple unordered list
Helpful links:
<a href="#cat">Show cat</a>
<a href="#dog">Show dog</a>
<a href="#cow">Show cow</a>
<article id="tabs">
<!-- NOTE: This list of IDs MUST be placed BEFORE the tab-sections!
The order here is important too!
The ids and anchors MUST be the first element RIGHT AFTER then "tabs" id.-->
<div id="show-cat" class="tab-id"></div>
<div id="show-dog" class="tab-id"></div>
<div id="show-cow" class="tab-id"></div>
<div id="cat" class="tab-anchor"></div>
<div id="dog" class="tab-anchor"></div>
<div id="cow" class="tab-anchor"></div>
<ul class="tab-menu">
<li class="tab-item"><a href="#show-cat">Cat</a></li>
<li class="tab-item"><a href="#show-dog">Dog</a></li>
<li class="tab-item"><a href="#show-cow">Cow</a></li>
<div class="tab-folder">
<p class="tab-content">This content appears on tab 1.</p>
<p class="tab-content">This content appears on tab 2.</p>
<p class="tab-content">This content appears on tab 3.</p>
#tabs {
// NOTE: If you set the div elements to display none,
// the browser will not jump to that anchor after clicking a tab.
.tab-id {
display: none;
// Alternative, used when scrolling to tab is desired
.tab-anchor {
scroll-margin-top: 7rem;
scroll-snap-margin-top: 7rem; /* Safari and IOS */
// By default, all tab contents should be hidden
.tab-folder>.tab-content {
display: none;
// If no tab is selected, chose the first tab
.tab-id:nth-child(1):not(:target) + .tab-id:nth-child(2):not(:target) + .tab-id:nth-child(3):not(:target) + .tab-anchor:nth-child(3n + 1):not(:target) + .tab-anchor:nth-child(3n + 2):not(:target) + .tab-anchor:nth-child(3n + 3):not(:target) ~ .tab-folder>.tab-content:nth-child(1) {
display: block;
// Show tab content, whenever its corresponding ID is referenced.
.tab-id:nth-child(1):target ~ .tab-folder>.tab-content:nth-child(1),
.tab-anchor:nth-child(3n + 1):target ~ .tab-folder>.tab-content:nth-child(1) {
.tab-id:nth-child(2):target ~ .tab-folder>.tab-content:nth-child(2),
.tab-anchor:nth-child(3n + 2):target ~ .tab-folder>.tab-content:nth-child(2) {
.tab-id:nth-child(3):target ~ .tab-folder>.tab-content:nth-child(3),
.tab-anchor:nth-child(3n + 3):target ~ .tab-folder>.tab-content:nth-child(3) {
// NOTE: This can be merged with the entry below, style the default tab always the same. But you can (optionally) style the unselected tab menu differently.
.tab-id:nth-child(1):not(:target) + .tab-id:nth-child(2):not(:target) + .tab-id:nth-child(3):not(:target) + .tab-anchor:nth-child(3n + 1):not(:target) + .tab-anchor:nth-child(3n + 2):not(:target) + .tab-anchor:nth-child(3n + 3):not(:target) ~ .tab-menu>.tab-item:nth-child(1) {
a {
// Color the tab, that is currently referenced to.
.tab-id:nth-child(1):target ~ .tab-menu>.tab-item:nth-child(1),
.tab-anchor:nth-child(3n + 1):target ~ .tab-menu>.tab-item:nth-child(1) {
a {
.tab-id:nth-child(2):target ~ .tab-menu>.tab-item:nth-child(2),
.tab-anchor:nth-child(3n + 2):target ~ .tab-menu>.tab-item:nth-child(2) {
a {
.tab-id:nth-child(3):target ~ .tab-menu>.tab-item:nth-child(3),
.tab-anchor:nth-child(3n + 3):target ~ .tab-menu>.tab-item:nth-child(3) {
a {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.