html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive FAQ Accordion</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
line-height: 1.6;
color: #333;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
min-height: 100vh;
padding: 20px;
}
.faq-section {
max-width: 900px;
margin: 0 auto;
}
.faq-section h2 {
text-align: center;
margin-bottom: 40px;
font-size: 2.5rem;
color: #2c3e50;
position: relative;
}
.faq-section h2::after {
content: '';
display: block;
width: 60px;
height: 4px;
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
margin: 10px auto 0;
border-radius: 2px;
}
.faq-list {
list-style: none;
}
.faq-item {
background: white;
border-radius: 12px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
overflow: hidden;
transition: box-shadow 0.3s ease;
}
.faq-item:hover {
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
}
.faq-question {
padding: 24px 32px;
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 1.2rem;
font-weight: 600;
color: #2c3e50;
transition: color 0.3s ease;
user-select: none;
}
.faq-question:hover {
color: #667eea;
}
.faq-icon {
font-size: 1.8rem;
font-weight: bold;
color: #667eea;
transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.faq-item.open .faq-icon {
transform: rotate(45deg);
}
.faq-answer {
max-height: 0;
overflow: hidden;
transition: max-height 0.4s cubic-bezier(0.4, 0, 0.2, 1);
background: linear-gradient(180deg, #f8f9ff 0%, #f0f2ff 100%);
padding: 0 32px;
}
.faq-item.open .faq-answer {
padding: 0 32px 24px;
}
.faq-answer p {
font-size: 1rem;
color: #5a6c7d;
line-height: 1.7;
}
/* Responsive */
@media (max-width: 768px) {
body {
padding: 10px;
}
.faq-section h2 {
font-size: 2rem;
margin-bottom: 30px;
}
.faq-question {
padding: 20px 24px;
font-size: 1.1rem;
}
.faq-icon {
font-size: 1.5rem;
width: 28px;
height: 28px;
}
.faq-item.open .faq-answer {
padding: 0 24px 20px;
}
.faq-answer {
padding-left: 24px;
padding-right: 24px;
}
}
@media (max-width: 480px) {
.faq-question {
padding: 18px 20px;
font-size: 1rem;
}
.faq-icon {
font-size: 1.3rem;
width: 24px;
height: 24px;
}
}
</style>
</head>
<body>
<section class="faq-section" role="region" aria-labelledby="faq-heading">
<h2 id="faq-heading">Frequently Asked Questions</h2>
<ul class="faq-list" role="list">
<li class="faq-item" role="region">
<div class="faq-question" role="button" tabindex="0" aria-expanded="false">
<span>What is a responsive FAQ accordion?</span>
<span class="faq-icon" aria-hidden="true">+</span>
</div>
<div class="faq-answer">
<p>A responsive FAQ accordion is a UI component that displays questions and answers in a collapsible format. It adapts seamlessly to different screen sizes, providing smooth expand/collapse animations for better user experience on desktops, tablets, and mobiles.</p>
</div>
</li>
<li class="faq-item" role="region">
<div class="faq-question" role="button" tabindex="0" aria-expanded="false">
<span>How does the animation work?</span>
<span class="faq-icon" aria-hidden="true">+</span>
</div>
<div class="faq-answer">
<p>The animation uses CSS transitions on <code>max-height</code> for the answer content and <code>transform: rotate()</code> for the icon. JavaScript dynamically sets the <code>max-height</code> to the content's <code>scrollHeight</code> for precise, smooth expansion without clipping.</p>
</div>
</li>
<li class="faq-item" role="region">
<div class="faq-question" role="button" tabindex="0" aria-expanded="false">
<span>Is it fully responsive?</span>
<span class="faq-icon" aria-hidden="true">+</span>
</div>
<div class="faq-answer">
<p>Yes! It uses mobile-first CSS with media queries for breakpoints at 768px and 480px. Flexbox ensures proper alignment, and padding/font sizes scale appropriately for all devices.</p>
</div>
</li>
<li class="faq-item" role="region">
<div class="faq-question" role="button" tabindex="0" aria-expanded="false">
<span>Can multiple sections be open at once?</span>
<span class="faq-icon" aria-hidden="true">+</span>
</div>
<div class="faq-answer">
<p>This implementation allows only one section open at a time (standard accordion behavior). Clicking another question closes the current one. Easily modifiable in JS to support multiple open states if needed.</p>
</div>
</li>
<li class="faq-item" role="region">
<div class="faq-question" role="button" tabindex="0" aria-expanded="false">
<span>How can I customize it?</span>
<span class="faq-icon" aria-hidden="true">+</span>
</div>
<div class="faq-answer">
<p>Edit the HTML for content, tweak CSS variables/colors/shadows/transitions, or modify JS for behavior (e.g., allow multiple opens). It's modular—copy the section into any project. Fully accessible with ARIA roles and keyboard support.</p>
</div>
</li>
</ul>
</section>
<script>
document.querySelectorAll('.faq-question').forEach(question => {
question.addEventListener('click', toggleFAQ);
question.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
toggleFAQ.call(question);
}
});
});
function toggleFAQ() {
const item = this.parentElement;
const answer = item.querySelector('.faq-answer');
const isOpen = item.classList.contains('open');
const icon = this.querySelector('.faq-icon');
const ariaExpanded = this.getAttribute('aria-expanded') === 'true';
// Close all other items
document.querySelectorAll('.faq-item.open').forEach(otherItem => {
if (otherItem !== item) {
const otherAnswer = otherItem.querySelector('.faq-answer');
const otherQuestion = otherItem.querySelector('.faq-question');
otherItem.classList.remove('open');
otherAnswer.style.maxHeight = '0px';
otherQuestion.setAttribute('aria-expanded', 'false');
}
});
// Toggle current
if (isOpen) {
item.classList.remove('open');
answer.style.maxHeight = '0px';
this.setAttribute('aria-expanded', 'false');
} else {
item.classList.add('open');
answer.style.maxHeight = answer.scrollHeight + 'px';
this.setAttribute('aria-expanded', 'true');
}
}
// Handle dynamic content height on window resize (e.g., font changes)
window.addEventListener('resize', () => {
document.querySelectorAll('.faq-item.open').forEach(item => {
const answer = item.querySelector('.faq-answer');
answer.style.maxHeight = answer.scrollHeight + 'px';
});
});
</script>
</body>
</html>
This is a complete, self-contained, responsive FAQ accordion with smooth animations. Key features: