<main>
<h2>
<button aria-expanded="false">
Section 1
<svg aria-hidden="true" focusable="false" viewBox="0 0 10 10">
<rect class="vert" height="8" width="2" y="1" x="4"/>
<rect height="2" width="8" y="4" x="1"/>
</svg>
</button>
</h2>
<div hidden>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque commodo purus quis mi cursus hendrerit eu eu metus. Aliquam aliquam arcu eget aliquet scelerisque. Pellentesque sodales turpis vitae venenatis vehicula. Ut id porta velit. Ut eu dignissim dui, quis gravida est. Cras quis venenatis mauris, a bibendum enim. Sed at augue libero. Nullam tortor metus, tincidunt ut urna id, posuere placerat orci. Ut quis risus dictum risus facilisis imperdiet quis sed eros.</p>
</div>
<h2>
<button aria-expanded="false">
Section 2
<svg aria-hidden="true" focusable="false" viewBox="0 0 10 10">
<rect class="vert" height="8" width="2" y="1" x="4"/>
<rect height="2" width="8" y="4" x="1"/>
</svg>
</button>
</h2>
<div hidden>
<p>Nullam tortor metus, tincidunt ut urna id, posuere placerat orci. Ut quis risus dictum risus facilisis imperdiet quis sed eros. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque commodo purus quis mi cursus hendrerit eu eu metus. Aliquam aliquam arcu eget aliquet scelerisque. Pellentesque sodales turpis vitae venenatis vehicula. Ut id porta velit. Ut eu dignissim dui, quis gravida est. Cras quis venenatis mauris, a bibendum enim. Sed at augue libero.</p>
</div>
</main>
body {
max-width: 40rem;
margin: 0 auto;
padding: 1em;
}
main {
border-width: 2px 0;
border-style: solid;
}
main h2 {
margin: 0;
}
main > div + h2 {
border-top: 2px solid;
}
h2 button {
all: inherit;
border: 0;
display: flex;
justify-content: space-between;
width: 100%;
padding: 0.5em 0;
}
h2 button:focus svg {
outline: 2px solid;
}
button svg {
height: 1em;
margin-left: 0.5em;
}
[aria-expanded="true"] .vert {
display: none;
}
[aria-expanded] rect {
fill: currentColor;
}
/* page styles */
html {
font-family: Arial, sans-serif;
}
* {
box-sizing: border-box;
}
(function() {
const headings = document.querySelectorAll('h2');
Array.prototype.forEach.call(headings, h => {
let btn = h.querySelector('button');
let target = h.nextElementSibling;
btn.onclick = () => {
let expanded = btn.getAttribute('aria-expanded') === 'true';
btn.setAttribute('aria-expanded', !expanded);
target.hidden = expanded;
}
});
})()
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.