Skip to content

Instantly share code, notes, and snippets.

@mqxu
Created March 14, 2021 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mqxu/f078ffe41c6c9d911b466667bcab5c08 to your computer and use it in GitHub Desktop.
Save mqxu/f078ffe41c6c9d911b466667bcab5c08 to your computer and use it in GitHub Desktop.
Pure CSS Accordion
<h1>Pure CSS Accordion <sup>2.0</sup></h1>
<div class="row">
<div class="col">
<h2>Open <b>multiple</b></h2>
<div class="tabs">
<div class="tab">
<input type="checkbox" id="chck1">
<label class="tab-label" for="chck1">Item 1</label>
<div class="tab-content">
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Ipsum, reiciendis!
</div>
</div>
<div class="tab">
<input type="checkbox" id="chck2">
<label class="tab-label" for="chck2">Item 2</label>
<div class="tab-content">
Lorem ipsum dolor sit amet consectetur adipisicing elit. A, in!
</div>
</div>
</div>
</div>
<div class="col">
<h2>Open <b>one</b></h2>
<div class="tabs">
<div class="tab">
<input type="radio" id="rd1" name="rd">
<label class="tab-label" for="rd1">Item 1</label>
<div class="tab-content">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Eos, facilis.
</div>
</div>
<div class="tab">
<input type="radio" id="rd2" name="rd">
<label class="tab-label" for="rd2">Item 2</label>
<div class="tab-content">
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nihil, aut.
</div>
</div>
<div class="tab">
<input type="radio" id="rd3" name="rd">
<label for="rd3" class="tab-close">Close others &times;</label>
</div>
</div>
</div>
</div>

Pure CSS Accordion

Version 2.0:

  • Redesigned with SCSS and smooth animation.
  • Added a tab "close" in "open one" option to close other tab.

Acordeon made with just CSS. Based on checkbox input+label trick to active tabs.

A Pen by Raúl Barrera on CodePen.

License.

$midnight: #2c3e50;
$clouds: #ecf0f1;
// General
body {
color: $midnight;
background: $clouds;
padding: 0 1em 1em;
}
h1 {
margin: 0;
line-height: 2;
text-align: center;
}
h2 {
margin: 0 0 .5em;
font-weight: normal;
}
input {
position: absolute;
opacity: 0;
z-index: -1;
}
// Layout
.row {
display:flex;
.col {
flex:1;
&:last-child {
margin-left: 1em;
}
}
}
/* Accordion styles */
.tabs {
border-radius: 8px;
overflow: hidden;
box-shadow: 0 4px 4px -2px rgba(0,0,0,0.5);
}
.tab {
width: 100%;
color: white;
overflow: hidden;
&-label {
display: flex;
justify-content: space-between;
padding: 1em;
background: $midnight;
font-weight: bold;
cursor: pointer;
/* Icon */
&:hover {
background: darken($midnight, 10%);
}
&::after {
content: "\276F";
width: 1em;
height: 1em;
text-align: center;
transition: all .35s;
}
}
&-content {
max-height: 0;
padding: 0 1em;
color: $midnight;
background: white;
transition: all .35s;
}
&-close {
display: flex;
justify-content: flex-end;
padding: 1em;
font-size: 0.75em;
background: $midnight;
cursor: pointer;
&:hover {
background: darken($midnight, 10%);
}
}
}
// :checked
input:checked {
+ .tab-label {
background: darken($midnight, 10%);
&::after {
transform: rotate(90deg);
}
}
~ .tab-content {
max-height: 100vh;
padding: 1em;
}
}
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,300,700" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment