JSFiddle - React, Tailwind, and code Playground
HTML
<div class="accordion">
<h1>This is the header</h1>
<div class="accordion-content">
<p>This is the content of this accordion</p>
</div>
</div>
CSS
h1
{
padding: 0.01em;
margin: 0;
}
h1, p
{
color: white;
}
.accordion
{
border-radius: 0.25em;
background: rgba(0, 0, 0, 0.6);
display: block;
}
.accordion > h1:after
{
content: "\25BC";
display: inline-block;
padding-left: 0.5em;
transition: transform 0.25s;
}
.accordion.closed > h1:after
{
transform: rotate(-90deg);
}
.accordion .accordion-content
{
background: rgba(255,255,255, 0.3);
height: auto;
max-height: 200px;
transition: max-height 0.5s;
overflow: hidden;
border-radius: 0 0 0.25em 0.25em;
}
.accordion.closed .accordion-content
{
max-height: 0;
}
JavaScript
var accordions = document.querySelectorAll('.accordion');
for(let accordion of accordions)
{
accordion.addEventListener( 'click' , (target => e => {
target.classList.toggle('closed');
})(accordion));
}