Simple FAQ Accordion With HTML CSS and JavaScript

In this tutorial we will see how to make a Simple FAQ Accordion With HTML, CSS and JavaScript. Pure CSS and Vanilla JavaScript is used to make this FAQ page, CSS code and JavaScript code is given along with the explanation.


Take a look at the HTML code given below

<div class="faq">

 <p class="question">What does HTML mean? <span onclick='faq(this)' class="opener">+</span></p>
 <p class="answer">HTML stands for HyperText Markup Language.</p>

 <p class="question">What does CSS mean? <span onclick='faq(this)' class="opener">+</span></p>
 <p class="answer">CSS stands for Cascading Style Sheets.</p>

 <p class="question">What is JavaScript? <span onclick='faq(this)' class="opener">+</span></p>
 <p class="answer">JavaScript is a client side programming language.</p>  


HTML Div elements and paragraph elements are used to make the basic structure of the FAQ section.

Span tag is used inside the p tag of each question to define the element, which will be used to open and close each individual answer of each question.

HTML onclick attribute is used inside each span tag.

HTML onclick attribute

HTML onclick attribute fires on a mouse click. In this case it will trigger a JavaScript function which will show and hide the answers.

CSS Code

CSS code is given below, In this code CSS class selectors like .faq, .question, .answer and .opener are used to style each HTML element.

Main CSS property used in this code is max-height. max-height of each answer is changed to create an open and close effect.

body {
.faq {
margin: 0 auto;
.faq div {
border: 1px solid #000;
margin: 5px 0px;
.question {
padding: 15px;
.answer {
padding: 0px 15px;
cursor: pointer;

JavaScript Code

Take a look at the Pure JavaScript Code given below, each important part of this code is further explained below.

function faq(ele)
var x =;
if(x=='max-height: 500px;')
{'transform: rotate(0deg);'; = "transition:0.3s;max-height:0px;";    
{'transform: rotate(45deg);'; = "max-height:500px;";

JavaScript this

JavaScript this refers to the object it belongs to. In this case, it refers to the clicked + or x sign, which is used to open or close the answer section of a question.

JavaScript parentElement

JavaScript parentElement returns the parent element of the selected HTML element.

In this case it is used to select the parent p tag which is used to define each question.

JavaScript nextElementSibling

JavaScript nextElementSibling returns the HTML element present in the same tree level, immediately after the selected HTML element.

In this case, it is used to target the answer of each question defined with the help of p tag, which is immediate next sibling of it's relevant question.

JavaScript style Property

JavaScript style Property sets or returns the CSSStyleDeclaration object of the selected HTML element.

In this example, it is used along with cssText Property, to change the height of the answers of the questions.

JavaScript cssText Property

JavaScript cssText Property sets or returns the style declaration of HTML element as a string.


Video Tutorial

Watch our video tutorial on How To Make Simple FAQ Accordion With HTML CSS and JavaScript.

Neumorphic Search Bar Design with HTML and CSS Simple Login Form with Placeholder Animation Pure CSS Button with Folded Corner Background Color Change Slider with HTML CSS and JavaScript 404 Page Not Found Template Design with HTML and CSS Movies Blog Card Design with HTML and CSS Simple CSS Border Animation with before and after Selector Custom Social Select Dropdown Menu with CSS and JavaScript Facebook Offline and Online Notification with CSS and JS New Year 2022 Text Animation with CSS and JS Change Placeholder Color using CSS Custom Select Tag DropDown with CSS and JavaScript Profile Card With Hover Effect using HTML and CSS Simple Price Range Slider With HTML CSS and JavaScript Circle With Two Colors using CSS Card With Ribbon using HTML and CSS Simple Loading Screen using HTML and CSS Disable Textarea Resizable Property using CSS Button like Links using CSS Simple Dropdown Menu Using HTML and CSS Background Color Transition using CSS Highlight Required Input Fields using CSS Disable HTML Link using CSS Difference Between Descendant Selector and Child Selector How To Change Color and Size of Hr Tag Using CSS How To Center Align HTML Table Convert Checkbox To Toggle Button Using CSS Drawing Stars using HTML and CSS Button Bounce Effect using CSS Image as Text Background using CSS background-clip Property Vertically Rotate HTML Element using CSS Vertical Align Middle using CSS Text Over Image Hover Effect - Css Hover Simple CSS Button Hover Effect using Before Selector
Password Show Hide with Eye Icon using JavaScript Change Background Color using Dropdown menu with JavaScript Get Select Tag value onchange in JavaScript Check If Class Exists JavaScript Add Event Listener To all Elements of Class JavaScript getElementsByTagName Event Listener JavaScript Change type attribute of Input Element With JavaScript Password Hide and Show with Toggle Emoji Button using JavaScript Hide and Show Navbar On Scroll With JavaScript Empty an Array in JavaScript Get Last Element Of Array Without Removing It Remove Last Element From Array JavaScript Remove First Element From Array in JavaScript Tic Tac Toe Game in JavaScript Remove Class from HTML Tag OnClick JavaScript JavaScript Select li Tag By Index Display JavaScript Variable In HTML Insert HTML Code anywhere With JavaScript JavaScript Date Difference in Days JavaScript Hide Button After Click Simple Click and Hold Effect Using JavaScript Remove Class From All Elements in JavaScript JavaScript Select All HTML Elements Whose ID Start With Same String