Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Unlock ! How To Create an Accordion in 3 Simple Steps for Stunning Code!" - Image 12

Unlock ! How To Create an Accordion in 3 Simple Steps for Stunning Code!”

Posted on October 6, 2023December 31, 2024 By Updategadh No Comments on Unlock ! How To Create an Accordion in 3 Simple Steps for Stunning Code!”

Discover How To Create an Accordion . Accordion. When you need to flip between hiding and presenting a huge quantity of data, accordions come in handy.

How To Create an Accordion

In this tutorial, you will learn the following:

  1. How to structure HTML elements to create an accordion.
  2. Applying CSS styles to customize the accordion’s appearance.
  3. Using CSS transitions and animations to create smooth accordion animations.
  4. Enhancing the accordion with additional features like icons and hover effects.

By the end of this tutorial, you’ll have a fully functional accordion that you can customize and integrate into your own web projects.

How To Create an Accordion
How To Create an Accordion

Table of Contents

  • How To Create an Accordion
  • In this tutorial, you will learn the following:
  • Project Folder Structure (How To Create an Accordion):
  • HTML:
  • CSS:
  • Conclusion:
  • How To Create an Accordion
    • Step 1) Add HTML:
    • Step 2) Add CSS:
    • Step 3) Add JavaScript
    • (How To Create an Accordion) DEMO
  • Source Code [How To Create an Accordion] :
    • Related Pages :-
    • https://updategadh.com/project-for-beginners-and-advanced/
    • https://updategadh.com/category/java-project/
    • https://updategadh.com/category/php-project/
    • Related Articles :-

Project Folder Structure (How To Create an Accordion):

To create an accordion and practice along with the tutorial, make sure you have the following project folder structure:

  • index.html: This file will contain the HTML markup.
  • styles.css: This file will hold the CSS styles.

You have the flexibility to use different file names or organization methods that suit your workflow.

How To Create an Accordion
How To Create an Accordion

HTML:

  1. Accordion Container:
    • The accordion container is wrapped in a <div> element with the class name “accordion”. This element holds all the accordion items.
  • Accordion Items:
    • Each accordion item is represented by a <div> element with the class name “accordion-item”.
    • Inside each accordion item, we have:
      • An <input> element with the attribute type=”checkbox” and a unique ID. This checkbox will control the visibility of the content section.
      • A <label> element with the class name “accordion-header” and the for attribute referencing the corresponding checkbox ID. This label acts as the header for the accordion item.
      • Inside the label, we have two spans:
        • The first span contains the text for the accordion item, e.g., “Accordion Item 1”.
        • The second span contains an <i> element with the class “fa-solid fa-caret-right” from the Font Awesome library. This creates an arrow icon next to the text.
  • Accordion Content:
    • Inside each accordion item, after the label, we have a <div> element with the class name “accordion-content”. This element holds the content that will expand and collapse.
    • The content can be any HTML elements, such as paragraphs, images, lists, etc. In this tutorial, we have a simple paragraph for demonstration purposes.
How To Create an Accordion
How To Create an Accordion

CSS:

  1. Global Styles:
    • The * selector is used to apply certain styles to all elements, such as setting box-sizing to border-box and using the “Poppins” font family.
    • The body has a background color of #0083e9, which sets the overall background color for the page.
  • Accordion Styles:
    • The “accordion” class sets the width, position, and centering of the accordion container using absolute positioning and translation techniques.
  • Accordion Item Styles:
    • The “accordion-item” class defines the padding, background color, and border-radius for each accordion item. The margin-bottom property adds some space between the accordion items.
  • Accordion Header Styles:
    • The “accordion-header” class defines the styles for the label element, which serves as the header for each accordion item.
    • The font-size, color, and padding properties are set to customize the appearance of the accordion header.
    • The cursor: pointer property changes the cursor to a pointer when hovering over the header.
    • Inside the label, the arrow icon is styled by using the .arrow class and transforming it using the rotate property to create the animation when the accordion item is expanded.
  • Accordion Content Styles:
    • The “accordion-content” class sets the color, font-size, and line-height properties for the content section.
    • The max-height: 0 and overflow: hidden properties hide the content initially.
    • The transition property is used to create a smooth animation effect when the content section expands and collapses. The max-height property is animated over a 1s duration.
How To Create an Accordion
How To Create an Accordion

Conclusion:

  • Accordions are an excellent way to present and organize content on a webpage.
  • With HTML, CSS you can create interactive and visually appealing accordions that enhance the user experience.
  • In this tutorial, we covered the basics of creating an accordion using HTML and CSS.
  • By following along with the video tutorial and understanding the concepts explained here, you’ll be able to build your own accordions and customize them to fit your project’s needs.
  • Feel free to experiment with different styles, animations, and functionality to create unique and engaging accordions for your websites.
  • Remember to practice and apply what you’ve learned to solidify your understanding.
  • Now, go ahead and start creating amazing accordions for your web projects!

How To Create an Accordion

Step 1) Add HTML:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>

<h2>Accordion with symbols</h2>
<p>In this example we have added a "plus" sign to each button. When the user clicks on the button, the "plus" sign is replaced with a "minus" sign.</p>
<button class="accordion">Section 1</button>
<div class="panel">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<button class="accordion">Section 2</button>
<div class="panel">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<button class="accordion">Section 3</button>
<div class="panel">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>


</body>
</html>
Step 2) Add CSS:

Style the accordion:

<style>
.accordion {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
  transition: 0.4s;
}

.active, .accordion:hover {
  background-color: #ccc;
}

.accordion:after {
  content: '\002B';
  color: #777;
  font-weight: bold;
  float: right;
  margin-left: 5px;
}

.active:after {
  content: "\2212";
}

.panel {
  padding: 0 18px;
  background-color: white;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}
</style>
Step 3) Add JavaScript:
<script>
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    } 
  });
}
</script>

(How To Create an Accordion) DEMO

Collapsible/Accordion Demo

Accordions are useful when you want to toggle between hiding and showing large amount of content:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.


Source Code [How To Create an Accordion] :

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.accordion {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
  transition: 0.4s;
}

.active, .accordion:hover {
  background-color: #ccc;
}

.accordion:after {
  content: '\002B';
  color: #777;
  font-weight: bold;
  float: right;
  margin-left: 5px;
}

.active:after {
  content: "\2212";
}

.panel {
  padding: 0 18px;
  background-color: white;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}
</style>
</head>
<body>

<h2>Accordion with symbols</h2>
<p>In this example we have added a "plus" sign to each button. When the user clicks on the button, the "plus" sign is replaced with a "minus" sign.</p>
<button class="accordion">Section 1</button>
<div class="panel">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<button class="accordion">Section 2</button>
<div class="panel">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<button class="accordion">Section 3</button>
<div class="panel">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<script>
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    } 
  });
}
</script>

</body>
</html>

Related Pages :-

https://updategadh.com/project-for-beginners-and-advanced/

https://updategadh.com/category/java-project/

https://updategadh.com/category/php-project/

Related Articles :-

https://updategadh.com/tag/how-to-design-digital-clock-using-javascript/

YouTube Playlist For Projects :- https://www.youtube.com/@Decodeit./playlists

https://www.javascript.com/

https://updategadh.com/javascript-project/how-to-design-digital-clock-using-javascript/#respond
https://updategadh.com/javascript/javascript-complete-course-for-beginners
https://updategadh.com/javascript-project/how-to-create-an-icon-bar-with-html-css/#respond
Post Views: 931
How to Tags:Collage Project, JavaScript

Post navigation

Previous Post: How To Create an Icon Bar with Html CSS
Next Post: Top 5 YouTube Channels for Python to Guide You

More Related Articles

What is an API and How to Build What is an API and How to Build API How to
How to Get Started with Ethical Hacking and Cybersecurity How to Get Started with Ethical Hacking and Cybersecurity How to
9 Mind-Blowing Mini Projects in Java How to Java web project configuration How to

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. Integrating AI and Machine into Your College Project : Comprehensive Guide
  2. Top 5 High-Demand IT Jobs for Fresh
  3. The Impact of Automation on IT Jobs: Navigating the Future of Work
  4. How to Use LeetCode and HackerRank for Interview Preparation
  5. What is an API and How to Build API
  6. What is SQL? How to use for Data

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Blog Site In PHP And MYSQL With Source Code || Best Project
  9. Online Bike Rental Management System Using PHP and MySQL
  10. E learning Website in php with Free source code
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Real-Time Medical Queue & Appointment System with Django
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code

Most Viewed Posts

  • Top Large Language Models in 2025 (8,616)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,227)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,875)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme