Tailwind Accordion is a collapsible UI element built using Tailwind. It allows users to expand and collapse sections of content by clicking on headers, revealing and hiding information on demand.
Examples
Basic Usage
Use data-kt-accordion selector to enable accordion behavior where clicking a section header expands its content, while others collapse.
<div data-kt-accordion="true" class="kt-accordion"> <div data-kt-accordion-item="true" class="kt-accordion-item"> <button id="accordion_toggle_0" data-kt-accordion-toggle="true" aria-controls="accordion_content_0" class="kt-accordion-toggle" > <span class="kt-accordion-title" >How is pricing determined for each plan?</span ><span aria-hidden="true" class="kt-accordion-indicator" ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus kt-accordion-indicator-on" aria-hidden="true" > <path d="M5 12h14"></path> <path d="M12 5v14"></path></svg ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus kt-accordion-indicator-off" aria-hidden="true" > <path d="M5 12h14"></path></svg ></span> </button> <div class="kt-accordion-content hidden" aria-labelledby="accordion_toggle_0" id="accordion_content_0" > <div class="kt-accordion-wrapper"> KtUI embraces flexible licensing options that empower you to choose the perfect fit for your project's needs and budget. </div> </div> </div> <div data-kt-accordion-item="true" class="kt-accordion-item"> <button id="accordion_toggle_1" data-kt-accordion-toggle="true" aria-controls="accordion_content_1" class="kt-accordion-toggle" > <span class="kt-accordion-title" >What payment methods are accepted for subscriptions?</span ><span aria-hidden="true" class="kt-accordion-indicator" ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus kt-accordion-indicator-on" aria-hidden="true" > <path d="M5 12h14"></path> <path d="M12 5v14"></path></svg ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus kt-accordion-indicator-off" aria-hidden="true" > <path d="M5 12h14"></path></svg ></span> </button> <div class="kt-accordion-content hidden" aria-labelledby="accordion_toggle_1" id="accordion_content_1" > <div class="kt-accordion-wrapper"> KtUI embraces flexible licensing options that empower you to choose the perfect fit for your project's needs and budget. </div> </div> </div> <div data-kt-accordion-item="true" class="kt-accordion-item"> <button id="accordion_toggle_2" data-kt-accordion-toggle="true" aria-controls="accordion_content_2" class="kt-accordion-toggle" > <span class="kt-accordion-title" >Are there any hidden fees in the pricing?</span ><span aria-hidden="true" class="kt-accordion-indicator" ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus kt-accordion-indicator-on" aria-hidden="true" > <path d="M5 12h14"></path> <path d="M12 5v14"></path></svg ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus kt-accordion-indicator-off" aria-hidden="true" > <path d="M5 12h14"></path></svg ></span> </button> <div class="kt-accordion-content hidden" aria-labelledby="accordion_toggle_2" id="accordion_content_2" > <div class="kt-accordion-wrapper"> KtUI embraces flexible licensing options that empower you to choose the perfect fit for your project's needs and budget. </div> </div> </div></div>
Default Open
Use active class on an accordion to display a specific section by default.
<div data-kt-accordion="true" class="kt-accordion"> <div data-kt-accordion-item="true" class="kt-accordion-item active"> <button id="accordion_toggle_0" data-kt-accordion-toggle="true" aria-controls="accordion_content_0" class="kt-accordion-toggle" > <span class="kt-accordion-title" >How is pricing determined for each plan?</span ><span aria-hidden="true" class="kt-accordion-indicator" ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus kt-accordion-indicator-on" aria-hidden="true" > <path d="M5 12h14"></path> <path d="M12 5v14"></path></svg ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus kt-accordion-indicator-off" aria-hidden="true" > <path d="M5 12h14"></path></svg ></span> </button> <div class="kt-accordion-content" aria-labelledby="accordion_toggle_0" id="accordion_content_0" > <div class="kt-accordion-wrapper"> Reui embraces flexible licensing options that empower you to choose the perfect fit for your project's needs and budget. </div> </div> </div> <div data-kt-accordion-item="true" class="kt-accordion-item"> <button id="accordion_toggle_1" data-kt-accordion-toggle="true" aria-controls="accordion_content_1" class="kt-accordion-toggle" > <span class="kt-accordion-title" >What payment methods are accepted for subscriptions?</span ><span aria-hidden="true" class="kt-accordion-indicator" ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus kt-accordion-indicator-on" aria-hidden="true" > <path d="M5 12h14"></path> <path d="M12 5v14"></path></svg ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus kt-accordion-indicator-off" aria-hidden="true" > <path d="M5 12h14"></path></svg ></span> </button> <div class="kt-accordion-content hidden" aria-labelledby="accordion_toggle_1" id="accordion_content_1" > <div class="kt-accordion-wrapper"> Reui embraces flexible licensing options that empower you to choose the perfect fit for your project's needs and budget. </div> </div> </div> <div data-kt-accordion-item="true" class="kt-accordion-item"> <button id="accordion_toggle_2" data-kt-accordion-toggle="true" aria-controls="accordion_content_2" class="kt-accordion-toggle" > <span class="kt-accordion-title" >Are there any hidden fees in the pricing?</span ><span aria-hidden="true" class="kt-accordion-indicator" ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus kt-accordion-indicator-on" aria-hidden="true" > <path d="M5 12h14"></path> <path d="M12 5v14"></path></svg ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus kt-accordion-indicator-off" aria-hidden="true" > <path d="M5 12h14"></path></svg ></span> </button> <div class="kt-accordion-content hidden" aria-labelledby="accordion_toggle_2" id="accordion_content_2" > <div class="kt-accordion-wrapper"> Reui embraces flexible licensing options that empower you to choose the perfect fit for your project's needs and budget. </div> </div> </div></div>
Expand All
Use data-kt-accordion-expand-all="true" attribute to keep all accordions visible at all times despite toggling.
<div data-kt-accordion="true" data-kt-accordion-expand-all="true" class="kt-accordion"> <div data-kt-accordion-item="true" class="kt-accordion-item active"> <button id="accordion_toggle_0" data-kt-accordion-toggle="true" aria-controls="accordion_content_0" class="kt-accordion-toggle" > <span class="kt-accordion-title" >How is pricing determined for each plan?</span ><span aria-hidden="true" class="kt-accordion-indicator" ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus kt-accordion-indicator-on" aria-hidden="true" > <path d="M5 12h14"></path> <path d="M12 5v14"></path></svg ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus kt-accordion-indicator-off" aria-hidden="true" > <path d="M5 12h14"></path></svg ></span> </button> <div class="kt-accordion-content" aria-labelledby="accordion_toggle_0" id="accordion_content_0" > <div class="kt-accordion-wrapper"> Reui embraces flexible licensing options that empower you to choose the perfect fit for your project's needs and budget. </div> </div> </div> <div data-kt-accordion-item="true" class="kt-accordion-item"> <button id="accordion_toggle_1" data-kt-accordion-toggle="true" aria-controls="accordion_content_1" class="kt-accordion-toggle" > <span class="kt-accordion-title" >What payment methods are accepted for subscriptions?</span ><span aria-hidden="true" class="kt-accordion-indicator" ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus kt-accordion-indicator-on" aria-hidden="true" > <path d="M5 12h14"></path> <path d="M12 5v14"></path></svg ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus kt-accordion-indicator-off" aria-hidden="true" > <path d="M5 12h14"></path></svg ></span> </button> <div class="kt-accordion-content hidden" aria-labelledby="accordion_toggle_1" id="accordion_content_1" > <div class="kt-accordion-wrapper"> Reui embraces flexible licensing options that empower you to choose the perfect fit for your project's needs and budget. </div> </div> </div> <div data-kt-accordion-item="true" class="kt-accordion-item"> <button id="accordion_toggle_2" data-kt-accordion-toggle="true" aria-controls="accordion_content_2" class="kt-accordion-toggle" > <span class="kt-accordion-title" >Are there any hidden fees in the pricing?</span ><span aria-hidden="true" class="kt-accordion-indicator" ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus kt-accordion-indicator-on" aria-hidden="true" > <path d="M5 12h14"></path> <path d="M12 5v14"></path></svg ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus kt-accordion-indicator-off" aria-hidden="true" > <path d="M5 12h14"></path></svg ></span> </button> <div class="kt-accordion-content hidden" aria-labelledby="accordion_toggle_2" id="accordion_content_2" > <div class="kt-accordion-wrapper"> Reui embraces flexible licensing options that empower you to choose the perfect fit for your project's needs and budget. </div> </div> </div></div>
Outline Style
Apply kt-accordion-outline class to add custom border styles to your accordion.
<div data-kt-accordion="true" data-kt-accordion-expand-all="true" class="kt-accordion kt-accordion-outline"> <div data-kt-accordion-item="true" class="kt-accordion-item active"> <button id="accordion_toggle_0" data-kt-accordion-toggle="true" aria-controls="accordion_content_0" class="kt-accordion-toggle" > <span class="kt-accordion-title" >How is pricing determined for each plan?</span ><span aria-hidden="true" class="kt-accordion-indicator" ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus kt-accordion-indicator-on" aria-hidden="true" > <path d="M5 12h14"></path> <path d="M12 5v14"></path></svg ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus kt-accordion-indicator-off" aria-hidden="true" > <path d="M5 12h14"></path></svg ></span> </button> <div class="kt-accordion-content" aria-labelledby="accordion_toggle_0" id="accordion_content_0" > <div class="kt-accordion-wrapper"> Reui embraces flexible licensing options that empower you to choose the perfect fit for your project's needs and budget. </div> </div> </div> <div data-kt-accordion-item="true" class="kt-accordion-item"> <button id="accordion_toggle_1" data-kt-accordion-toggle="true" aria-controls="accordion_content_1" class="kt-accordion-toggle" > <span class="kt-accordion-title" >What payment methods are accepted for subscriptions?</span ><span aria-hidden="true" class="kt-accordion-indicator" ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus kt-accordion-indicator-on" aria-hidden="true" > <path d="M5 12h14"></path> <path d="M12 5v14"></path></svg ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus kt-accordion-indicator-off" aria-hidden="true" > <path d="M5 12h14"></path></svg ></span> </button> <div class="kt-accordion-content hidden" aria-labelledby="accordion_toggle_1" id="accordion_content_1" > <div class="kt-accordion-wrapper"> Reui embraces flexible licensing options that empower you to choose the perfect fit for your project's needs and budget. </div> </div> </div> <div data-kt-accordion-item="true" class="kt-accordion-item"> <button id="accordion_toggle_2" data-kt-accordion-toggle="true" aria-controls="accordion_content_2" class="kt-accordion-toggle" > <span class="kt-accordion-title" >Are there any hidden fees in the pricing?</span ><span aria-hidden="true" class="kt-accordion-indicator" ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus kt-accordion-indicator-on" aria-hidden="true" > <path d="M5 12h14"></path> <path d="M12 5v14"></path></svg ><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minus kt-accordion-indicator-off" aria-hidden="true" > <path d="M5 12h14"></path></svg ></span> </button> <div class="kt-accordion-content hidden" aria-labelledby="accordion_toggle_2" id="accordion_content_2" > <div class="kt-accordion-wrapper"> Reui embraces flexible licensing options that empower you to choose the perfect fit for your project's needs and budget. </div> </div> </div></div>
Navigation Menu
Use accordion to handle advanced navigation menus.
These data attributes allow you to set options for the accordion component during auto initialization.
Option
Type
Default
Description
data-kt-accordion-expand-all
boolean
false
Using true allows all accordion sections to remain expanded even when another is opened.
data-kt-accordion-hidden-class
string
"hidden"
Tailwind class to use for the hidden state of the accordion contents.
data-kt-accordion-active-class
string
"active"
Custom class to use for the active state of the accordion contents.
Selectors
This table details the custom classes and data attributes used by the accordion component.
Name
Description
Data Attributes
data-kt-accordion="true"
A selector used to auto-initialize the accordion object on page load.
data-kt-accordion-item="true"
Used to identify individual accordion item elements within the accordion structure.
data-kt-accordion-toggle="#content_id"
Specifies an ID of an element that should be shown or hidden when the accordion header is clicked.
Classes
active
Sets a specific accordion item to be open initially when used together with data-kt-accordion-item selector.
hidden
Sets a specific accordion content to be hidden initially when used together with data-kt-accordion-item selector.
Tailwind Modifiers
Custom modifiers to control the accordion’s style and behavior with Tailwind classes.
Name
Description
kt-accordion-active:*
A modifier that applies specific Tailwind classes when the section becomes active.
Methods
Use KTAccordion component's API methods to programmatically control its behavior.
Method
Description
new KTAccordion(element, options)
Creates an object instance of KTAccordion class for the given DOM -element- and configuration options.
show(element)
Shows the accordion section that corresponds to the given DOM element.
hide(element)
Hides the accordion section that corresponds to the given DOM element.
toggle(element)
Toggles the accordion section that corresponds to the given DOM element.
getOption(name)
Retrieves the value of a configuration option by name parameter from a KTAccordion instance.
getElement()
Retrieves the DOM element linked to a specific KTAccordion instance.
on(eventName, handler)
Allows attaching event listeners to the KTAccordion custom events using the eventName and eventId string parameters. These events enable programmatic interaction based on user actions or internal state changes of KTAccordion. The function returns string as a unique identifier for the registered listener, allowing you to remove it later if needed.
off(eventName, eventId)
Removes an event listener for the eventName and eventId parameters attached with the on method.
dispose()
Removes the KTAccordion instance from an element, including any associated data stored on the DOM element.
// Accordion elementconst accordionEl = document.querySelector('#my_accordion');// Accordion item elementconst accordionItemEl = document.querySelector('#my_accordion_item_1');// Get instance of the initialized accordion objectconst accordion = KTAccordion.getInstance(accordionEl);// Create a new instance on demand.const newAccordion = new KTAccordion(accordionEl, { expandAll: false});// Public methodsaccordion.show(accordionItemEl);accordion.hide(accordionItemEl);accordion.toggle(accordionItemEl);
To initialize Tailwind Accordion with JavaScript, use data-accordion="false" attribute instead.
This prevents automatic initialization on page load.
Utilities
Manage and interact with KTAccordion instances using these static methods of KTAccordion JavaScript class.
Method
Description
init()
Automatically initializes KTAccordion object for all elements with the data-kt-accordion attribute on page load.
createInstances()
Allows to create KTAccordion instances for all elements that have been dynamically added to the DOM but haven't been activated yet.
getInstance(element)
Returns the KTAccordion object associated with the given DOM element element.
getOrCreateInstance(element)
Returns the existing KTAccordion object for the provided DOM element element, or creates a new instance if none exists, then returns the same.
// Initialize all accordionsKTAccordion.init()// Initialzie pending accordionsKTAccordion.createInstances();// Get accordion objectconst accordionEl = document.querySelector('#my_accordion');const accordion = KTAccordion.getInstance(accordionEl);
Event
KTAccordion custom events allows you to register callback functions(event listeners)
that will be invoked automatically whenever specific custom events are triggered within the component.
Event
Description
show
Triggered immediately before an accordion section is shown.
shown
Triggered immediately after an accordion section is shown.
hide
Triggered immediately before an accordion section is hidden.
hidden
Triggered immediately after an accordion section is hidden.
toggle
Triggered immediately before an accordion section is toggled (shown/hidden).