<ul id="accordion" class="accordion">
<li class="accordion__item">
<span class="accordion__header">Question 1?</span>
<div class="accordion__panel">
<h3>Question 1?</h3>
<ul class="accordion__body">
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ligula elit, congue vel nulla ut, ullamcorper ullamcorper eros. Proin molestie viverra odio vitae euismod. In a diam quam. Pellentesque sit amet urna et libero sollicitudin interdum. Duis et tellus diam. Mauris tempus nec tortor maximus fringilla. Aenean ultricies quam id nunc posuere, ac iaculis dui tristique. Sed sit amet sem sed nulla sollicitudin vulputate tristique sit amet odio. Ut tempus, nulla eu tempor tempus, felis leo ultrices felis, vitae elementum ligula dui eu nisl. Aliquam dignissim mi orci, eu congue ipsum sollicitudin ut.
</li>
</ul>
</div>
</li>
<li class="accordion__item">
<span class="accordion__header">Question 2?</span>
<div class="accordion__panel">
<h3>Question 2?</h3>
<ul class="accordion__body">
<li>Sed sed dolor accumsan, imperdiet elit in, egestas nisl. Curabitur faucibus purus felis, eu posuere libero rutrum vel. Phasellus eget mauris lacinia, commodo diam eu, fermentum nunc. Suspendisse potenti. Sed orci erat, tempor ac neque hendrerit, rhoncus sodales ante. Donec maximus iaculis vestibulum. In dictum consectetur ligula, in placerat sem varius in. Quisque ac justo felis. Aliquam semper rutrum auctor. Nunc a ipsum fermentum, molestie libero non, porta urna.
</li>
</ul>
</div>
</li>
<li class="accordion__item">
<span class="accordion__header">Question 3?</span>
<div class="accordion__panel">
<h3>Question 3?</h3>
<ul class="accordion__body">
<li>Nunc a urna libero. Morbi sit amet neque et purus viverra viverra vel sit amet nisl. Quisque eu orci nisl. Nunc varius dolor erat. Integer tellus eros, vestibulum in lobortis id, cursus feugiat ex. Suspendisse dapibus, metus at suscipit hendrerit, justo quam vestibulum est, vel tristique odio massa nec dui. Nunc convallis placerat laoreet. Maecenas nunc enim, pretium at elit eget, interdum fringilla purus. Pellentesque molestie libero vitae justo lacinia, at ornare lectus volutpat. Pellentesque ac posuere dui. Suspendisse aliquet lobortis viverra. In tincidunt vulputate nisl, et hendrerit dui fermentum non. Maecenas venenatis pellentesque arcu non maximus.
</li>
</ul>
</div>
</li>
</ul>
.accordion {
list-style: none;
margin: 1rem;
padding: 0;
&__item {
color: #6505cc;
background: #eedbff;
border: 1px solid;
border-bottom: 0;
&:last-child {
border-bottom: 1px solid;
}
}
&__header {
display: block;
position: relative;
padding: 0.5rem 1rem;
text-transform: uppercase;
font-weight: bold;
&::after {
content: '';
position: absolute;
right: 1rem;
width: 0px;
height: 0px;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 15px solid #6505cc;
}
}
&__panel {
padding: 1rem;
border-top: 1px solid;
h3 {
margin-top: 0;
}
}
&__body {
list-style: none;
padding: 0;
}
}
.visually-hidden {
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
clip: rect(1px, 1px, 1px, 1px);
white-space: nowrap; /* added line */
}
View Compiled
const accordion = document.getElementById('accordion')
function toggleAccordion(e) {
const pressButtonKeyCode = e.code === 'Enter' || e.code === 'Space' ? true: false;
if (e.type === 'click' || pressButtonKeyCode) {
this.nextElementSibling.classList.toggle('visually-hidden');
}
}
if (accordion) {
const headers = document.querySelectorAll('.accordion__header')
headers.forEach(header => {
header.tabIndex = 0
header.addEventListener('click', toggleAccordion)
header.addEventListener('keypress', toggleAccordion)
})
const panels = document.querySelectorAll('.accordion__panel')
panels.forEach(panel => panel.classList.add('visually-hidden'))
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.