Accordion Component- This is generally used in the FAQ's section of popular websites.
Accordion.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2
?family=Inter:wght@400;500;700&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="./Accordion-styles.css" />
<script defer type="text/javascript" src="./Accordion-script.js"></script>
<title>Accordion Component</title>
</head>
<body>
<div class="accordion">
<div class="item">
<p class="number">01</p>
<p class="text">What are the skills required a web developer?</p>
<div class="icon">
<svg
xmlns="http://www.w3.org/2000/svg"
class="up"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
stroke-width="2"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M5 15l7-7 7 7"
/>
</svg>
<svg
xmlns="http://www.w3.org/2000/svg"
class="down"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
stroke-width="2"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M19 9l-7 7-7-7"
/>
</svg>
</div>
<div class="hidden-box">
<p>
Web development is an awesome field where we can design some cool
web pages with good design, layouts and patterns. The following are
the skills required for a person to be a professional web developer:
</p>
<ul>
<li>HTML5</li>
<li>CSS3</li>
<li>Javascript</li>
</ul>
</div>
</div>
<div class="item">
<p class="number">02</p>
<p class="text">What are the resources to acquire the skills?</p>
<div class="icon">
<svg
xmlns="http://www.w3.org/2000/svg"
class="up"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
stroke-width="2"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M5 15l7-7 7 7"
/>
</svg>
<svg
xmlns="http://www.w3.org/2000/svg"
class="down"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
stroke-width="2"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M19 9l-7 7-7-7"
/>
</svg>
</div>
<div class="hidden-box">
<p>
A lot of content is availbale online to acquire the skills required
for web development. Some of the important ones include:
</p>
<ul>
<li>
<a href="https://web-app-developers.blogspot.com/"
>Blog for Web Developers</a
>
</li>
<li>
<a href="https://developer.mozilla.org/en-US/"
>MDN Documentation</a
>
</li>
<li><a href="https://www.w3schools.com/">W3 Schools</a></li>
</ul>
</div>
</div>
<div class="item">
<p class="number">03</p>
<p class="text">What are the frameworks in javascript?</p>
<div class="icon">
<svg
xmlns="http://www.w3.org/2000/svg"
class="up"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
stroke-width="2"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M5 15l7-7 7 7"
/>
</svg>
<svg
xmlns="http://www.w3.org/2000/svg"
class="down"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
stroke-width="2"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M19 9l-7 7-7-7"
/>
</svg>
</div>
<div class="hidden-box">
<p>
Javascript is a popular language used in the web which makes a
webpage dynamic by responding to user actions. However, most modern
websites use frameworks built on top of javascript for development,
these include:
</p>
<ul>
<li>Angular JS</li>
<li>React JS</li>
<li>Vue JS</li>
</ul>
</div>
</div>
</div>
</body>
</html>
Accordion-styles.css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-size: 62.5%;
}
body {
font-family: "Inter", sans-serif;
color: #343a40;
line-height: 1;
}
.accordion {
width: 130rem;
margin: 4rem auto;
display: flex;
flex-direction: column;
gap: 3.6rem;
}
.item {
box-shadow: 0 0 3.2rem rgba(0, 0, 0, 0.1);
padding: 3.6rem;
display: grid;
grid-template-columns: auto 1fr auto;
column-gap: 3.6rem;
row-gap: 4.8rem;
align-items: center;
font-size: 2.4rem;
}
.number,
.text {
font-weight: 500;
}
.number {
color: #ced4da;
}
.up,
.down {
width: 3.6rem;
height: 3.6rem;
}
.down {
display: none;
}
.hidden-box {
grid-column: 2;
display: none;
}
.hidden-box p {
line-height: 1.6;
margin-bottom: 2.4rem;
}
.hidden-box ul {
color: #adb5bd;
margin-left: 3.2rem;
display: flex;
flex-direction: column;
gap: 1.2rem;
}
a:link,
a:visited {
text-decoration: none;
color: #555;
}
a:hover,
a:active {
color: #087f5b;
}
.open {
border-top: 0.4rem solid #087f5b;
}
.open .hidden-box {
display: block;
}
.open .number,
.open .text {
color: #087f5b;
}
.open .up {
display: none;
}
.open .down {
stroke: #087f5b;
display: block;
}
Accordion-script.js
console.log("Working...");
const accordionEl = document.querySelector(".accordion");
// console.log(accordionEl);
accordionEl.addEventListener("click", (e) => {
e.preventDefault();
const itemEl = e.target.closest(".item");
if (!itemEl) return;
// console.log(itemEl);
if (itemEl.classList.contains("open")) itemEl.classList.remove("open");
else {
const siblings = itemEl.closest(".accordion").
querySelectorAll(".item");
siblings.forEach((el) => {
el.classList.remove("open");
});
itemEl.classList.add("open");
}
});
NOTE: 1. The icon pack used is from heroicons.com.
Result: