Accordion Component
Accordion: A vertically stacked set of interactive headings that each reveal a section of content.
Live Examples
1. Basic Accordion
Example Preview
- What is UIKit?
UIKit is a lightweight and modular front-end framework for developing fast and powerful web interfaces. It provides a comprehensive collection of HTML, CSS, and JS components.
- Is UIKit free to use?
Yes, UIKit is completely free and open source. You can use it for personal and commercial projects without any restrictions.
- How do I install UIKit?
You can install UIKit via CDN, NPM, Yarn, or download it directly from GitHub. Check the installation guide for detailed instructions.
2. Multiple Open Accordion
Example Preview
3. Accordion with Icons
Example Preview
4. Nested Accordion
Example Preview
- Web Development
- Mobile Development
Native and cross-platform mobile app development.
JavaScript Methods
Methods:
// Get accordion instance const accordion = UIkit.accordion(element); // Toggle an item accordion.toggle(index, animate); // Open an item accordion.toggle(index, true); // Close an item accordion.toggle(index, false);
Example Usage:
// HTML
<ul id="myAccordion" uk-accordion>
<li>...</li>
</ul>
<button onclick="openFirst()">Open First</button>
// JavaScript
function openFirst() {
const accordion = UIkit.accordion('#myAccordion');
accordion.toggle(0, true);
}Complete Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>UIKit Accordion Example</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/uikit@3.19.2/dist/css/uikit.min.css" />
</head>
<body>
<div class="uk-container uk-margin-top">
<h2>Frequently Asked Questions</h2>
<ul uk-accordion="multiple: true; collapsible: false">
<li class="uk-open">
<a class="uk-accordion-title" href="#">
<span uk-icon="icon: question"></span>
What browsers does UIKit support?
</a>
<div class="uk-accordion-content">
<p>UIKit supports all modern browsers including Chrome, Firefox, Safari, and Edge.</p>
</div>
</li>
<li>
<a class="uk-accordion-title" href="#">
<span uk-icon="icon: question"></span>
Can I customize the theme?
</a>
<div class="uk-accordion-content">
<p>Yes, UIKit is highly customizable using Sass variables.</p>
</div>
</li>
<li>
<a class="uk-accordion-title" href="#">
<span uk-icon="icon: question"></span>
Is it mobile responsive?
</a>
<div class="uk-accordion-content">
<p>Absolutely! UIKit follows a mobile-first approach.</p>
</div>
</li>
</ul>
</div>
<!-- UIKit JS -->
<script src="https://cdn.jsdelivr.net/npm/uikit@3.19.2/dist/js/uikit.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/uikit@3.19.2/dist/js/uikit-icons.min.js"></script>
<script>
// JavaScript to programmatically control accordion
document.addEventListener('DOMContentLoaded', function() {
const accordion = UIkit.accordion('ul[uk-accordion]');
// Open all items after 3 seconds
setTimeout(() => {
accordion.toggle(0, true);
accordion.toggle(1, true);
accordion.toggle(2, true);
}, 3000);
});
</script>
</body>
</html>Options & Attributes
| Option | Type | Default | Description |
|---|---|---|---|
active | Number | false | Index of active element |
animation | Boolean | true | Enable opening/closing animation |
collapsible | Boolean | true | Allow all items to be closed |
content | String | .uk-accordion-content | CSS selector for content |
duration | Number | 200 | Animation duration in ms |
multiple | Boolean | false | Allow multiple open items |
targets | String | > * | CSS selector for items |
toggle | String | .uk-accordion-title | CSS selector for toggle |
transition | String | ease | Animation easing function |
Best Practices:
- Use clear and descriptive titles for each accordion item
- Keep content concise and relevant to the title
- Consider using icons to enhance visual hierarchy
- Test keyboard navigation for accessibility
- Use
multiple: truewhen users need to compare content - Set
collapsible: falsewhen one item must always be open