This code creates an accordion in Vanilla JavaScript with plus and minus icons. The accordion expands or collapses its content when clicked. It helps organize and display content in a collapsible manner.
You can use this code on your website to create interactive FAQ sections or collapsible content panels. It enhances user experience by providing a neat and organized way to present information. Additionally, it improves readability by allowing users to focus on relevant content while hiding less important details.
How to Create Accordion In Vanilla Javascript With Plus Minus
1. First of all, load the Normalize CSS by adding the following CDN link into the head tag of your HTML document.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
2. Set up your HTML file. You’ll need a container for your accordion, along with individual sections for each accordion item. Inside your accordion container, add individual accordion items. Each item consists of a header and content section. Here’s an example of an accordion item:
<div class="app"> <div class="app__head"> <h1>Questions and answers about login</h1> </div> <div class="app__content"> <div class="accordion-container"> <div class="accordion "> <header class="accordion__header"> <p> <button> <span class="accordion__title">Do I have to allow the use of cookies?</span> <div class="accordion__icon"> <svg class="icon__plus" xmlns="http://www.w3.org/2000/svg" width="40" height="40"> <g fill="none" fill-rule="evenodd"> <circle cx="20" cy="20" r="20" fill="#E5DDE7" opacity=".497" /> <path d="M8 8h24v24H8z" /> <path stroke="#DC094E" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M20 13v14m-7-7h14" /> </g> </svg> <svg class="icon__minus" xmlns="http://www.w3.org/2000/svg" width="40" height="40"> <g fill="none" fill-rule="evenodd"> <circle cx="20" cy="20" r="20" fill="#E5DDE7" opacity=".497" /> <path stroke="#DC094E" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.929" d="M11 19.5h18" /> </g> </svg> </div> </button> </p> </header> <div class="accordion__content"> <div class="accordion__inner"> <p> Yes, in order to use My Page, you must allow the use of cookies in your browser. <p> See also: What is a cookie and what does it do? </p> </p> </div> </div> </div> <div class="accordion"> <header class="accordion__header"> <p> <button> <span class="accordion__title">How do I change my My Page Password?</span> <div class="accordion__icon"> <svg class="icon__plus" xmlns="http://www.w3.org/2000/svg" width="40" height="40"> <g fill="none" fill-rule="evenodd"> <circle cx="20" cy="20" r="20" fill="#E5DDE7" opacity=".497" /> <path d="M8 8h24v24H8z" /> <path stroke="#DC094E" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M20 13v14m-7-7h14" /> </g> </svg> <svg class="icon__minus" xmlns="http://www.w3.org/2000/svg" width="40" height="40"> <g fill="none" fill-rule="evenodd"> <circle cx="20" cy="20" r="20" fill="#E5DDE7" opacity=".497" /> <path stroke="#DC094E" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.929" d="M11 19.5h18" /> </g> </svg> </div> </button> </p> </header> <div class="accordion__content "> <div class="accordion__inner"> <p> Get to setting > password and change your old password then click save button </p> </div> </div> </div> <div class="accordion"> <header class="accordion__header"> <p> <button> <span class="accordion__title">What is BankID?</span> <div class="accordion__icon"> <svg class="icon__plus" xmlns="http://www.w3.org/2000/svg" width="40" height="40"> <g fill="none" fill-rule="evenodd"> <circle cx="20" cy="20" r="20" fill="#E5DDE7" opacity=".497" /> <path d="M8 8h24v24H8z" /> <path stroke="#DC094E" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M20 13v14m-7-7h14" /> </g> </svg> <svg class="icon__minus" xmlns="http://www.w3.org/2000/svg" width="40" height="40"> <g fill="none" fill-rule="evenodd"> <circle cx="20" cy="20" r="20" fill="#E5DDE7" opacity=".497" /> <path stroke="#DC094E" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.929" d="M11 19.5h18" /> </g> </svg> </div> </button> </p> </header> <div class="accordion__content"> <div class="accordion__inner"> <p> The BankID is your number account id used to login </p> </div> </div> </div> <div class="accordion"> <header class="accordion__header"> <p> <button> <span class="accordion__title">Whose birth number can I use?</span> <div class="accordion__icon"> <svg class="icon__plus" xmlns="http://www.w3.org/2000/svg" width="40" height="40"> <g fill="none" fill-rule="evenodd"> <circle cx="20" cy="20" r="20" fill="#E5DDE7" opacity=".497" /> <path d="M8 8h24v24H8z" /> <path stroke="#DC094E" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M20 13v14m-7-7h14" /> </g> </svg> <svg class="icon__minus" xmlns="http://www.w3.org/2000/svg" width="40" height="40"> <g fill="none" fill-rule="evenodd"> <circle cx="20" cy="20" r="20" fill="#E5DDE7" opacity=".497" /> <path stroke="#DC094E" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.929" d="M11 19.5h18" /> </g> </svg> </div> </button> </p> </header> <div class="accordion__content"> <div class="accordion__inner"> <p> You can choose from numbers and letters. Must be 8 characters long ! </p> </div> </div> </div> <div class="accordion"> <header class="accordion__header"> <p> <button> <span class="accordion__title">When do I receive a password ordered by letter?</span> <div class="accordion__icon"> <svg class="icon__plus" xmlns="http://www.w3.org/2000/svg" width="40" height="40"> <g fill="none" fill-rule="evenodd"> <circle cx="20" cy="20" r="20" fill="#E5DDE7" opacity=".497" /> <path d="M8 8h24v24H8z" /> <path stroke="#DC094E" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M20 13v14m-7-7h14" /> </g> </svg> <svg class="icon__minus" xmlns="http://www.w3.org/2000/svg" width="40" height="40"> <g fill="none" fill-rule="evenodd"> <circle cx="20" cy="20" r="20" fill="#E5DDE7" opacity=".497" /> <path stroke="#DC094E" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.929" d="M11 19.5h18" /> </g> </svg> </div> </button> </p> </header> <div class="accordion__content"> <div class="accordion__inner"> <p> You will receive a new password after your registration ! </p> </div> </div> </div> <div class="accordion"> <header class="accordion__header"> <p> <button> <span class="accordion__title">What do I have to do to receive a password via SMS?</span> <div class="accordion__icon"> <svg class="icon__plus" xmlns="http://www.w3.org/2000/svg" width="40" height="40"> <g fill="none" fill-rule="evenodd"> <circle cx="20" cy="20" r="20" fill="#E5DDE7" opacity=".497" /> <path d="M8 8h24v24H8z" /> <path stroke="#DC094E" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M20 13v14m-7-7h14" /> </g> </svg> <svg class="icon__minus" xmlns="http://www.w3.org/2000/svg" width="40" height="40"> <g fill="none" fill-rule="evenodd"> <circle cx="20" cy="20" r="20" fill="#E5DDE7" opacity=".497" /> <path stroke="#DC094E" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.929" d="M11 19.5h18" /> </g> </svg> </div> </button> </p> </header> <div class="accordion__content"> <div class="accordion__inner"> <p> You need to have a valid phone number in your account! </p> </div> </div> </div> </div> </div> </div>
3. You can style your accordion using CSS to achieve the desired look and feel. Here’s a basic example of CSS to get you started:
@import url("https://fonts.googleapis.com/css2?family=Mulish:wght@400;700&display=swap"); :root { --clr-head: hsl(246, 35%, 11%); --clr-body: hsl(248, 35%, 11%); box-sizing: border-box; } *, *::before, *::after { margin: 0; padding: 0; box-sizing: inherit; } html { width: 100%; height: 100%; font-size: 62.5%; } body { font-family: "Mulish", sans-serif; width: 100%; min-height: 720px; font-size: 1.6rem; color: var(--clr-body); display: flex; align-items: center; justify-content: center; background-color: #50155b; } .app { max-width: 66em; width: 100%; background-color: white; border-radius: 10px; position: relative; padding: 3em; display: grid; grid-template-columns: 0.8fr 1fr; gap: 2rem; } .app__head { margin-top: 2em; } .app__head h1 { font-size: clamp(3rem, 5vw, 3.6rem); font-weight: 700; width: 80%; line-height: 1.2; } .accordion { margin-bottom: 0.5em; max-width: 588px; border-radius: 3px; width: 100%; border: 1px solid #e6e6e6; cursor: pointer; } .accordion.is-open { box-shadow: 0 18px 20px -10px rgba(80, 21, 91, 0.32); border-radius: 3px; } .accordion__header button { display: flex; align-items: center; justify-content: space-between; width: 100%; border: 0; background-color: #fff; padding: 2em; text-align: left; outline: none; } .accordion__title { font-size: 1.8rem; /* font-size: clamp(); */ font-weight: 400; } .accordion__icon { cursor: pointer; } .accordion .accordions__icon > * { width: 4rem; } .accordion .icon__plus { display: block; } .icon__minus { display: none; } .accordion.is-open .icon__minus { display: block; } .accordion.is-open .icon__plus { display: none; } .accordion__content { height: 0; overflow: hidden; transition: height 0.3s ease-out; } .accordion__inner { max-width: 80%; width: 100%; padding: 1em 1.5em; } .accordion__inner p { font-size: 1.6rem; line-height: 1.4; margin-bottom: 1em; color: var(--clr-body); opacity: 0.7; } @media (max-width: 768px) { .app { max-width: 90%; margin: 0 auto; display: grid; grid-template-columns: 1fr; gap: 3rem; } .app__head h1 { width: 90%; } .app::before { display: none; } .app::after { display: none; } }
4. Finally, add JavaScript to make your accordion interactive. This script will handle the opening and closing of accordion items when clicked.
// Design by https://uidesigndaily.com/posts/sketch-accordion-website-day-1175 const accordionContainer = document.querySelector(".accordion-container"); // select accordion header accordionContainer.addEventListener("click", e => { const accordionHeader = e.target.closest(".accordion__header"); if (accordionHeader) { const accordion = accordionHeader.parentElement; const accordionContent = accordionHeader.nextElementSibling; const accordionInner = accordionContent.children[0]; let height; if (accordion.classList.contains("is-open")) { height = 0; } else { height = accordionInner.getBoundingClientRect().height; } accordion.classList.toggle("is-open"); accordionContent.style.height = `${height}px`; } });
That’s all! hopefully, you have successfully created Accordion in Vanilla JavaScript with plus minus icons. If you have any questions or suggestions, feel free to comment below.