FAQ Accordion Section Using HTML, CSS & JavaScript

FAQ Accordion Section Using HTML, CSS & JavaScript

FAQ Accordion Section Using HTML, CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make FAQ Accordion which is used in google when we type some question and there are many questions related to it just below the answer. In Today’s session, We will use HTML, CSS, and JavaScript to complete this FAQ Accordion Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make FAQ Accordion Project. Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the FAQ Accordion Project. At last, we will use JS (JavaScript) which will add logic to make the FAQ Accordion Project responsive from the user end.

In this blog post, we will discuss FAQ Accordion Section Using HTML, CSS & JavaScript with complete source code so you can just copy and paste them into your own project. Happy exploring and learning !!

I hope you have got an idea about the project.

HTML Code for Accordion

First, we’ll start with creating the structure of the FAQ Accordion project for that as you can see in the above code we have used all the necessary elements & attributes to set up the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FAQ Accordion</title>
    <!--Font Awesome-->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <!--Google Font-->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="wrapper">
            <button class="toggle">
                What is the name of the company?
                <i class="fas fa-plus icon"></i>
            </button>
            <div class="content">
                <p>The name of the company is Codewithrandom.</p>
            </div>
        </div>
        <div class="wrapper">
            <button class="toggle">
                How do find it on Internet?
                <i class="fas fa-plus icon"></i>
            </button>
            <div class="content">
                <p>Search on google Codewithrandom</p>
            </div>
        </div>
        <div class="wrapper">
            <button class="toggle">
                Does it have a YouTube Channel?
                <i class="fas fa-plus icon"></i>
            </button>
            <div class="content">
                <p>Yes</p>
            </div>
        </div>
    </div>
    <!--Script-->
    <script src="script.js"></script>
</body>
</html>

Chatbot Template UI Design Using HTML, CSS, and JavaScript

CSS Code for Accordion

Second, comes the CSS code which we have styled for the structure we have padded as well as aligned the FAQ Accordion project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now, let’s code the JavaScript part to make it responsive.

*,
*:before,
*:after{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
body{
    background-color: rgb(16,120,108);
}
.container{
    width: 45%;
    min-width: 500px;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
}
.wrapper{
    background-color: #ffffff;
    margin-bottom: 20px;
    padding: 15px 40px;
    border-radius: 5px;
    box-shadow: 0 15px 25px rgba(0,0,50,0.2);
}
.toggle,
.content{
    font-family: "Poppins",sans-serif;
}
.toggle{
    width: 100%;
    background-color: transparent;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 16px;
    color: #111130;
    font-weight: 500;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 15px 0;
}
.content{
    position: relative;
    font-size: 14px;
    text-align: justify;
    line-height: 30px;
    height: 0;
    overflow: hidden;
    transition: all 1s;
}

Typing with Typo Correction and Pause Css | Text Typing Effect Html

JavaScript Code for Accordion

Last stage of the project the JavaScript which we added the logic and coded as per the requirement with some conditions. Also, we have created the functions in which the answers are embedded in it and they will appear after the user action. Let us see the Final Output of the project FAQ Accordion using HTML, CSS & JavaScript (Source Code).

let toggles = document.getElementsByClassName('toggle');
let contentDiv = document.getElementsByClassName('content');
let icons = document.getElementsByClassName('icon');

for(let i=0; i<toggles.length; i++){
    toggles[i].addEventListener('click', ()=>{
        if( parseInt(contentDiv[i].style.height) != contentDiv[i].scrollHeight){
            contentDiv[i].style.height = contentDiv[i].scrollHeight + "px";
            toggles[i].style.color = "#0084e9";
            icons[i].classList.remove('fa-plus');
            icons[i].classList.add('fa-minus');
        }
        else{
            contentDiv[i].style.height = "0px";
            toggles[i].style.color = "#111130";
            icons[i].classList.remove('fa-minus');
            icons[i].classList.add('fa-plus');
        }

        for(let j=0; j<contentDiv.length; j++){
            if(j!==i){
                contentDiv[j].style.height = "0px";
                toggles[j].style.color = "#111130";
                icons[j].classList.remove('fa-minus');
                icons[j].classList.add('fa-plus');
            }
        }
    });
}

Final Output


We have successfully created our FAQ Accordion using HTML, CSS & JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Thank You And Happy Learning!!!

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply