
HTML & CSS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
:root {
--background: #ffffff;
--heading-color: #121212;
--accordion-heading: #282828;
--body-color: #616161;
--border-color: #C7C7C7;
--accodion-gradient: linear-gradient(to bottom, #ffffff33, #99999933);
}
.dark {
--background: #0F0F0F;
--heading-color: #FFFFFF;
--accordion-heading: #D9D9D9;
--body-color: #B8B8B8;
--border-color: #282828;
--accodion-gradient: linear-gradient(to bottom, #6d6d6d33, #00000033);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: var(--background);
font-family: "Poppins", sans-serif;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
margin-top: 100px;
}
h2 {
font-size: 32px;
color: var(--heading-color);
margin-bottom: 24px;
}
.accordion-item {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 10px;
}
.accordion-header {
position: relative;
display: flex;
justify-content: space-between;
align-items: center;
padding: 14px 25px;
border-radius: 10px;
border: 1px solid var(--border-color);
cursor: pointer;
width: 100%;
font-size: 18px;
color: var(--accordion-heading);
}
.accordion-header::after {
content: "";
width: 13px;
height: 13px;
background: url("./icons/open.svg") no-repeat;
}
.dark .accordion-header::after {
background: url("./icons/open-light.svg") no-repeat;
}
.accordion-header[aria-expanded=true]::after {
background: url("./icons/close.svg") no-repeat;
height: 3px;
}
.dark .accordion-header[aria-expanded=true]::after {
background: url("./icons/close-light.svg") no-repeat;
}
.accordion-content {
background: var(--accodion-gradient);
color: var(--body-color);
width: calc(100% + 20px);
padding: 0 34px;
border-radius: 10px;
max-height: 0;
overflow: hidden;
transition: max-height 0.4s, padding .4s, margin 0.4s 0.3s;
box-shadow: inset 0 0 0 1px var(--border-color);
}
.accordion-content[aria-expanded=true] {
transition: max-height 0.4s, padding .4s, margin 0.4s;
margin-top: 10px;
padding: 24px 34px;
max-height: calc(var(--accordion-max-height) + 48px);
}
</style>
<div class="container">
<h2>FAQ</h2>
<div class="accordion">
<div class="accordion-item">
<div class="accordion-header">What is an accordion?</div>
<div class="accordion-content" aria-expanded="false">An accordion is a graphical control element comprising a vertically stacked list of items, such as labels or thumbnails. Each item can be "expanded" or "collapsed" to reveal or hide content associated with that item.</div>
</div>
<div class="accordion-item">
<div class="accordion-header">How does it work?</div>
<div class="accordion-content" aria-expanded="false">When a user clicks on an accordion header, the corresponding content panel expands to show more information, while other panels may collapse to keep the interface clean and organized.</div>
</div>
<div class="accordion-item">
<div class="accordion-header">Where can I use an accordion?</div>
<div class="accordion-content" aria-expanded="false">Accordions are commonly used in FAQs, menus, and any interface where space is limited and information needs to be organized efficiently.</div>
</div>
</div>
</div>
JS Code:
1
2
3
4
5
6
7
8
9
10
11
12
const accordion = document.querySelectorAll(".accordion");
const calculateMaxHeight = (accordionContent) => { return accordionContent.scrollHeight; }
const setAccordionMaxHeight = (accordion) => { const accordionContentElms = accordion.querySelectorAll(".accordion-content");
accordionContentElms.forEach(accordionContentElm => { const height = calculateMaxHeight(accordionContentElm);
accordionContentElm.style.setProperty("--accordion-max-height", `${height}px`); }) }
const toggleAccodion = (accordion, e) => { const currentElement = e.target;
if(currentElement.classList.contains("accordion-header")) { if(currentElement.nextElementSibling.getAttribute("aria-expanded") == "true") { e.target.nextElementSibling.setAttribute("aria-expanded", false); currentElement.setAttribute("aria-expanded", false); } else { const prevOpenedElms = accordion.querySelectorAll("[aria-expanded='true'");
prevOpenedElms.forEach(prevOpenedElm => { prevOpenedElm.setAttribute("aria-expanded", false); });
e.target.nextElementSibling.setAttribute("aria-expanded", true); currentElement.setAttribute("aria-expanded", true); } }; }
const initAccordion = (accordion) => { setAccordionMaxHeight(accordion);
accordion.addEventListener("click", (e) => { toggleAccodion(accordion, e); }); }
document.addEventListener("DOMContentLoaded", () => { accordion.forEach(accordion => initAccordion(accordion)); });