This FAQ section website tutorial shows you how to build an accessible accordion with a header, six animated questions, and a call to action block. Each answer opens smoothly using the grid-template-rows trick, and the open item switches to a dark background with a plus to minus icon. I also cover accessibility basics like aria-labelledby, :focus-visible, and prefers-reduced-motion.
Final preview:
Good questions. Straight answers.
Everything you need to know before getting started. Can’t find what you’re looking for? We’re just a message away.
Do I need coding experience to follow these tutorials?
No. Every HTML, CSS, and JavaScript tutorial on Coding Yaar is written for beginners, with steps broken down from scratch.
Do the tutorials use any frameworks?
No. Coding Yaar focuses on plain HTML, CSS, and JavaScript, so you understand the fundamentals without relying on a framework.
How often are new tutorials published?
New web development tutorials are added regularly, covering UI components, animations, and beginner JS concepts.
Where can I find the demo or preview before using a component?
Every tutorial includes a live preview so you can see how it looks and behaves before adding it to your site.
Can I use the code in my own projects?
Yes. You’re free to copy, tweak, and use the code from any tutorial in your personal or client projects.
Will the components work on mobile devices?
Yes. Every component is built to be responsive, so it adapts to phones, tablets, and desktops without extra work.
Still have something on your mind?
No question too small. We’re happy to help.
Steps for FAQ Section in Website:
1. Set Up the Section and Base Styles
Link the DM Sans font from Google Font in the head.
<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=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&display=swap" rel="stylesheet">
Let’s start with a section element with the class faq-section. Inside it, I am adding a .section-container div with a .faq-header, a .faq-items-wrapper, and a .faq-cta.
<section class="faq-section" aria-labelledby="faq-title">
<div class="section-container">
<div class="faq-header"></div>
<div class="faq-items-wrapper"></div>
<div class="faq-cta"></div>
</div>
</section>
Then reset the margins and define the colors as CSS variables on :root. Next, give the section a light background, and use clamp() on the padding. Finally, center the container with the margin inline property set to auto and a max width.
* {
margin: 0;
box-sizing: border-box;
}
:root {
--color-background: hsl(60 23% 97%);
--color-surface: hsl(80 19% 94%);
--color-surface-hover: hsl(75 17% 91%);
--opacity: 1;
--color-primary: hsl(139 22% 23% / var(--opacity));
--color-text: hsl(129 9% 15%);
--color-text-muted: hsl(102 5% 39%);
--color-text-inverse: hsl(0 0% 100%);
--color-border: hsl(75 17% 91%);
--color-border-accent: hsl(95 11% 71%);
}
body {
font-family: "DM Sans", sans-serif;
}
.faq-section {
padding: clamp(4.5rem, calc(1rem + 5.56vw), 6rem) clamp(2rem, calc(1rem + 3.33vw), 4rem);
background-color: var(--color-background);
color: var(--color-text);
}
.faq-section .section-container {
max-width: 62rem;
margin-inline: auto;
}
Note:
aria-labelledby. It points to the id of the heading, which is faq-title here. As a result, screen readers announce the section as “Good questions. Straight answers.” and users can jump to it from a landmarks list.
2. Build the Header
Next, fill in .faq-header with a left side for the label and heading, and a right side for the description.
<div class="faq-header">
<div class="faq-header-left">
<span class="section-label">A little more clarity</span>
<h2 id="faq-title">Good questions. Straight answers.</h2>
</div>
<div class="faq-header-right">
<p>Everything you need to know before getting started. Can't find what you're looking for? We're just a message away.</p>
</div>
</div>
Use flexbox with the justify content property set to space-between, so the heading and description sit at opposite ends. Then set the align items property to flex-end, so the description lines up with the bottom of the heading. Since the row can get tight, add flex-wrap: wrap.
.faq-section .faq-header {
display: flex;
justify-content: space-between;
align-items: flex-end;
flex-wrap: wrap;
gap: 1.75rem 4rem;
}
Then style the label, the heading, and the description. The bullet in front of the label comes from ::before, so it stays out of the HTML.
.faq-section .faq-header .section-label {
color: var(--color-primary);
opacity: .8;
font-size: .75rem;
font-weight: 600;
line-height: 1.3;
letter-spacing: 1.8px;
text-transform: uppercase;
}
.faq-section .faq-header .section-label::before {
content: "•";
margin-inline-end: 1ch;
}
.faq-section .faq-header h2 {
max-width: 14ch;
margin-block-start: 1.75rem;
font-size: clamp(2.25rem, calc(1rem + 2.78vw), 3.5rem);
font-weight: 500;
line-height: 1.07;
letter-spacing: -0.0223em;
}
.faq-section .faq-header-right {
max-width: 18rem;
}
.faq-section .faq-header-right p {
color: var(--color-text-muted);
font-size: .875rem;
line-height: 1.7;
}
Note:
ch unit. 1ch is the width of the number 0 in the current font. So max-width: 14ch limits the heading to about 14 characters per line, without a br tag.
Output:
Good questions. Straight answers.
Everything you need to know before getting started. Can’t find what you’re looking for? We’re just a message away.
3. Add the FAQ Items
Now fill in .faq-items-wrapper with six details elements. Each one has a summary for the question, and a .faq-content div for the answer. The .icon span is decorative, so I added aria-hidden=”true” to it.
<div class="faq-items-wrapper">
<details>
<summary>Do I need coding experience to follow these tutorials?<span class="icon" aria-hidden="true"></span></summary>
<div class="faq-content">
<div>
<p>No. Every HTML, CSS, and JavaScript tutorial on Coding Yaar is written for beginners, with steps broken down from scratch.</p>
</div>
</div>
</details>
<!-- add more questions -->
</div>
Add a top margin to the wrapper. Then give each details a surface background, a border, and rounded corners. Since the items stack, add a bottom margin to every item except the last one using :not(:last-child). On hover, switch to the accent border color.
.faq-section .faq-items-wrapper {
margin-block-start: 2.5rem;
}
.faq-section .faq-items-wrapper details {
background-color: var(--color-surface);
border: 1px solid var(--color-border);
border-radius: .5rem;
transition: border-color .15s, background-color .15s, color .15s;
}
.faq-section .faq-items-wrapper details:not(:last-child) {
margin-block-end: .75rem;
}
.faq-section .faq-items-wrapper details:hover {
border-color: var(--color-border-accent);
}
Note:
details and summary. The browser gives you an accordion for free. The summary is the clickable question, and everything else inside details hides until it opens. It is also keyboard friendly by default.
Output:
Good questions. Straight answers.
Everything you need to know before getting started. Can’t find what you’re looking for? We’re just a message away.
Do I need coding experience to follow these tutorials?
No. Every HTML, CSS, and JavaScript tutorial on Coding Yaar is written for beginners, with steps broken down from scratch.
Do the tutorials use any frameworks?
No. Coding Yaar focuses on plain HTML, CSS, and JavaScript, so you understand the fundamentals without relying on a framework.
How often are new tutorials published?
New web development tutorials are added regularly, covering UI components, animations, and beginner JS concepts.
Where can I find the demo or preview before using a component?
Every tutorial includes a live preview so you can see how it looks and behaves before adding it to your site.
Can I use the code in my own projects?
Yes. You’re free to copy, tweak, and use the code from any tutorial in your personal or client projects.
Will the components work on mobile devices?
Yes. Every component is built to be responsive, so it adapts to phones, tablets, and desktops without extra work.
4. Style the Question Row
Use flexbox with the justify content property set to space-between, so the question and the icon sit at opposite ends. Then remove the default triangle using ::-webkit-details-marker. Add a :focus-visible outline, so keyboard users can see which question is selected.
.faq-section .faq-items-wrapper summary {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
padding: 1.625rem 1.75rem 1.25rem;
font-size: 1.063rem;
font-weight: 500;
line-height: 1.5;
cursor: pointer;
}
.faq-section .faq-items-wrapper summary::-webkit-details-marker {
display: none;
}
.faq-section .faq-items-wrapper summary:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 3px;
border-radius: .5rem;
}
Note:
:focus-visible. It shows the outline when someone tabs with the keyboard, and not on every mouse click. I explained this in more detail in my Registration Form post.
Output:
Good questions. Straight answers.
Everything you need to know before getting started. Can’t find what you’re looking for? We’re just a message away.
Do I need coding experience to follow these tutorials?
No. Every HTML, CSS, and JavaScript tutorial on Coding Yaar is written for beginners, with steps broken down from scratch.
Do the tutorials use any frameworks?
No. Coding Yaar focuses on plain HTML, CSS, and JavaScript, so you understand the fundamentals without relying on a framework.
How often are new tutorials published?
New web development tutorials are added regularly, covering UI components, animations, and beginner JS concepts.
Where can I find the demo or preview before using a component?
Every tutorial includes a live preview so you can see how it looks and behaves before adding it to your site.
Can I use the code in my own projects?
Yes. You’re free to copy, tweak, and use the code from any tutorial in your personal or client projects.
Will the components work on mobile devices?
Yes. Every component is built to be responsive, so it adapts to phones, tablets, and desktops without extra work.
5. Draw the Plus Icon
Next, style the .icon span as a circle. Set the position property to relative, so the two bars can be placed inside it using absolute positioning. The ::before pseudo-element draws the horizontal bar, and ::after draws the vertical bar. Together they form a plus. Add flex-shrink: 0 as well, so a long question never squashes the circle.
.faq-section .faq-items-wrapper .icon {
position: relative;
width: 1.5rem;
height: 1.5rem;
border: 1px solid var(--color-border-accent);
border-radius: 50%;
flex-shrink: 0;
}
.faq-section .faq-items-wrapper .icon::before,
.faq-section .faq-items-wrapper .icon::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: var(--color-text);
transition: transform .15s;
}
.faq-section .faq-items-wrapper .icon::before {
width: .625rem;
height: 1px;
}
.faq-section .faq-items-wrapper .icon::after {
width: 1px;
height: .625rem;
}
Output:
Good questions. Straight answers.
Everything you need to know before getting started. Can’t find what you’re looking for? We’re just a message away.
Do I need coding experience to follow these tutorials?
No. Every HTML, CSS, and JavaScript tutorial on Coding Yaar is written for beginners, with steps broken down from scratch.
Do the tutorials use any frameworks?
No. Coding Yaar focuses on plain HTML, CSS, and JavaScript, so you understand the fundamentals without relying on a framework.
How often are new tutorials published?
New web development tutorials are added regularly, covering UI components, animations, and beginner JS concepts.
Where can I find the demo or preview before using a component?
Every tutorial includes a live preview so you can see how it looks and behaves before adding it to your site.
Can I use the code in my own projects?
Yes. You’re free to copy, tweak, and use the code from any tutorial in your personal or client projects.
Will the components work on mobile devices?
Yes. Every component is built to be responsive, so it adapts to phones, tablets, and desktops without extra work.
6. Add the Smooth Answer Panel
Now style .faq-content. I am using the grid-template-rows trick here. It starts at 0fr, so the row has no height, and it grows to 1fr when the item opens. As a result, the height animates smoothly without knowing the exact pixel height. The inner div needs the overflow property set to hidden, so the text is clipped while the row is closed. I covered this trick in my FAQ Section HTML CSS JavaScript post, so check that one out for a deeper look.
.faq-section .faq-content {
display: grid;
grid-template-rows: 0fr;
max-width: 36rem;
padding-inline: 1.75rem;
transition: grid-template-rows .25s;
}
.faq-section .faq-content>div {
overflow: hidden;
opacity: 0;
transition: opacity .15s;
}
.faq-section .faq-content p {
margin-block-end: 1.75rem;
font-size: .938rem;
line-height: 1.85;
}
Note:
fr units in grid-template-rows. Browsers can animate between 0fr and 1fr. Older tricks used max-height, but they needed a guessed number. This one adapts to any amount of text.
7. Style the Open State
Since the layout is done, let’s add the open state. Instead of the default [open] attribute, I am using an is-open class that the JavaScript adds. That way the answer can animate before the browser closes the details element. When an item has is-open, give it the dark primary background and white text. Then open the grid row, fade in the text, rotate the vertical bar to turn the plus into a minus, and flip the focus outline color.
.faq-section .faq-items-wrapper details.is-open {
background-color: var(--color-primary);
color: var(--color-text-inverse);
}
.faq-section .faq-items-wrapper details.is-open .faq-content {
grid-template-rows: 1fr;
}
.faq-section .faq-items-wrapper details.is-open .faq-content>div {
opacity: 1;
}
.faq-section .faq-items-wrapper details.is-open .icon::after {
transform: translate(-50%, -50%) rotate(90deg);
background: var(--color-text-inverse);
}
.faq-section .faq-items-wrapper details.is-open summary:focus-visible {
outline-color: var(--color-text-inverse);
outline-offset: -4px;
}
8. Build the Call to Action Block
After the questions, fill in .faq-cta with a text block and a link with an arrow icon.
<div class="faq-cta">
<div class="faq-cta-text">
<h3>Still have something on your mind?</h3>
<p>No question too small. We're happy to help.</p>
</div>
<a href="#" class="faq-cta-link">
<span class="faq-cta-label">Let's talk</span>
<svg class="faq-cta-arrow" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.25" aria-hidden="true" focusable="false">
<path d="M4.16667 10H15.8333M10.8333 15L15.8333 10L10.8333 5" />
</svg>
</a>
</div>
Add a top border to separate the block from the questions. Use flexbox with the justify content property set to space-between, so the text and link sit at opposite ends. Then add flex-wrap: wrap, since the row stacks on small screens.
.faq-section .faq-cta {
margin-block-start: 2.5rem;
padding-block-start: 2.25rem;
border-block-start: 1px solid var(--color-border-accent);
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 1.25rem 2rem;
h3 {
font-weight: 500;
font-size: 1rem;
line-height: 1.5;
}
p {
margin-block-start: .375rem;
font-size: .875rem;
line-height: 1.4;
color: var(--color-text-muted);
}
}
Then style the link with the primary color and no default underline. Instead, underline only the label, and use text-underline-offset to move the line slightly away from the text. Add a focus outline, so keyboard users can find the link. Finally, slide the arrow to the right when the link is hovered.
.faq-section .faq-cta-link {
display: inline-flex;
align-items: center;
gap: 1rem;
color: var(--color-primary);
font-size: .875rem;
font-weight: 500;
text-decoration: none;
&:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 4px;
border-radius: .25rem;
}
}
.faq-section .faq-cta-label {
text-decoration: underline;
text-underline-offset: .3em;
text-decoration-thickness: 1px;
}
.faq-section .faq-cta-arrow {
transition: transform .2s;
}
.faq-section .faq-cta-link {
&:hover .faq-cta-arrow {
transform: translateX(4px);
}
}
Output:
Good questions. Straight answers.
Everything you need to know before getting started. Can’t find what you’re looking for? We’re just a message away.
Do I need coding experience to follow these tutorials?
No. Every HTML, CSS, and JavaScript tutorial on Coding Yaar is written for beginners, with steps broken down from scratch.
Do the tutorials use any frameworks?
No. Coding Yaar focuses on plain HTML, CSS, and JavaScript, so you understand the fundamentals without relying on a framework.
How often are new tutorials published?
New web development tutorials are added regularly, covering UI components, animations, and beginner JS concepts.
Where can I find the demo or preview before using a component?
Every tutorial includes a live preview so you can see how it looks and behaves before adding it to your site.
Can I use the code in my own projects?
Yes. You’re free to copy, tweak, and use the code from any tutorial in your personal or client projects.
Will the components work on mobile devices?
Yes. Every component is built to be responsive, so it adapts to phones, tablets, and desktops without extra work.
Still have something on your mind?
No question too small. We’re happy to help.
9. Add the Responsive and Reduced Motion Rules
Next, add a smaller padding on tablets and below, and a tighter bottom margin on the answer text. Then respect users who prefer less movement. The prefers-reduced-motion media query turns off the transitions for them.
@media (width < 769px) {
.faq-section {
padding: 4rem 2rem;
}
.faq-section .faq-content p {
margin-block-end: 1.5rem;
}
}
@media (prefers-reduced-motion: reduce) {
.faq-section .faq-items-wrapper details,
.faq-section .faq-content,
.faq-section .faq-content>div,
.faq-section .faq-items-wrapper .icon::before,
.faq-section .faq-items-wrapper .icon::after,
.faq-section .faq-cta-arrow {
transition: none;
}
}
Note:
reduced motion. Some people feel dizzy or distracted by animations, so they turn on a “reduce motion” setting in their device. This query reads that setting, so you can switch the animations off for them.
10. Open and Close the Items with JavaScript
Finally, link script.js before the closing body tag. The accordion logic is similar to this FAQ Section post. The difference is that this version toggles a class instead of writing inline styles.
Wait for the page to load, then select the section, all the details items, and the reduced motion setting. Next, write an openItem function. It sets el.open to true, so the content exists in the page. After two animation frames, it adds the is-open class, so the browser has time to paint the closed state first. As a result, the transition actually plays.
document.addEventListener("DOMContentLoaded", () => {
const section = document.querySelector('.faq-section');
const items = section.querySelectorAll('details');
const reduceMotion = matchMedia('(prefers-reduced-motion: reduce)');
function openItem(el) {
el.open = true;
requestAnimationFrame(() => requestAnimationFrame(() => el.classList.add('is-open')));
}
Then write a closeItem function. It stops early if the item is not open. Otherwise, it removes the is-open class. When reduced motion is on, close the details right away. If not, wait for the grid-template-rows transition to end, and only then set el.open to false. The check on e.target makes sure we react to the answer panel only.
function closeItem(el) {
if (!el.classList.contains('is-open')) return;
el.classList.remove('is-open');
if (reduceMotion.matches) { el.open = false; return; }
const content = el.querySelector('.faq-content');
const onEnd = (e) => {
if (e.target !== content || e.propertyName !== 'grid-template-rows') return;
content.removeEventListener('transitionend', onEnd);
if (!el.classList.contains('is-open')) el.open = false;
};
content.addEventListener('transitionend', onEnd);
}
Last, add a click listener on every summary. Call e.preventDefault(), because the browser would otherwise toggle the details instantly and skip our animation. If the item is already open, close it. If not, close every other item first, and then open this one, so only one answer stays open at a time.
items.forEach((el) => {
el.querySelector('summary').addEventListener('click', (e) => {
e.preventDefault();
if (el.classList.contains('is-open')) {
closeItem(el);
} else {
items.forEach((other) => other !== el && closeItem(other));
openItem(el);
}
});
});
});
Output:
Good questions. Straight answers.
Everything you need to know before getting started. Can’t find what you’re looking for? We’re just a message away.
Do I need coding experience to follow these tutorials?
No. Every HTML, CSS, and JavaScript tutorial on Coding Yaar is written for beginners, with steps broken down from scratch.
Do the tutorials use any frameworks?
No. Coding Yaar focuses on plain HTML, CSS, and JavaScript, so you understand the fundamentals without relying on a framework.
How often are new tutorials published?
New web development tutorials are added regularly, covering UI components, animations, and beginner JS concepts.
Where can I find the demo or preview before using a component?
Every tutorial includes a live preview so you can see how it looks and behaves before adding it to your site.
Can I use the code in my own projects?
Yes. You’re free to copy, tweak, and use the code from any tutorial in your personal or client projects.
Will the components work on mobile devices?
Yes. Every component is built to be responsive, so it adapts to phones, tablets, and desktops without extra work.
Still have something on your mind?
No question too small. We’re happy to help.
Final Source Code for FAQ Section in Website:
HTML
<section class="faq-section" aria-labelledby="faq-title">
<div class="section-container">
<div class="faq-header">
<div class="faq-header-left">
<span class="section-label">A little more clarity</span>
<h2 id="faq-title">Good questions.
Straight answers.</h2>
</div>
<div class="faq-header-right">
<p>Everything you need to know before getting started. Can't find what you're looking for? We're
just a
message away.</p>
</div>
</div>
<div class="faq-items-wrapper">
<details>
<summary>Do I need coding experience to follow these tutorials?<span class="icon"
aria-hidden="true"></span>
</summary>
<div class="faq-content">
<div>
<p>No. Every HTML, CSS, and JavaScript tutorial on Coding Yaar is written for beginners,
with steps broken
down from scratch.</p>
</div>
</div>
</details>
<details>
<summary>Do the tutorials use any frameworks?<span class="icon" aria-hidden="true"></span>
</summary>
<div class="faq-content">
<div>
<p>No. Coding Yaar focuses on plain HTML, CSS, and JavaScript, so you understand the
fundamentals without
relying on a framework.</p>
</div>
</div>
</details>
<details>
<summary>How often are new tutorials published?<span class="icon" aria-hidden="true"></span>
</summary>
<div class="faq-content">
<div>
<p>New web development tutorials are added regularly, covering UI components,
animations,
and beginner JS
concepts.</p>
</div>
</div>
</details>
<details>
<summary>Where can I find the demo or preview before using a component?
<span class="icon" aria-hidden="true"></span>
</summary>
<div class="faq-content">
<div>
<p>Every tutorial includes a live preview so you can see how it looks and behaves before
adding it to your
site.</p>
</div>
</div>
</details>
<details>
<summary>Can I use the code in my own projects?<span class="icon" aria-hidden="true"></span>
</summary>
<div class="faq-content">
<div>
<p>Yes. You're free to copy, tweak, and use the code from any tutorial in your personal or
client projects.</p>
</div>
</div>
</details>
<details>
<summary>Will the components work on mobile devices?<span class="icon" aria-hidden="true"></span>
</summary>
<div class="faq-content">
<div>
<p>Yes. Every component is built to be responsive, so it adapts to phones, tablets, and
desktops without extra work.</p>
</div>
</div>
</details>
</div>
<div class="faq-cta">
<div class="faq-cta-text">
<h3>Still have something on your mind?</h3>
<p>No question too small. We're happy to help.</p>
</div>
<a href="#" class="faq-cta-link">
<span class="faq-cta-label">Let's talk</span>
<svg class="faq-cta-arrow" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor"
stroke-width="1.25" aria-hidden="true" focusable="false">
<path d="M4.16667 10H15.8333M10.8333 15L15.8333 10L10.8333 5" />
</svg>
</a>
</div>
</div>
</section>
CSS
* {
margin: 0;
box-sizing: border-box;
}
:root {
--color-background: hsl(60 23% 97%);
--color-surface: hsl(80 19% 94%);
--color-surface-hover: hsl(75 17% 91%);
--opacity: 1;
--color-primary: hsl(139 22% 23% / var(--opacity));
--color-text: hsl(129 9% 15%);
--color-text-muted: hsl(102 5% 39%);
--color-text-inverse: hsl(0 0% 100%);
--color-border: hsl(75 17% 91%);
--color-border-accent: hsl(95 11% 71%);
}
body {
font-family: "DM Sans", sans-serif;
}
/* FAQ Section */
.faq-section {
padding: clamp(4.5rem, calc(1rem + 5.56vw), 6rem) clamp(2rem, calc(1rem + 3.33vw), 4rem);
background-color: var(--color-background);
color: var(--color-text);
}
.faq-section .section-container {
max-width: 62rem;
margin-inline: auto;
}
/* FAQ Header */
.faq-section .faq-header {
display: flex;
justify-content: space-between;
align-items: flex-end;
flex-wrap: wrap;
gap: 1.75rem 4rem;
}
.faq-section .faq-header .section-label {
color: var(--color-primary);
opacity: .8;
font-size: .75rem;
font-weight: 600;
line-height: 1.3;
letter-spacing: 1.8px;
text-transform: uppercase;
}
.faq-section .faq-header .section-label::before {
content: "•";
margin-inline-end: 1ch;
}
.faq-section .faq-header h2 {
max-width: 14ch;
margin-top: 1.75rem;
font-size: clamp(2.25rem, calc(1rem + 2.78vw), 3.5rem);
font-weight: 500;
line-height: 1.07;
letter-spacing: -0.0223em;
}
.faq-section .faq-header-right {
max-width: 18rem;
}
.faq-section .faq-header-right p {
color: var(--color-text-muted);
font-size: .875rem;
line-height: 1.7;
}
/* FAQ Items */
.faq-section .faq-items-wrapper {
margin-top: 2.5rem;
}
.faq-section .faq-items-wrapper details {
background-color: var(--color-surface);
border: 1px solid var(--color-border);
border-radius: .5rem;
transition: border-color .15s, background-color .15s, color .15s;
}
.faq-section .faq-items-wrapper details:not(:last-child) {
margin-block-end: .75rem;
}
.faq-section .faq-items-wrapper details:hover {
border-color: var(--color-border-accent);
}
/* FAQ Summary */
.faq-section .faq-items-wrapper summary {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
padding: 1.625rem 1.75rem 1.25rem;
font-size: 1.063rem;
font-weight: 500;
line-height: 1.5;
cursor: pointer;
}
.faq-section .faq-items-wrapper summary::-webkit-details-marker {
display: none;
}
.faq-section .faq-items-wrapper summary:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 3px;
border-radius: .5rem;
}
/* Plus/Minus Icon */
.faq-section .faq-items-wrapper .icon {
position: relative;
width: 1.5rem;
height: 1.5rem;
border: 1px solid var(--color-border-accent);
border-radius: 50%;
flex-shrink: 0;
}
.faq-section .faq-items-wrapper .icon::before,
.faq-section .faq-items-wrapper .icon::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: var(--color-text);
transition: transform .15s;
}
.faq-section .faq-items-wrapper .icon::before {
width: .625rem;
height: 1px;
}
.faq-section .faq-items-wrapper .icon::after {
width: 1px;
height: .625rem;
}
/* FAQ Answer */
.faq-section .faq-content {
display: grid;
grid-template-rows: 0fr;
max-width: 36rem;
padding-inline: 1.75rem;
transition: grid-template-rows .25s;
}
.faq-section .faq-content>div {
overflow: hidden;
opacity: 0;
transition: opacity .15s;
}
.faq-section .faq-content p {
margin-block-end: 1.75rem;
font-size: .938rem;
line-height: 1.85;
}
/* Open State */
.faq-section .faq-items-wrapper details.is-open {
background-color: var(--color-primary);
color: var(--color-text-inverse);
}
.faq-section .faq-items-wrapper details.is-open .faq-content {
grid-template-rows: 1fr;
}
.faq-section .faq-items-wrapper details.is-open .faq-content>div {
opacity: 1;
}
.faq-section .faq-items-wrapper details.is-open .icon::after {
transform: translate(-50%, -50%) rotate(90deg);
background: var(--color-text-inverse);
}
.faq-section .faq-items-wrapper details.is-open summary:focus-visible {
outline-color: var(--color-text-inverse);
outline-offset: -4px;
}
.faq-section .faq-cta {
margin-top: 2.5rem;
padding-top: 2.25rem;
border-top: 1px solid var(--color-border-accent);
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 1.25rem 2rem;
h3 {
font-weight: 500;
font-size: 1rem;
line-height: 1.5;
}
p {
margin-top: .375rem;
font-size: .875rem;
line-height: 1.4;
color: var(--color-text-muted);
}
}
.faq-section .faq-cta-link {
display: inline-flex;
align-items: center;
gap: 1rem;
color: var(--color-primary);
font-size: .875rem;
font-weight: 500;
text-decoration: none;
&:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 4px;
border-radius: .25rem;
}
}
.faq-section .faq-cta-label {
text-decoration: underline;
text-underline-offset: .3em;
text-decoration-thickness: 1px;
}
.faq-section .faq-cta-arrow {
transition: transform .2s;
}
.faq-section .faq-cta-link {
&:hover .faq-cta-arrow {
transform: translateX(4px);
}
}
/* Responsive */
@media (width < 769px) {
.faq-section {
padding: 4rem 2rem;
}
.faq-section .faq-content p {
margin-block-end: 1.5rem;
}
}
/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
.faq-section .faq-items-wrapper details,
.faq-section .faq-content,
.faq-section .faq-content>div,
.faq-section .faq-items-wrapper .icon::before,
.faq-section .faq-items-wrapper .icon::after,
.faq-section .faq-cta-arrow {
transition: none;
}
}
JS:
document.addEventListener("DOMContentLoaded", () => {
const section = document.querySelector('.faq-section');
const items = section.querySelectorAll('details');
const reduceMotion = matchMedia('(prefers-reduced-motion: reduce)');
function openItem(el) {
el.open = true;
requestAnimationFrame(() => requestAnimationFrame(() => el.classList.add('is-open')));
}
function closeItem(el) {
if (!el.classList.contains('is-open')) return;
el.classList.remove('is-open');
if (reduceMotion.matches) { el.open = false; return; }
const content = el.querySelector('.faq-content');
const onEnd = (e) => {
if (e.target !== content || e.propertyName !== 'grid-template-rows') return;
content.removeEventListener('transitionend', onEnd);
if (!el.classList.contains('is-open')) el.open = false;
};
content.addEventListener('transitionend', onEnd);
}
items.forEach((el) => {
el.querySelector('summary').addEventListener('click', (e) => {
e.preventDefault();
if (el.classList.contains('is-open')) {
closeItem(el);
} else {
items.forEach((other) => other !== el && closeItem(other));
openItem(el);
}
});
});
});
If you have any doubts or stuck somewhere, you can reach out through Coding Yaar's Discord server.