Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section>
	<h1>Accessible Multi-select Accordion</h1>
	<dl class="accordion">
		<dt>
			<button class="accordion__button" aria-expanded="true" aria-controls="accordion-resources">What resources did you use to build this?</button>
		</dt>
		<dd class="accordion__content" id="accordion-resources" aria-hidden="false">
			<ul>
				<li><a href="https://www.w3.org/TR/wai-aria-practices/#disclosure">Disclosure (FAQ) Design Pattern</a></li>
				<li><a href="https://www.w3.org/TR/wai-aria-practices/examples/disclosure/disclosure-faq.html">Disclosure (FAQ) Example</a></li>
				<li><a href="https://www.w3.org/TR/wai-aria-practices/#accordion">Accordion Design Pattern</a></li>
				<li><a href="https://www.w3.org/TR/wai-aria-practices/examples/accordion/accordion.html">Accordion Example</a></li>
			</ul>
		</dd>

		<dt>
			<button class="accordion__button" aria-expanded="true" aria-controls="accordion-markup">What are the HTML tags and ARIA attributes used?</button>
		</dt>
		<dd class="accordion__content" id="accordion-markup" aria-hidden="false">
			<ul>
				<li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section">section</a></li>
				<li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button">button</a></li>
				<li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dl">dl</a></li>
				<li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dt">dt</a></li>
				<li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dd">dd</a></li>
				<li><a href="https://www.w3.org/TR/wai-aria-1.1/#aria-expanded">aria-expanded</a></li>
				<li><a href="https://www.w3.org/TR/wai-aria-1.1/#aria-controls">aria-controls</a></li>
			</ul>
		</dd>

		<dt>
			<button class="accordion__button" aria-expanded="true" aria-controls="accordion-behavior">What is the expected behavior of this component?</button>
		</dt>
		<dd class="accordion__content" id="accordion-behavior" aria-hidden="false">
			<ul>
				<li>Users can tab to an accordion button which will show focus that button</li>
				<li>When using a keyboard and the NVDA screen reader users can hit Enter or Space to toggle the content for the focused accordion button</li>
				<li>When using the VO screen reader users can navigate to the button with Control + Option + Space</li>
				<li>Then when focused on the button, VO users can toggle the content with Control + Option + Space</li>
			</ul>
		</dd>
	</dl>
</section>
              
            
!

CSS

              
                $theme-primary: #9482ec;
$theme-secondary: #444;

*:focus {
	outline: none;
	box-shadow: 0 0 0 0.125rem #fff, 0 0 0 0.25rem #000;
}

body {
	padding: 1rem 2rem;
	font-family: 'Roboto', sans-serif;
	background-color: #fff;
}

h1 {
	text-align: center;
	margin-bottom: 1.5rem;
	font-family: 'Poppins', sans-serif;
	color: $theme-secondary;
	font-size: 2rem;
}

li {
	margin-bottom: 0.75rem;
}

a {
	padding: 0.25rem 0.125rem 0;
	color: $theme-primary;
	text-decoration: none;
	border-bottom: 1px solid $theme-primary;
	transition: all 150ms ease;

	&:focus,
	&:hover {
		background-color: #cbffd9;
		color: #6a51e7;
		border-bottom: 3px solid #6a51e7;
	}
}

.accordion {
	max-width: 40rem;
	margin: auto;

	&__button {
		text-align: left;
		font-size: 1rem;
		font-family: 'Poppins', sans-serif;
		font-weight: 500;
		background-color: $theme-primary;
		color: #fff;
		border: none;
		padding: 0.5rem 1rem;
		border-radius: 0.25rem;
		display: block;
		width: 100%;
		z-index: 1; // add z-index so focus is always visible
		position: relative;
		margin-top: 1rem;
		transition: all 150ms linear;
		
		&::after {
			content: "";
			display: inline-block;
			position: absolute;
			right: 0.875rem;
			top: 0.875rem;
			width: 0.4rem;
			height: 0.4rem;
			border-right: 0.25rem solid #fff;
			border-top: 0.25rem solid #fff;
			transform: rotate(135deg);
			transition: transform 250ms linear;
		}
		
		&:hover {
			background-color: $theme-secondary;
			transform: scale(1.025);
		}
		
		&[aria-expanded='true'] {
			background-color: $theme-secondary;
		}
		
		&[aria-expanded='true']::after {
			transform: rotate(315deg) translateY(0.25rem);
		}
	}
	
	&__content {
		padding: 0.875rem;
		margin-left: 0;
		background-color: #fff;
		border: 2px solid #eee;
		box-shadow: 1px 4px 10px 1px rgba(0, 0, 0, 0.15);
		transition: all 200ms linear;
		
		&[aria-hidden='false'] {
			display: block;
		}
		
		&[aria-hidden='true'] {
			display: none;
		}
	}
}
              
            
!

JS

              
                const buttons = document.querySelectorAll('.accordion__button');
const contentSections = document.querySelectorAll('.accordion__content');

function setInitialAriaValue(elements, aria, ariaValue) {
	elements.forEach((element) => {
		element.setAttribute(`${aria}`, `${ariaValue}`);
	});
}

function accordionToggleHandler() {
	buttons.forEach((button) => {
		button.addEventListener('click', toggleContent);
	});
}

function toggleContent(e) {
	toggleButton(e.target);
	toggleDetails(e.target);
}

function toggleButton(button) {
	const expandedValue = button.getAttribute('aria-expanded');
	const setValue = expandedValue === 'true' ? 'false' : 'true';
	button.setAttribute('aria-expanded', setValue);
}

function toggleDetails(button) {
	const buttonAriaControl = button.getAttribute('aria-controls');
	const content = document.getElementById(buttonAriaControl);
  const hiddenValue = content.getAttribute('aria-hidden');
	const setValue = hiddenValue === 'true' ? 'false' : 'true';
	content.setAttribute('aria-hidden', setValue);
}

setInitialAriaValue(buttons, 'aria-expanded', 'false');
setInitialAriaValue(contentSections, 'aria-hidden', 'true');
accordionToggleHandler();
              
            
!
999px

Console