Considering responsive design for FAQ section
I'm thinking about how to approach the FAQ section's responsiveness. I want to set the container width to 90% with a max-width of 800px for a neat layout. Using CSS for fluid margins seems preferable, and I might use the font-size clamp for adaptation. As for animations, I could employ a CSS transform to rotate an icon based on its expanded state. The structure could start with a section marked by for "FAQ."Creating a simple FAQ section example
I'm working on a straightforward HTML structure for a FAQ section. It includes a header, a list of questions, and answers hidden by default. I'm thinking that the buttons should toggle the visibility of answers using an aria-expanded attribute for accessibility. I want an accordion style where only one answer can be open at a time. The CSS is also important to ensure everything looks nice with proper spacing and interaction feedback. A nice hover effect for the buttons could enhance the user experience!Detailing FAQ accordion functionality
I'm outlining the CSS for the FAQ accordion. I'll set up smooth transitions for both expanding and collapsing questions using properties like max-height. When a question is opened, I'll ensure the icon changes to a minus sign. I want to address edge cases too: if a user resizes the window while an answer is open, the assigned max-height could be too small. I might suggest adding a resize event listener to recalculate the height. Overall, I’ll provide a concise code snippet for the user’s request for an animated accordion FAQ section.Finalizing the FAQ section
I'm preparing to create a single HTML file for the FAQ section. The instructions will be simple: users can copy the code into a .html file and open it in a browser. I’ll also include a note about how to customize the questions and answers. Additionally, I might mention that they can enable an accordion behavior, allowing only one question to be open at a time by uncommenting a section. It’s important to ensure the code maintains accessible semantics! Below is a complete, responsive FAQ accordion with smooth expand/collapse using plain HTML, CSS, and JavaScript.
Copy this into an .html file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>FAQ Accordion</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
background: #f4f5f7;
color: #222;
line-height: 1.5;
}
.faq-section {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: flex-start;
padding: clamp(1.5rem, 4vw, 3rem) 1rem;
}
.faq-container {
width: 100%;
max-width: 800px;
margin: 0 auto;
background: #fff;
border-radius: 0.75rem;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
padding: clamp(1.5rem, 3vw, 2.5rem);
}
.faq-header {
margin-bottom: 1.5rem;
text-align: center;
}
.faq-header h2 {
margin: 0 0 0.25rem;
font-size: clamp(1.5rem, 3vw, 2rem);
}
.faq-header p {
margin: 0;
color: #666;
font-size: 0.95rem;
}
.faq-list {
margin-top: 1.5rem;
}
.faq-item {
border-bottom: 1px solid #e0e0e0;
}
.faq-item:last-child {
border-bottom: none;
}
.faq-question {
width: 100%;
padding: 1rem 0;
border: none;
background: transparent;
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
cursor: pointer;
text-align: left;
font: inherit;
color: inherit;
}
.faq-question-text {
flex: 1;
font-weight: 600;
font-size: 0.98rem;
}
.faq-icon {
flex-shrink: 0;
width: 1.5rem;
height: 1.5rem;
border-radius: 999px;
border: 1px solid #ccc;
display: flex;
align-items: center;
justify-content: center;
font-size: 1rem;
color: #555;
transition: transform 0.25s ease, background 0.25s ease, color 0.25s ease, border-color 0.25s ease;
}
.faq-question[aria-expanded="true"] .faq-icon {
transform: rotate(90deg);
background: #2563eb;
border-color: #2563eb;
color: #fff;
}
.faq-icon::before {
content: "+";
display: inline-block;
}
.faq-answer {
overflow: hidden;
max-height: 0;
transition: max-height 0.3s ease;
}
.faq-answer-inner {
padding: 0 0 1rem;
padding-right: 2.4rem; /* leave room for icon */
font-size: 0.94rem;
color: #555;
}
.faq-answer-inner p {
margin: 0.35rem 0;
}
.faq-question:focus-visible {
outline: 2px solid #2563eb;
outline-offset: 2px;
}
@media (max-width: 600px) {
.faq-container {
padding: 1.25rem 1rem;
}
.faq-answer-inner {
padding-right: 0;
}
}
</style>
</head>
<body>
<section class="faq-section">
<div class="faq-container" aria-label="Frequently Asked Questions">
<header class="faq-header">
<h2>Frequently Asked Questions</h2>
<p>Click a question to see the answer.</p>
</header>
<div class="faq-list">
<!-- FAQ Item 1 -->
<article class="faq-item">
<button class="faq-question" aria-expanded="false">
<span class="faq-question-text">What is this FAQ accordion?</span>
<span class="faq-icon" aria-hidden="true"></span>
</button>
<div class="faq-answer" aria-hidden="true">
<div class="faq-answer-inner">
<p>
This is a responsive FAQ accordion built with HTML, CSS, and JavaScript.
Click on any question to smoothly expand or collapse the answer.
</p>
</div>
</div>
</article>
<!-- FAQ Item 2 -->
<article class="faq-item">
<button class="faq-question" aria-expanded="false">
<span class="faq-question-text">Can I have multiple questions open at once?</span>
<span class="faq-icon" aria-hidden="true"></span>
</button>
<div class="faq-answer" aria-hidden="true">
<div class="faq-answer-inner">
<p>
Yes. By default, multiple questions can be open at the same time.
You can easily switch to “true accordion” behavior in the JavaScript
so only one item is open at once.
</p>
</div>
</div>
</article>
<!-- FAQ Item 3 -->
<article class="faq-item">
<button class="faq-question" aria-expanded="false">
<span class="faq-question-text">Is it responsive on mobile devices?</span>
<span class="faq-icon" aria-hidden="true"></span>
</button>
<div class="faq-answer" aria-hidden="true">
<div class="faq-answer-inner">
<p>
The layout uses fluid widths and modern CSS so it adapts to phones,
tablets, and desktops without needing extra JavaScript.
</p>
</div>
</div>
</article>
<!-- FAQ Item 4 -->
<article class="faq-item">
<button class="faq-question" aria-expanded="false">
<span class="faq-question-text">How do I customize the questions and answers?</span>
<span class="faq-icon" aria-hidden="true"></span>
</button>
<div class="faq-answer" aria-hidden="true">
<div class="faq-answer-inner">
<p>
Duplicate any <code><article class="faq-item">...</article></code> block
and edit the text inside the <code>.faq-question-text</code> and
<code>.faq-answer-inner</code> elements.
</p>
</div>
</div>
</article>
</div>
</div>
</section>
<script>
const faqItems = document.querySelectorAll(".faq-item");
faqItems.forEach((item) => {
const button = item.querySelector(".faq-question");
const answer = item.querySelector(".faq-answer");
// Ensure closed by default
answer.style.maxHeight = "0px";
button.addEventListener("click", () => {
const isExpanded = button.getAttribute("aria-expanded") === "true";
// OPTIONAL: Uncomment this block if you want ONLY ONE item open at a time.
/*
faqItems.forEach((otherItem) => {
if (otherItem !== item) {
const otherButton = otherItem.querySelector(".faq-question");
const otherAnswer = otherItem.querySelector(".faq-answer");
otherButton.setAttribute("aria-expanded", "false");
otherAnswer.setAttribute("aria-hidden", "true");
otherAnswer.style.maxHeight = "0px";
}
});
*/
if (isExpanded) {
// Collapse
button.setAttribute("aria-expanded", "false");
answer.setAttribute("aria-hidden", "true");
answer.style.maxHeight = "0px";
} else {
// Expand
button.setAttribute("aria-expanded", "true");
answer.setAttribute("aria-hidden", "false");
answer.style.maxHeight = answer.scrollHeight + "px";
}
});
});
// Keep open panels sized correctly on window resize
window.addEventListener("resize", () => {
faqItems.forEach((item) => {
const button = item.querySelector(".faq-question");
const answer = item.querySelector(".faq-answer");
const isExpanded = button.getAttribute("aria-expanded") === "true";
if (isExpanded) {
answer.style.maxHeight = answer.scrollHeight + "px";
}
});
});
</script>
</body>
</html>
To adapt it:
- Change titles and text inside each
.faq-item.
- Adjust colors, border-radius, etc., in the CSS to match your design.
- Uncomment the “ONLY ONE item open” block in the JS if you want strict accordion behavior.