The user wants a responsive FAQ accordion section with animated expand-and-collapse functionality. Let me create a comprehensive solution with:
- HTML structure for FAQ items
- CSS for styling and animations
- JavaScript for the expand/collapse functionality
Features I'll include:
- Smooth animations for opening/closing
- Icon rotation for visual feedback
- Responsive design for all screen sizes
- Keyboard accessibility
- Clean, modern design
- Multiple FAQ items
- Only one item open at a time (optional - I'll make it configurable)
- Hover effects
- Focus states for accessibility
Let me create a complete, well-documented solution. # Responsive FAQ Accordion Section
Here's a complete FAQ accordion with smooth animations, modern styling, and full accessibility support.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FAQ Accordion</title>
<style>
/* CSS Reset & Variables */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root {
--primary-color: #6366f1;
--primary-dark: #4f46e5;
--bg-color: #f8fafc;
--card-bg: #ffffff;
--text-primary: #1e293b;
--text-secondary: #64748b;
--border-color: #e2e8f0;
--shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
0 2px 4px -1px rgba(0, 0, 0, 0.06);
--shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
0 4px 6px -2px rgba(0, 0, 0, 0.05);
--transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
--radius: 12px;
}
/* Dark mode */
[data-theme="dark"] {
--bg-color: #0f172a;
--card-bg: #1e293b;
--text-primary: #f1f5f9;
--text-secondary: #94a3b8;
--border-color: #334155;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: var(--bg-color);
min-height: 100vh;
padding: 20px;
transition: var(--transition);
}
/* Container */
.faq-container {
max-width: 800px;
margin: 0 auto;
padding: 40px 20px;
}
/* Header Section */
.faq-header {
text-align: center;
margin-bottom: 50px;
}
.faq-header h1 {
color: var(--text-primary);
font-size: clamp(2rem, 5vw, 3rem);
font-weight: 700;
margin-bottom: 15px;
background: linear-gradient(135deg, var(--primary-color), #a855f7);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.faq-header p {
color: var(--text-secondary);
font-size: 1.1rem;
max-width: 500px;
margin: 0 auto;
}
/* Theme Toggle */
.theme-toggle {
position: fixed;
top: 20px;
right: 20px;
background: var(--card-bg);
border: 2px solid var(--border-color);
border-radius: 50%;
width: 50px;
height: 50px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.5rem;
box-shadow: var(--shadow);
transition: var(--transition);
z-index: 100;
}
.theme-toggle:hover {
transform: scale(1.1);
box-shadow: var(--shadow-lg);
}
/* Controls */
.faq-controls {
display: flex;
justify-content: center;
gap: 15px;
margin-bottom: 30px;
flex-wrap: wrap;
}
.control-btn {
padding: 10px 20px;
background: var(--card-bg);
border: 2px solid var(--border-color);
border-radius: 25px;
color: var(--text-primary);
font-size: 0.9rem;
font-weight: 500;
cursor: pointer;
transition: var(--transition);
}
.control-btn:hover {
background: var(--primary-color);
border-color: var(--primary-color);
color: white;
transform: translateY(-2px);
}
/* Search Box */
.search-container {
position: relative;
margin-bottom: 30px;
}
.search-input {
width: 100%;
padding: 15px 20px 15px 50px;
font-size: 1rem;
border: 2px solid var(--border-color);
border-radius: var(--radius);
background: var(--card-bg);
color: var(--text-primary);
transition: var(--transition);
}
.search-input:focus {
outline: none;
border-color: var(--primary-color);
box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
}
.search-icon {
position: absolute;
left: 18px;
top: 50%;
transform: translateY(-50%);
color: var(--text-secondary);
font-size: 1.2rem;
}
/* Category Tabs */
.category-tabs {
display: flex;
gap: 10px;
margin-bottom: 30px;
overflow-x: auto;
padding-bottom: 10px;
scrollbar-width: none;
}
.category-tabs::-webkit-scrollbar {
display: none;
}
.category-tab {
padding: 10px 20px;
background: var(--card-bg);
border: 2px solid var(--border-color);
border-radius: 25px;
color: var(--text-secondary);
font-size: 0.9rem;
font-weight: 500;
cursor: pointer;
transition: var(--transition);
white-space: nowrap;
}
.category-tab:hover,
.category-tab.active {
background: var(--primary-color);
border-color: var(--primary-color);
color: white;
}
/* Accordion */
.accordion {
display: flex;
flex-direction: column;
gap: 15px;
}
/* FAQ Item */
.faq-item {
background: var(--card-bg);
border: 2px solid var(--border-color);
border-radius: var(--radius);
overflow: hidden;
transition: var(--transition);
animation: fadeIn 0.5s ease forwards;
opacity: 0;
}
.faq-item:nth-child(1) { animation-delay: 0.1s; }
.faq-item:nth-child(2) { animation-delay: 0.2s; }
.faq-item:nth-child(3) { animation-delay: 0.3s; }
.faq-item:nth-child(4) { animation-delay: 0.4s; }
.faq-item:nth-child(5) { animation-delay: 0.5s; }
.faq-item:nth-child(6) { animation-delay: 0.6s; }
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.faq-item:hover {
box-shadow: var(--shadow-lg);
border-color: var(--primary-color);
}
.faq-item.active {
border-color: var(--primary-color);
box-shadow: var(--shadow-lg);
}
.faq-item.hidden {
display: none;
}
/* Question Button */
.faq-question {
width: 100%;
padding: 20px 25px;
background: transparent;
border: none;
display: flex;
align-items: center;
justify-content: space-between;
gap: 15px;
cursor: pointer;
text-align: left;
transition: var(--transition);
}
.faq-question:focus {
outline: none;
background: rgba(99, 102, 241, 0.05);
}
.question-content {
display: flex;
align-items: center;
gap: 15px;
flex: 1;
}
.question-number {
width: 35px;
height: 35px;
background: linear-gradient(135deg, var(--primary-color), #a855f7);
color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.85rem;
font-weight: 600;
flex-shrink: 0;
}
.question-text {
color: var(--text-primary);
font-size: 1.05rem;
font-weight: 600;
line-height: 1.5;
}
/* Icon */
.faq-icon {
width: 40px;
height: 40px;
background: var(--bg-color);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
transition: var(--transition);
}
.faq-icon svg {
width: 20px;
height: 20px;
stroke: var(--primary-color);
transition: var(--transition);
}
.faq-item.active .faq-icon {
background: var(--primary-color);
}
.faq-item.active .faq-icon svg {
stroke: white;
transform: rotate(180deg);
}
/* Answer */
.faq-answer {
max-height: 0;
overflow: hidden;
transition: max-height 0.4s cubic-bezier(0.4, 0, 0.2, 1),
padding 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}
.faq-item.active .faq-answer {
max-height: 500px;
}
.answer-content {
padding: 0 25px 25px 75px;
color: var(--text-secondary);
line-height: 1.8;
font-size: 1rem;
}
.answer-content p {
margin-bottom: 15px;
}
.answer-content p:last-child {
margin-bottom: 0;
}
.answer-content ul {
margin: 15px 0;
padding-left: 20px;
}
.answer-content li {
margin-bottom: 8px;
}
.answer-content a {
color: var(--primary-color);
text-decoration: none;
font-weight: 500;
}
.answer-content a:hover {
text-decoration: underline;
}
/* Tags */
.answer-tags {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 15px;
}
.tag {
padding: 5px 12px;
background: rgba(99, 102, 241, 0.1);
color: var(--primary-color);
border-radius: 15px;
font-size: 0.8rem;
font-weight: 500;
}
/* No Results */
.no-results {
text-align: center;
padding: 50px 20px;
color: var(--text-secondary);
display: none;
}
.no-results.visible {
display: block;
}
.no-results svg {
width: 80px;
height: 80px;
stroke: var(--border-color);
margin-bottom: 20px;
}
.no-results h3 {
color: var(--text-primary);
margin-bottom: 10px;
}
/* Footer */
.faq-footer {
text-align: center;
margin-top: 50px;
padding: 30px;
background: var(--card-bg);
border-radius: var(--radius);
border: 2px solid var(--border-color);
}
.faq-footer h3 {
color: var(--text-primary);
margin-bottom: 10px;
}
.faq-footer p {
color: var(--text-secondary);
margin-bottom: 20px;
}
.contact-btn {
padding: 12px 30px;
background: linear-gradient(135deg, var(--primary-color), #a855f7);
color: white;
border: none;
border-radius: 25px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: var(--transition);
}
.contact-btn:hover {
transform: translateY(-3px);
box-shadow: 0 10px 20px rgba(99, 102, 241, 0.3);
}
/* Responsive */
@media (max-width: 768px) {
.faq-container {
padding: 20px 10px;
}
.faq-question {
padding: 15px 18px;
}
.question-number {
width: 30px;
height: 30px;
font-size: 0.8rem;
}
.question-text {
font-size: 0.95rem;
}
.answer-content {
padding: 0 18px 20px 63px;
font-size: 0.9rem;
}
.faq-icon {
width: 35px;
height: 35px;
}
.faq-icon svg {
width: 16px;
height: 16px;
}
}
@media (max-width: 480px) {
.answer-content {
padding: 0 18px 20px 18px;
}
.question-number {
display: none;
}
}
/* Print Styles */
@media print {
.theme-toggle,
.faq-controls,
.search-container,
.category-tabs {
display: none;
}
.faq-item {
break-inside: avoid;
}
.faq-answer {
max-height: none !important;
}
}
</style>
</head>
<body>
<!-- Theme Toggle -->
<button class="theme-toggle" onclick="toggleTheme()" aria-label="Toggle dark mode">
🌙
</button>
<div class="faq-container">
<!-- Header -->
<header class="faq-header">
<h1>Frequently Asked Questions</h1>
<p>Find answers to common questions about our products and services</p>
</header>
<!-- Search -->
<div class="search-container">
<span class="search-icon">🔍</span>
<input
type="text"
class="search-input"
placeholder="Search for questions..."
id="searchInput"
aria-label="Search FAQ"
>
</div>
<!-- Category Tabs -->
<div class="category-tabs" role="tablist">
<button class="category-tab active" data-category="all">All Questions</button>
<button class="category-tab" data-category="general">General</button>
<button class="category-tab" data-category="pricing">Pricing</button>
<button class="category-tab" data-category="features">Features</button>
<button class="category-tab" data-category="support">Support</button>
</div>
<!-- Controls -->
<div class="faq-controls">
<button class="control-btn" onclick="expandAll()">Expand All</button>
<button class="control-btn" onclick="collapseAll()">Collapse All</button>
</div>
<!-- Accordion -->
<div class="accordion" id="faqAccordion">
<!-- FAQ Item 1 -->
<div class="faq-item" data-category="general">
<button class="faq-question" aria-expanded="false">
<div class="question-content">
<span class="question-number">01</span>
<span class="question-text">What is your product and how does it work?</span>
</div>
<div class="faq-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</div>
</button>
<div class="faq-answer">
<div class="answer-content">
<p>Our product is a comprehensive solution designed to streamline your workflow and boost productivity. It works by integrating seamlessly with your existing tools and automating repetitive tasks.</p>
<p>Key features include:</p>
<ul>
<li>Real-time collaboration</li>
<li>Advanced analytics dashboard</li>
<li>Customizable workflows</li>
<li>24/7 customer support</li>
</ul>
<div class="answer-tags">
<span class="tag">Getting Started</span>
<span class="tag">Overview</span>
</div>
</div>
</div>
</div>
<!-- FAQ Item 2 -->
<div class="faq-item" data-category="pricing">
<button class="faq-question" aria-expanded="false">
<div class="question-content">
<span class="question-number">02</span>
<span class="question-text">What pricing plans do you offer?</span>
</div>
<div class="faq-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</div>
</button>
<div class="faq-answer">
<div class="answer-content">
<p>We offer flexible pricing plans to suit businesses of all sizes:</p>
<ul>
<li><strong>Starter:</strong> $9/month - Perfect for individuals</li>
<li><strong>Professional:</strong> $29/month - Ideal for small teams</li>
<li><strong>Enterprise:</strong> Custom pricing - For large organizations</li>
</ul>
<p>All plans include a 14-day free trial with no credit card required. <a href="#">View full pricing details →</a></p>
<div class="answer-tags">
<span class="tag">Pricing</span>
<span class="tag">Plans</span>
</div>
</div>
</div>
</div>
<!-- FAQ Item 3 -->
<div class="faq-item" data-category="features">
<button class="faq-question" aria-expanded="false">
<div class="question-content">
<span class="question-number">03</span>
<span class="question-text">Can I integrate with other tools and services?</span>
</div>
<div class="faq-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</div>
</button>
<div class="faq-answer">
<div class="answer-content">
<p>Yes! We support integrations with 100+ popular tools and services including:</p>
<ul>
<li>Slack, Microsoft Teams, Discord</li>
<li>Google Workspace, Microsoft 365</li>
<li>Salesforce, HubSpot, Zendesk</li>
<li>Zapier, Make (Integromat)</li>
</ul>
<p>We also provide a robust API for custom integrations. Check our <a href="#">integration documentation</a> for more details.</p>
<div class="answer-tags">
<span class="tag">Integrations</span>
<span class="tag">API</span>
</div>
</div>
</div>
</div>
<!-- FAQ Item 4 -->
<div class="faq-item" data-category="support">
<button class="faq-question" aria-expanded="false">
<div class="question-content">
<span class="question-number">04</span>
<span class="question-text">How can I contact customer support?</span>
</div>
<div class="faq-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</div>
</button>
<div class="faq-answer">
<div class="answer-content">
<p>Our support team is here to help! You can reach us through:</p>
<ul>
<li><strong>Live Chat:</strong> Available 24/7 on our website</li>
<li><strong>Email:</strong> support@example.com (response within 4 hours)</li>
<li><strong>Phone:</strong> 1-800-EXAMPLE (Mon-Fri, 9AM-6PM EST)</li>
<li><strong>Help Center:</strong> Searchable knowledge base with tutorials</li>
</ul>
<p>Enterprise customers get access to dedicated account managers and priority support.</p>
<div class="answer-tags">
<span class="tag">Support</span>
<span class="tag">Contact</span>
</div>
</div>
</div>
</div>
<!-- FAQ Item 5 -->
<div class="faq-item" data-category="general">
<button class="faq-question" aria-expanded="false">
<div class="question-content">
<span class="question-number">05</span>
<span class="question-text">Is my data secure and private?</span>
</div>
<div class="faq-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</div>
</button>
<div class="faq-answer">
<div class="answer-content">
<p>Absolutely! Security is our top priority. We implement industry-leading security measures:</p>
<ul>
<li>256-bit SSL encryption for all data transfers</li>
<li>SOC 2 Type II certified infrastructure</li>
<li>GDPR and CCPA compliant</li>
<li>Regular security audits and penetration testing</li>
<li>Two-factor authentication (2FA) support</li>
</ul>
<p>Your data is stored in secure, geographically distributed data centers with 99.99% uptime guarantee.</p>
<div class="answer-tags">
<span class="tag">Security</span>
<span class="tag">Privacy</span>
<span class="tag">Compliance</span>
</div>
</div>
</div>
</div>
<!-- FAQ Item 6 -->
<div class="faq-item" data-category="pricing">
<button class="faq-question" aria-expanded="false">
<div class="question-content">
<span class="question-number">06</span>
<span class="question-text">What is your refund policy?</span>
</div>
<div class="faq-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</div>
</button>
<div class="faq-answer">
<div class="answer-content">
<p>We offer a hassle-free 30-day money-back guarantee on all plans. If you're not satisfied with our service for any reason, simply contact our support team within 30 days of your purchase for a full refund.</p>
<p>Annual subscriptions can be refunded on a prorated basis after the 30-day period. No questions asked!</p>
<div class="answer-tags">
<span class="tag">Refund</span>
<span class="tag">Guarantee</span>
</div>
</div>
</div>
</div>
</div>
<!-- No Results Message -->
<div class="no-results" id="noResults">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
<circle cx="11" cy="11" r="8"></circle>
<path d="m21 21-4.35-4.35"></path>
</svg>
<h3>No results found</h3>
<p>Try adjusting your search or filter to find what you're looking for.</p>
</div>
<!-- Footer CTA -->
<footer class="faq-footer">
<h3>Still have questions?</h3>
<p>Can't find the answer you're looking for? Our team is happy to help!</p>
<button class="contact-btn">Contact Support</button>
</footer>
</div>
<script>
// FAQ Accordion Class
class FAQAccordion {
constructor(containerId, options = {}) {
this.container = document.getElementById(containerId);
this.items = this.container.querySelectorAll('.faq-item');
this.searchInput = document.getElementById('searchInput');
this.categoryTabs = document.querySelectorAll('.category-tab');
this.noResults = document.getElementById('noResults');
// Options with defaults
this.options = {
allowMultiple: options.allowMultiple || false,
animationDuration: options.animationDuration || 300,
...options
};
this.activeCategory = 'all';
this.searchTerm = '';
this.init();
}
init() {
// Initialize accordion items
this.items.forEach(item => {
const button = item.querySelector('.faq-question');
// Click event
button.addEventListener('click', () => this.toggleItem(item));
// Keyboard navigation
button.addEventListener('keydown', (e) => this.handleKeydown(e, item));
});
// Search functionality
if (this.searchInput) {
this.searchInput.addEventListener('input',
this.debounce(() => this.handleSearch(), 300)
);
}
// Category tabs
this.categoryTabs.forEach(tab => {
tab.addEventListener('click', () => this.handleCategoryChange(tab));
});
}
toggleItem(item) {
const isActive = item.classList.contains('active');
const button = item.querySelector('.faq-question');
const answer = item.querySelector('.faq-answer');
// Close others if not allowing multiple
if (!this.options.allowMultiple && !isActive) {
this.items.forEach(otherItem => {
if (otherItem !== item && otherItem.classList.contains('active')) {
this.closeItem(otherItem);
}
});
}
// Toggle current item
if (isActive) {
this.closeItem(item);
} else {
this.openItem(item);
}
}
openItem(item) {
const button = item.querySelector('.faq-question');
const answer = item.querySelector('.faq-answer');
const content = answer.querySelector('.answer-content');
item.classList.add('active');
button.setAttribute('aria-expanded', 'true');
// Animate max-height
answer.style.maxHeight = content.scrollHeight + 'px';
// Announce to screen readers
this.announce(`Answer expanded`);
}
closeItem(item) {
const button = item.querySelector('.faq-question');
const answer = item.querySelector('.faq-answer');
item.classList.remove('active');
button.setAttribute('aria-expanded', 'false');
answer.style.maxHeight = '0';
}
handleKeydown(e, item) {
const items = Array.from(this.items).filter(i => !i.classList.contains('hidden'));
const currentIndex = items.indexOf(item);
switch(e.key) {
case 'ArrowDown':
e.preventDefault();
const nextItem = items[currentIndex + 1] || items[0];
nextItem.querySelector('.faq-question').focus();
break;
case 'ArrowUp':
e.preventDefault();
const prevItem = items[currentIndex - 1] || items[items.length - 1];
prevItem.querySelector('.faq-question').focus();
break;
case 'Home':
e.preventDefault();
items[0].querySelector('.faq-question').focus();
break;
case 'End':
e.preventDefault();
items[items.length - 1].querySelector('.faq-question').focus();
break;
}
}
handleSearch() {
this.searchTerm = this.searchInput.value.toLowerCase().trim();
this.filterItems();
}
handleCategoryChange(tab) {
// Update active tab
this.categoryTabs.forEach(t => t.classList.remove('active'));
tab.classList.add('active');
this.activeCategory = tab.dataset.category;
this.filterItems();
}
filterItems() {
let visibleCount = 0;
this.items.forEach(item => {
const question = item.querySelector('.question-text').textContent.toLowerCase();
const answer = item.querySelector('.answer-content').textContent.toLowerCase();
const category = item.dataset.category;
const matchesSearch = !this.searchTerm ||
question.includes(this.searchTerm) ||
answer.includes(this.searchTerm);
const matchesCategory = this.activeCategory === 'all' ||
category === this.activeCategory;
if (matchesSearch && matchesCategory) {
item.classList.remove('hidden');
visibleCount++;
// Highlight search terms
if (this.searchTerm) {
this.highlightText(item, this.searchTerm);
} else {
this.removeHighlight(item);
}
} else {
item.classList.add('hidden');
}
});
// Show/hide no results message
this.noResults.classList.toggle('visible', visibleCount === 0);
}
highlightText(item, term) {
const questionText = item.querySelector('.question-text');
const originalText = questionText.textContent;
if (term && originalText.toLowerCase().includes(term)) {
const regex = new RegExp(`(${term})`, 'gi');
questionText.innerHTML = originalText.replace(regex,
'<mark style="background: yellow; padding: 0 2px;">$1</mark>'
);
}
}
removeHighlight(item) {
const questionText = item.querySelector('.question-text');
questionText.innerHTML = questionText.textContent;
}
expandAll() {
this.items.forEach(item => {
if (!item.classList.contains('hidden')) {
this.openItem(item);
}
});
}
collapseAll() {
this.items.forEach(item => {
this.closeItem(item);
});
}
// Utility functions
debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
announce(message) {
const announcement = document.createElement('div');
announcement.setAttribute('aria-live', 'polite');
announcement.setAttribute('aria-atomic', 'true');
announcement.className = 'sr-only';
announcement.style.cssText = 'position: absolute; left: -9999px;';
announcement.textContent = message;
document.body.appendChild(announcement);
setTimeout(() => announcement.remove(), 1000);
}
}
// Initialize FAQ
const faq = new FAQAccordion('faqAccordion', {
allowMultiple: false
});
// Global functions for buttons
function expandAll() {
faq.expandAll();
}
function collapseAll() {
faq.collapseAll();
}
// Theme toggle
function toggleTheme() {
const body = document.body;
const themeBtn = document.querySelector('.theme-toggle');
if (body.dataset.theme === 'dark') {
body.dataset.theme = 'light';
themeBtn.textContent = '🌙';
localStorage.setItem('theme', 'light');
} else {
body.dataset.theme = 'dark';
themeBtn.textContent = '☀️';
localStorage.setItem('theme', 'dark');
}
}
// Load saved theme
document.addEventListener('DOMContentLoaded', () => {
const savedTheme = localStorage.getItem('theme') ||
(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
if (savedTheme === 'dark') {
document.body.dataset.theme = 'dark';
document.querySelector('.theme-toggle').textContent = '☀️';
}
});
// Add smooth scroll behavior for better UX
document.documentElement.style.scrollBehavior = 'smooth';
</script>
</body>
</html>
Features Included
🎨 Design Features
- Modern, clean UI with gradient accents
- Smooth animations and transitions
- Dark/Light theme toggle with persistence
- Responsive design for all screen sizes
- Visual feedback on hover and focus states
⚡ Functionality
| Feature | Description |
|---|
| Accordion | Smooth expand/collapse with CSS transitions |
| Search | Real-time filtering with debounced input |
| Categories | Filter FAQs by category tabs |
| Expand/Collapse All | Quick controls for all items |
| Single/Multiple Mode | Configurable accordion behavior |
♿ Accessibility
- Full keyboard navigation (Arrow keys, Home, End)
- ARIA attributes for screen readers
- Focus management
- Live announcements for state changes
- High contrast support
📱 Responsive Breakpoints
- Desktop: Full layout with numbers
- Tablet (768px): Adjusted spacing
- Mobile (480px): Simplified layout
How to Use
- Copy the code into an HTML file
- Customize the FAQ content in the HTML
- Modify colors via CSS variables in
:root
- Configure options when initializing:
const faq = new FAQAccordion('faqAccordion', {
allowMultiple: true // Allow multiple open items
});