Tailwind CSS and Alpine JS Accordion
Accordions allow you to organize a stack of items that can be expanded or collapsed, revealing or hiding additional content. Accordion examples use the Alpine JS's collapse plugin, which provides a smooth collapse/expand animation. If you prefer not to use this plugin, you can remove the "x-collapse" directive from each item, but you will lose this animation.
This component requires Alpine JS v3 to function properly. Some advanced features may require additional Alpine plugins (such as focus).
Tell Me MoreDefault Accordion
Default or always open accordion is an example of an accordion where each item is independent from the others. This means any number of accordion items can be open or closed at the same time.
<div class="">
<div x-data="{ isExpanded: false }" class="">
<button id="controlsAccordionItemOne" type="button" class="" aria-controls="accordionItemOne" @click="isExpanded = ! isExpanded" :class="isExpanded ? 'text-onSurfaceStrong dark:text-onSurfaceDarkStrong font-bold' : 'text-onSurface dark:text-onSurfaceDark font-medium'" :aria-expanded="isExpanded ? 'true' : 'false'">
What browsers are supported?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke="currentColor" class="size-5 shrink-0 transition" aria-hidden="true" :class="isExpanded ? 'rotate-180' : ''">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<div x-cloak x-show="isExpanded" id="accordionItemOne" role="region" aria-labelledby="controlsAccordionItemOne" x-collapse>
<div class="">
Our website is optimized for the latest versions of Chrome, Firefox, Safari, and Edge. Check our <a href="#" class="">documentation</a> for additional information.
</div>
</div>
</div>
<div x-data="{ isExpanded: false }" class="">
<button id="controlsAccordionItemTwo" type="button" class="" aria-controls="accordionItemTwo" @click="isExpanded = ! isExpanded" :class="isExpanded ? 'text-onSurfaceStrong dark:text-onSurfaceDarkStrong font-bold' : 'text-onSurface dark:text-onSurfaceDark font-medium'" :aria-expanded="isExpanded ? 'true' : 'false'">
How can I contact customer support?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke="currentColor" class="size-5 shrink-0 transition" aria-hidden="true" :class="isExpanded ? 'rotate-180' : ''">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<div x-cloak x-show="isExpanded" id="accordionItemTwo" role="region" aria-labelledby="controlsAccordionItemTwo" x-collapse>
<div class="">
Reach out to our dedicated support team via email at <a href="#" class="">support@example.com</a> or call our toll-free number at 1-800-123-4567 during business hours.
</div>
</div>
</div>
<div x-data="{ isExpanded: false }" class="">
<button id="controlsAccordionItemThree" type="button" class="" aria-controls="accordionItemThree" @click="isExpanded = ! isExpanded" :class="isExpanded ? 'text-onSurfaceStrong dark:text-onSurfaceDarkStrong font-bold' : 'text-onSurface dark:text-onSurfaceDark font-medium'" :aria-expanded="isExpanded ? 'true' : 'false'">
What is the refund policy?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke="currentColor" class="size-5 shrink-0 transition" aria-hidden="true" :class="isExpanded ? 'rotate-180' : ''">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<div x-cloak x-show="isExpanded" id="accordionItemThree" role="region" aria-labelledby="controlsAccordionItemThree" x-collapse>
<div class="">
Please refer to our <a href="#" class="">refund policy page</a> on the website for detailed information regarding eligibility, timeframes, and the process for requesting a refund.
</div>
</div>
</div>
</div>
Single Open Accordion
Single or exclusive accordion is an example of an accordion where only one item can be open at a time. Opening a new item will close the previously opened item.
<div x-data="{ selectedAccordionItem: 'one' }" class="">
<div class="">
<button id="controlsAccordionItemOne" type="button" class="" aria-controls="accordionItemOne" @click="selectedAccordionItem = 'one'" :class="selectedAccordionItem === 'one' ? 'text-onSurfaceStrong dark:text-onSurfaceDarkStrong font-bold' : 'text-onSurface dark:text-onSurfaceDark font-medium'" :aria-expanded="selectedAccordionItem === 'one' ? 'true' : 'false'">
What browsers are supported?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke="currentColor" class="size-5 shrink-0 transition" aria-hidden="true" :class="selectedAccordionItem === 'one' ? 'rotate-180' : ''">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<div x-cloak x-show="selectedAccordionItem === 'one'" id="accordionItemOne" role="region" aria-labelledby="controlsAccordionItemOne" x-collapse>
<div class="">
Our website is optimized for the latest versions of Chrome, Firefox, Safari, and Edge. Check our <a href="#" class="">documentation</a> for additional information.
</div>
</div>
</div>
<div class="">
<button id="controlsAccordionItemTwo" type="button" class="" aria-controls="accordionItemTwo" @click="selectedAccordionItem = 'two'" :class="selectedAccordionItem === 'two' ? 'text-onSurfaceStrong dark:text-onSurfaceDarkStrong font-bold' : 'text-onSurface dark:text-onSurfaceDark font-medium'" :aria-expanded="selectedAccordionItem === 'two' ? 'true' : 'false'">
How can I contact customer support?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke="currentColor" class="size-5 shrink-0 transition" aria-hidden="true" :class="selectedAccordionItem === 'two' ? 'rotate-180' : ''">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<div x-cloak x-show="selectedAccordionItem === 'two'" id="accordionItemTwo" role="region" aria-labelledby="controlsAccordionItemTwo" x-collapse>
<div class="">
Reach out to our dedicated support team via email at <a href="#" class="">support@example.com</a> or call our toll-free number at 1-800-123-4567 during business hours.
</div>
</div>
</div>
<div class="">
<button id="controlsAccordionItemThree" type="button" class="" aria-controls="accordionItemThree" @click="selectedAccordionItem = 'three'" :class="selectedAccordionItem === 'three' ? 'text-onSurfaceStrong dark:text-onSurfaceDarkStrong font-bold' : 'text-onSurface dark:text-onSurfaceDark font-medium'" :aria-expanded="selectedAccordionItem === 'three' ? 'true' : 'false'">
What is the refund policy?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke="currentColor" class="size-5 shrink-0 transition" aria-hidden="true" :class="selectedAccordionItem === 'three' ? 'rotate-180' : ''">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<div x-cloak x-show="selectedAccordionItem === 'three'" id="accordionItemThree" role="region" aria-labelledby="controlsAccordionItemThree" x-collapse>
<div class="">
Please refer to our <a href="#" class="">refund policy page</a> on the website for detailed information regarding eligibility, timeframes, and the process for requesting a refund.
</div>
</div>
</div>
</div>
Split Accordion
An accordion that presents content in a more organized and divided manner, featuring multiple collapsible sections that you can expand or collapse independently.
<div class="">
<div x-data="{ isExpanded: false }" class="">
<button id="controlsAccordionItemOne" type="button" class="" aria-controls="accordionItemOne" @click="isExpanded = ! isExpanded" :class="isExpanded ? 'text-onSurfaceStrong dark:text-onSurfaceDarkStrong font-bold' : 'text-onSurface dark:text-onSurfaceDark font-medium'" :aria-expanded="isExpanded ? 'true' : 'false'">
What browsers are supported?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke="currentColor" class="size-5 shrink-0 transition" aria-hidden="true" :class="isExpanded ? 'rotate-180' : ''">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<div x-cloak x-show="isExpanded" id="accordionItemOne" role="region" aria-labelledby="controlsAccordionItemOne" x-collapse>
<div class="">
Our website is optimized for the latest versions of Chrome, Firefox, Safari, and Edge. Check our <a href="#" class="">documentation</a> for additional information.
</div>
</div>
</div>
<div x-data="{ isExpanded: false }" class="">
<button id="controlsAccordionItemTwo" type="button" class="" aria-controls="accordionItemTwo" @click="isExpanded = ! isExpanded" :class="isExpanded ? 'text-onSurfaceStrong dark:text-onSurfaceDarkStrong font-bold' : 'text-onSurface dark:text-onSurfaceDark font-medium'" :aria-expanded="isExpanded ? 'true' : 'false'">
How can I contact customer support?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke="currentColor" class="size-5 shrink-0 transition" aria-hidden="true" :class="isExpanded ? 'rotate-180' : ''">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<div x-cloak x-show="isExpanded" id="accordionItemTwo" role="region" aria-labelledby="controlsAccordionItemTwo" x-collapse>
<div class="">
Reach out to our dedicated support team via email at <a href="#" class="">support@example.com</a> or call our toll-free number at 1-800-123-4567 during business hours.
</div>
</div>
</div>
<div x-data="{ isExpanded: false }" class="">
<button id="controlsAccordionItemThree" type="button" class="" aria-controls="accordionItemThree" @click="isExpanded = ! isExpanded" :class="isExpanded ? 'text-onSurfaceStrong dark:text-onSurfaceDarkStrong font-bold' : 'text-onSurface dark:text-onSurfaceDark font-medium'" :aria-expanded="isExpanded ? 'true' : 'false'">
What is the refund policy?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke="currentColor" class="size-5 shrink-0 transition" aria-hidden="true" :class="isExpanded ? 'rotate-180' : ''">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<div x-cloak x-show="isExpanded" id="accordionItemThree" role="region" aria-labelledby="controlsAccordionItemThree" x-collapse>
<div class="">
Please refer to our <a href="#" class="">refund policy page</a> on the website for detailed information regarding eligibility, timeframes, and the process for requesting a refund.
</div>
</div>
</div>
</div>
Accordion with No Background
Also known as a flush accordion, this is an example of an accordion with a minimalist design that seamlessly integrates into the overall aesthetics of your webpage, creating a clean and user-friendly interface.
<div class="">
<div x-data="{ isExpanded: false }">
<button id="controlsAccordionItemOne" type="button" class="" aria-controls="accordionItemOne" @click="isExpanded = ! isExpanded" :class="isExpanded ? 'text-onSurfaceStrong dark:text-onSurfaceDarkStrong font-bold' : 'text-onSurface dark:text-onSurfaceDark font-medium'" :aria-expanded="isExpanded ? 'true' : 'false'">
What browsers are supported?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke="currentColor" class="size-5 shrink-0 transition" aria-hidden="true" :class="isExpanded ? 'rotate-180' : ''">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<div x-cloak x-show="isExpanded" id="accordionItemOne" role="region" aria-labelledby="controlsAccordionItemOne" x-collapse>
<div class="">
Our website is optimized for the latest versions of Chrome, Firefox, Safari, and Edge. Check our <a href="#" class="">documentation</a> for additional information.
</div>
</div>
</div>
<div x-data="{ isExpanded: false }">
<button id="controlsAccordionItemTwo" type="button" class="" aria-controls="accordionItemTwo" @click="isExpanded = ! isExpanded" :class="isExpanded ? 'text-onSurfaceStrong dark:text-onSurfaceDarkStrong font-bold' : 'text-onSurface dark:text-onSurfaceDark font-medium'" :aria-expanded="isExpanded ? 'true' : 'false'">
How can I contact customer support?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke="currentColor" class="size-5 shrink-0 transition" aria-hidden="true" :class="isExpanded ? 'rotate-180' : ''">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<div x-cloak x-show="isExpanded" id="accordionItemTwo" role="region" aria-labelledby="controlsAccordionItemTwo" x-collapse>
<div class="">
Reach out to our dedicated support team via email at <a href="#" class="">support@example.com</a> or call our toll-free number at 1-800-123-4567 during business hours.
</div>
</div>
</div>
<div x-data="{ isExpanded: false }">
<button id="controlsAccordionItemThree" type="button" class="" aria-controls="accordionItemThree" @click="isExpanded = ! isExpanded" :class="isExpanded ? 'text-onSurfaceStrong dark:text-onSurfaceDarkStrong font-bold' : 'text-onSurface dark:text-onSurfaceDark font-medium'" :aria-expanded="isExpanded ? 'true' : 'false'">
What is the refund policy?
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-width="2" stroke="currentColor" class="size-5 shrink-0 transition" aria-hidden="true" :class="isExpanded ? 'rotate-180' : ''">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
</svg>
</button>
<div x-cloak x-show="isExpanded" id="accordionItemThree" role="region" aria-labelledby="controlsAccordionItemThree" x-collapse>
<div class="">
Please refer to our <a href="#" class="">refund policy page</a> on the website for detailed information regarding eligibility, timeframes, and the process for requesting a refund.
</div>
</div>
</div>
</div>
Data
List of all Alpine JS data used in this component.
Property | Description |
---|---|
isExpanded |
Boolean - Accordion is expanded/collapsed |
selectedAccordionItem |
String - Selected(Expanded) Accordion |
Keyboard Navigation
Key | Action |
---|---|
Tab |
Next focusable element gets the focus |
Space |
Item collapsed: item expands Item expanded: item collapses |