Skip to content

Instantly share code, notes, and snippets.

@feo52
Last active July 8, 2017 15:20
Show Gist options
  • Save feo52/27cfe41305e20f7c62c2 to your computer and use it in GitHub Desktop.
Save feo52/27cfe41305e20f7c62c2 to your computer and use it in GitHub Desktop.
Pure CSS Tab
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pure CSS Tab with :checked</title>
<link rel="stylesheet" href="style.css">
<style>
body{ margin:0; padding:0; }
.tab-caption:nth-of-type(1) { background-color: #FFAAAA; }
.tab-caption:nth-of-type(2) { background-color: #AAFFAA; }
.tab-caption:nth-of-type(3) { background-color: #AAAAFF; }
.tab-content:nth-of-type(1) { background-color: #FFAAAA; }
.tab-content:nth-of-type(2) { background-color: #AAFFAA; }
.tab-content:nth-of-type(3) { background-color: #AAAAFF; }
</style>
</head>
<body>
<input class="tab-status" id ="tabSwitch01" type="radio" name="tab" checked="checked">
<input class="tab-status" id ="tabSwitch02" type="radio" name="tab">
<input class="tab-status" id ="tabSwitch03" type="radio" name="tab">
<div class="tab-caption-container">
<label class="tab-caption" for="tabSwitch01">tab1</label>
<label class="tab-caption" for="tabSwitch02">tab2</label>
<label class="tab-caption" for="tabSwitch03">tab3</label>
</div>
<div class="tab-content-container">
<div class="tab-content">tab1-data<br></div>
<div class="tab-content">tab2-data<br>tab2-data<br></div>
<div class="tab-content">tab3-data<br>tab3-data<br>tab3-data<br></div>
</div>
</body>
</html>
/* ----------------------------------------------------------------------------
Pure CSS Tab
------------------------------------------------------------------------- */
.tab-status { display: none; }
.tab-status ~ .tab-caption-container { display: flex; }
.tab-status ~ .tab-caption-container > .tab-caption { opacity: 0.4; cursor: pointer; }
.tab-status:nth-of-type(1):checked ~ .tab-caption-container > .tab-caption:nth-of-type(1) { opacity: 1; }
.tab-status:nth-of-type(2):checked ~ .tab-caption-container > .tab-caption:nth-of-type(2) { opacity: 1; }
.tab-status:nth-of-type(3):checked ~ .tab-caption-container > .tab-caption:nth-of-type(3) { opacity: 1; }
.tab-status ~ .tab-content-container > .tab-content { display: none; }
.tab-status:nth-of-type(1):checked ~ .tab-content-container > .tab-content:nth-of-type(1) { display: block; }
.tab-status:nth-of-type(2):checked ~ .tab-content-container > .tab-content:nth-of-type(2) { display: block; }
.tab-status:nth-of-type(3):checked ~ .tab-content-container > .tab-content:nth-of-type(3) { display: block; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment