Accordion / FAQ with HTML, CSS & JavaScript
30 DAYS 30 PROJECT CHALLENGE
Day #06
Project Overview
An Accordion / FAQ Section built using HTML, CSS, and JavaScript that allows users to expand and collapse content sections smoothly.
The project improves content readability by showing only relevant information at a time, making it ideal for FAQs, help sections, and documentation pages.
Key Features
- Expand and collapse FAQ items with a click.
- Smooth open/close animation for better user experience.
- Allows only one item open at a time for clarity.
- Clear visual indicators for open and closed states.
- Clean, minimal, and responsive design.
- Easy to customize and reuse across projects.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Accordion / FAQ</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="faq-container">
<h2>Frequently Asked Questions</h2>
<div class="faq-item">
<div class="faq-question">What is an accordion?</div>
<div class="faq-answer">
An accordion is a UI component that expands and collapses content sections.
</div>
</div>
<div class="faq-item">
<div class="faq-question">Is this responsive?</div>
<div class="faq-answer">
Yes, this accordion layout is fully responsive on all devices.
</div>
</div>
<div class="faq-item">
<div class="faq-question">Can multiple items be open?</div>
<div class="faq-answer">
No, only one item opens at a time for better readability.
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS Code
* {
box-sizing: border-box;
font-family: "Segoe UI", sans-serif;
}
body {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: #042453;
}
.faq-container {
background: #ffffff;
width: 500px;
padding: 25px;
border-radius: 12px;
box-shadow: 0 15px 30px rgba(0,0,0,0.1);
}
h2 {
text-align: center;
margin-bottom: 20px;
}
.faq-item {
border-bottom: 1px solid #e5e7eb;
}
.faq-question {
padding: 15px;
cursor: pointer;
font-weight: 600;
position: relative;
}
.faq-question::after {
content: "+";
position: absolute;
right: 20px;
font-size: 20px;
}
.faq-item.active .faq-question::after {
content: "−";
}
.faq-answer {
max-height: 0;
overflow: hidden;
padding: 0 15px;
color: #555;
transition: max-height 0.3s ease;
}
.faq-item.active .faq-answer {
max-height: 100px;
padding-bottom: 15px;
}
Javascript Code
const faqItems = document.querySelectorAll(".faq-item");
faqItems.forEach(item => {
const question = item.querySelector(".faq-question");
question.addEventListener("click", () => {
faqItems.forEach(i => {
if (i !== item) {
i.classList.remove("active");
}
});
item.classList.toggle("active");
});
});
Related Projects
Day 13 : Like / Dislike Counter System
Interactive counter system that tracks user likes and dislikes in real time.
Concepts: User interaction handling, state tracking, dynamic count updates, UI feedback.
Day 1 : Login & Signup System
User signup and login system with validated credentials and a clean UI.
Concepts: DOM manipulation, form validation, LocalStorage.
Day 2 : Multi-Step FormĀ
A step-by-step form with progress tracking and input validation.
Concepts: DOM manipulation, form validation, progress tracking.