---
title: "Accordion with the help of an HTML, CSS and JavaScript"  
description: "Accordion with the help of an HTML, CSS and JavaScript"  
author: "Anonymous User"  
published: 2021-07-20  
updated: 2023-11-28  
canonical: https://www.mindstick.com/forum/156556/accordion-with-the-help-of-an-html-css-and-javascript  
category: "html"  
tags: ["javascript", "html", "css-css3"]  
reading_time: 2 minutes  

---

# Accordion with the help of an HTML, CSS and JavaScript

What is an [Accordion](https://www.mindstick.com/blog/626/accordion-using-bootstrap-in-asp-dot-net)? How to make an Accordion with the help of an [HTML](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript), [CSS](https://www.mindstick.com/forum/514/background-gradient-ie7-css-problem) and [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Aryan Kumar

An accordion is a UI (User Interface) pattern that allows you to collapse and expand content sections, revealing or hiding additional information. It typically consists of a list of items, and each item can be expanded or collapsed independently of the others.

Here's an example of how to create a simple accordion using HTML, CSS, and JavaScript:

```plaintext
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Accordion Example</title>
  <style>
    /* Style for the accordion */
    .accordion {
      display: flex;
      flex-direction: column;
    }

    /* Style for accordion items */
    .accordion-item {
      border: 1px solid #ccc;
      margin-bottom: 5px;
    }

    /* Style for accordion headers (titles) */
    .accordion-header {
      background-color: #f1f1f1;
      padding: 10px;
      cursor: pointer;
    }

    /* Style for accordion content (hidden by default) */
    .accordion-content {
      display: none;
      padding: 10px;
    }

    /* Style for active accordion items */
    .active {
      display: block;
    }
  </style>
</head>
<body>

  <div class="accordion">
    <div class="accordion-item">
      <div class="accordion-header" onclick="toggleAccordion(this)">Section 1</div>
      <div class="accordion-content">
        <p>Content for section 1.</p>
      </div>
    </div>
    <div class="accordion-item">
      <div class="accordion-header" onclick="toggleAccordion(this)">Section 2</div>
      <div class="accordion-content">
        <p>Content for section 2.</p>
      </div>
    </div>
    <!-- Add more sections as needed -->
  </div>

  <script>
    function toggleAccordion(header) {
      var content = header.nextElementSibling;
      content.style.display = content.style.display === "block" ? "none" : "block";
    }
  </script>

</body>
</html>
```

In this example:

- Each accordion item is structured with a header and content.
- Clicking on the header triggers the **toggleAccordion** function, which toggles the display of the associated content.

You can customize the styles, add more sections, and extend the functionality based on your specific requirements. This is a basic example, and more complex accordions can be built using frameworks like Bootstrap or dedicated accordion libraries.


---

Original Source: https://www.mindstick.com/forum/156556/accordion-with-the-help-of-an-html-css-and-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
