FAQ Collapse with JavaScript, HTML, and CSS

Every website needs an FAQ (Frequently Asked Questions) section to address common customer inquiries efficiently. It's a crucial element for improving user experience and offering instant support. In this tutorial, we'll walk through creating a dynamic and stylish FAQ section that users can interact with to find the answers they need. This implementation uses JavaScript, HTML, and CSS, incorporating sleek animations and a modern design.

Project Setup

Create a Project Directory: Name it faq-section. This will organize your project files.

File Structure:
  • index.html: Contains the HTML markup for the application.
  • style.css: Styles the application.
  • script.js: Contains the logic of the FAQ section.

Development Steps

1. HTML Markup (index.html)

The HTML structure is straightforward. It consists of a main container that holds multiple FAQ items. Each FAQ item contains a question (.faq-title), an answer (.faq-text), and a toggle button to show or hide the answer.

Let's open the index.html file and add the following code to it:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" integrity="sha512-1PKOgIY59xJ8Co8+NE6FZ+LOAZKjy+KY8iq0G4B3CyeY6wYHN3yt9PW0XpSriVlkMXe40PTKnXrLnZ9+fkDaog==" crossorigin="anonymous" />
    <link rel="stylesheet" href="style.css" />
    <title>FAQ</title>
  </head>
  <body>
    <h1>Frequently Asked Questions</h1>
    <div class="faq-container">
      <div class="faq active">
        <h3 class="faq-title">
          Why shouldn't we trust atoms?
        </h3>

        <p class="faq-text">
          They make up everything
        </p>

        <button class="faq-toggle">
          <i class="fas fa-chevron-down"></i>
          <i class="fas fa-times"></i>
        </button>
      </div>

      <div class="faq">
        <h3 class="faq-title">
          What do you call someone with no body and no nose?
        </h3>
        <p class="faq-text">
          Nobody knows.
        </p>
        <button class="faq-toggle">
          <i class="fas fa-chevron-down"></i>
          <i class="fas fa-times"></i>
        </button>
      </div>

      <div class="faq">
        <h3 class="faq-title">
          What's the object-oriented way to become wealthy?
        </h3>
        <p class="faq-text">
          Inheritance.
        </p>
        <button class="faq-toggle">
          <i class="fas fa-chevron-down"></i>
          <i class="fas fa-times"></i>
        </button>
      </div>

      <div class="faq">
        <h3 class="faq-title">
          How many tickles does it take to tickle an octopus?
        </h3>
        <p class="faq-text">
          Ten-tickles!
        </p>
        <button class="faq-toggle">
          <i class="fas fa-chevron-down"></i>
          <i class="fas fa-times"></i>
        </button>
      </div>

      <div class="faq">
        <h3 class="faq-title">
          What is: 1 + 1?
        </h3>
        <p class="faq-text">
          Depends on who are you asking.
        </p>
        <button class="faq-toggle">
          <i class="fas fa-chevron-down"></i>
          <i class="fas fa-times"></i>
        </button>
      </div>
    </div>
    <script src="script.js"></script>
  </body>
</html>
This structure is replicated for each FAQ item, making it easy to add or remove questions and answers. 

2. style.css - CSS Styling 

The CSS gives life to the FAQ section, making it visually appealing and engaging. We use the Muli font for a clean look, apply a soft background color to the body, and style each FAQ item with rounded borders and padding for a modern touch. The toggle button transitions and icons (from Font Awesome) enhance user interaction, indicating whether an answer is visible.

Let's open the style.css file and add the following CSS code to it:  
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');

* {
  box-sizing: border-box;
}

body {
  font-family: 'Muli', sans-serif;
  background-color: #f0f0f0;
}

h1 {
  margin: 50px 0 30px;
  text-align: center;
}

.faq-container {
  max-width: 600px;
  margin: 0 auto;
}

.faq {
  background-color: transparent;
  border: 1px solid #9fa4a8;
  border-radius: 10px;
  margin: 20px 0;
  padding: 30px;
  position: relative;
  overflow: hidden;
  transition: 0.3s ease;
}

.faq.active {
  background-color: #fff;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1), 0 3px 6px rgba(0, 0, 0, 0.1);
}

.faq.active::before,
.faq.active::after {
  content: '\f075';
  font-family: 'Font Awesome 5 Free';
  color: #2ecc71;
  font-size: 7rem;
  position: absolute;
  opacity: 0.2;
  top: 20px;
  left: 20px;
  z-index: 0;
}

.faq.active::before {
  color: #3498db;
  top: -10px;
  left: -30px;
  transform: rotateY(180deg);
}

.faq-title {
  margin: 0 35px 0 0;
}

.faq-text {
  display: none;
  margin: 30px 0 0;
}

.faq.active .faq-text {
  display: block;
}

.faq-toggle {
  background-color: transparent;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  padding: 0;
  position: absolute;
  top: 30px;
  right: 30px;
  height: 30px;
  width: 30px;
}

.faq-toggle:focus {
  outline: 0;
}

.faq-toggle .fa-times {
  display: none;
}

.faq.active .faq-toggle .fa-times {
  color: #fff;
  display: block;
}

.faq.active .faq-toggle .fa-chevron-down {
  display: none;
}

.faq.active .faq-toggle {
  background-color: #9fa4a8;
}
The .active class modifies the FAQ item's appearance when it's expanded, providing a visual cue to the user. 

3. script.js - JavaScript Functionality 

JavaScript adds the interactive functionality to the FAQ section. It listens for clicks on the toggle button, then adds or removes the .active class to the parent FAQ item accordingly. This class toggle shows or hides the answer and changes the icon in the toggle button.
const toggles = document.querySelectorAll('.faq-toggle')

toggles.forEach(toggle => {
    toggle.addEventListener('click', () => {
        toggle.parentNode.classList.toggle('active')
    })
})
This script is simple yet powerful, providing an intuitive experience for users to interact with the FAQ section. 

Open index.html in Browser 

Let's open the index.html file in the browser, and you will be able to see the following screen: 
FAQ Collapse with JavaScript, HTML, and CSS

Conclusion 

An interactive FAQ section is essential for any website aiming to improve its user experience. By following this tutorial, you've learned how to create a dynamic FAQ section that not only looks good but is also functional. 

The combination of HTML, CSS, and JavaScript in this project demonstrates how different web technologies can work together to create engaging and interactive web components. This FAQ collapse functionality can be easily integrated into any website, providing immediate answers to common questions in a visually appealing format. 

Feel free to customize the styles, animations, and behavior to match your site's design and user experience goals. Happy coding!

Comments