Develop an accordion menu that smoothly expands and collapses its content sections using…
- User
Develop an accordion menu that smoothly expands and collapses its content sections using HTML, CSS, and JS.
- Assistant
Creating accordion menu
The user is asking for an accordion menu that smoothly expands and collapses, using HTML, CSS, and JS. It seems they're looking for sample code, and I should provide a full working example with explanations. I want to ensure the transitions are smooth, using techniques like max-height transitions. A simple approach is toggling an "active" class with JS to set the maxHeight. I’ll include CSS for styling and make sure to create a user-friendly item setup.Implementing accessible accordion
I'm focusing on creating accessible semantics for the accordion menu, using button elements as headers with attributes like aria-expanded, aria-controls, and role=region. To achieve smooth transitions, I'll apply CSS transitions on max-height or height.
Although there's an option to use CSS only, the user prefers JavaScript. So, I’ll compute the content height in JS for smooth expand/collapse. For each accordion item, I'll set the maxHeight to create transitions, ensuring the design is both functional and accessible. Now, I'll craft the final code!Building the accordion menu code
I’m putting together the HTML structure for the accordion, with buttons as headers including aria-expanded attributes for accessibility. The JavaScript will listen for click events on each header.
When a header is clicked, I need to check if it’s already expanded. If it is, I’ll set aria-expanded to false, change the maxHeight to 0, and manage the hidden attribute to ensure a smooth transition. I might remove hidden when opening and only add it back after they close, so the animation is preserved. Now, let’s wrap it up!Refining accordion functionality
I’m refining how the accordion expands and collapses. If the content isn’t expanded, I’ll make it visible and set the maxHeight to its scrollHeight. For closing, I want to animate to zero height smoothly, using requestAnimationFrame for better performance.
While I could ignore the hidden attribute and rely on maxHeight and aria-hidden for accessibility, I want to keep things clear and simple. I’ll use CSS for managing the max-height and overflow properties while maximizing readability and usability. However, using JS to manage dynamic height feels more practical.Crafting the accordion functionality
For demonstration, I can set a large max-height like 1000px to allow for animation, but it feels hacky. Instead, I want a dynamic approach. On DOMContentLoaded, I’ll set the maxHeight of each content area to 0px and toggle open/closed states on header clicks.
I’ll also add the option to allow only one section open at a time. The JavaScript will manage each item's state, and I'll include corresponding CSS for styling, including a nice rotation for the accordion icon. Finally, I’ll present the complete HTML file with moderate explanations and customization notes to keep it concise. Here is a complete, minimal accordion that smoothly expands and collapses sections using HTML, CSS, and JavaScript.
You can save this as
index.htmland open it in a browser.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Smooth Accordion Example</title> <style> * { box-sizing: border-box; } body { font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: #f5f5f5; margin: 0; padding: 40px; display: flex; justify-content: center; } .accordion { width: 100%; max-width: 600px; background: #fff; border-radius: 8px; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08); overflow: hidden; } .accordion-item + .accordion-item { border-top: 1px solid #e2e2e2; } .accordion-header { width: 100%; padding: 16px 20px; border: none; background: #fff; cursor: pointer; display: flex; align-items: center; justify-content: space-between; font-size: 16px; font-weight: 500; text-align: left; transition: background 0.2s ease; } .accordion-header:hover { background: #f9f9f9; } .accordion-title { flex: 1; margin-right: 12px; } .accordion-icon { transition: transform 0.25s ease; font-size: 18px; } .accordion-item.open .accordion-icon { transform: rotate(90deg); } .accordion-content { max-height: 0; overflow: hidden; padding: 0 20px; background: #fafafa; color: #444; transition: max-height 0.3s ease; } .accordion-content-inner { padding: 12px 0 16px; line-height: 1.5; font-size: 14px; } </style> </head> <body> <div class="accordion" id="accordion"> <div class="accordion-item"> <button class="accordion-header" aria-expanded="false"> <span class="accordion-title">Section 1: Introduction</span> <span class="accordion-icon">▶</span> </button> <div class="accordion-content"> <div class="accordion-content-inner"> <p> This is the content of section 1. You can place any HTML here: text, images, lists, etc. </p> <p> It expands and collapses smoothly thanks to a CSS transition on <code>max-height</code> controlled by JavaScript. </p> </div> </div> </div> <div class="accordion-item"> <button class="accordion-header" aria-expanded="false"> <span class="accordion-title">Section 2: Details</span> <span class="accordion-icon">▶</span> </button> <div class="accordion-content"> <div class="accordion-content-inner"> <ul> <li>Item one with more information.</li> <li>Item two with additional details.</li> <li>Item three with further explanation.</li> </ul> </div> </div> </div> <div class="accordion-item"> <button class="accordion-header" aria-expanded="false"> <span class="accordion-title">Section 3: FAQ</span> <span class="accordion-icon">▶</span> </button> <div class="accordion-content"> <div class="accordion-content-inner"> <p><strong>Q:</strong> Can multiple sections be open?</p> <p><strong>A:</strong> In this example, only one section is open at a time. You can change that in the JS.</p> </div> </div> </div> </div> <script> const accordionItems = document.querySelectorAll('.accordion-item'); accordionItems.forEach((item) => { const header = item.querySelector('.accordion-header'); const content = item.querySelector('.accordion-content'); header.addEventListener('click', () => { const isOpen = item.classList.contains('open'); // Optional: close all other items (so only one is open at a time) accordionItems.forEach((otherItem) => { if (otherItem !== item) { otherItem.classList.remove('open'); const otherContent = otherItem.querySelector('.accordion-content'); const otherHeader = otherItem.querySelector('.accordion-header'); otherContent.style.maxHeight = null; otherHeader.setAttribute('aria-expanded', 'false'); } }); if (!isOpen) { // Open this item item.classList.add('open'); header.setAttribute('aria-expanded', 'true'); content.style.maxHeight = content.scrollHeight + 'px'; } else { // Close this item item.classList.remove('open'); header.setAttribute('aria-expanded', 'false'); content.style.maxHeight = null; } }); }); // Optional: expand the first item on load // const firstItem = accordionItems[0]; // if (firstItem) { // const firstHeader = firstItem.querySelector('.accordion-header'); // firstHeader.click(); // } </script> </body> </html>Key points you can customize:
- Change the animation speed via
transition: max-height 0.3s ease;. - Allow multiple sections open by removing the loop that closes
otherItems. - Adjust styles (colors, fonts, icons) in the CSS as needed.
- Change the animation speed via
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.