Core Spark (Azuredevopswiki)

Azuredevopswiki

An Azure DevOps wiki helper

Write a wiki for how to create an accordion in HTML using Bootstrap 5 include code samples


Responses From Spark Variants that Implement the Core Spark (AzureDevOpsWiki)

[[_TOC_]]

# Creating an Accordion in HTML using Bootstrap 5

In this guide, we will walk through the process of creating a responsive accordion using Bootstrap 5. An accordion is a UI component that allows you to show and hide sections of related content on a page.

## Prerequisites

Before you start, ensure you have the following:
- Basic understanding of HTML and CSS.
- Bootstrap 5 included in your project. You can include it via a CDN or by downloading it.

## Step-by-Step Guide

### 1. Include Bootstrap 5

First, include the Bootstrap 5 CSS and JavaScript in your HTML file. You can use the Bootstrap CDN for this.

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bootstrap 5 Accordion</title>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
</body>
</html>
```

### 2. Create the Accordion Structure

Next, create the basic structure of the accordion using HTML. Bootstrap 5 provides a straightforward way to create accordions using its classes.

```html
<div class="accordion" id="accordionExample">
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingOne">
      <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
        Accordion Item #1
      </button>
    </h2>
    <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingTwo">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
        Accordion Item #2
      </button>
    </h2>
    <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingThree">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
        Accordion Item #3
      </button>
    </h2>
    <div id="collapseThree" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions.
      </div>
    </div>
  </div>
</div>
```

### 3. Explanation of Code

- **Accordion Container**: The main container for the accordion is a `<div>` with the class `accordion` and an `id` (e.g., `accordionExample`).
- **Accordion Item**: Each accordion item is wrapped in a `<div>` with the class `accordion-item`.
- **Accordion Header**: The header of the accordion is contained within an `<h2>` tag with the class `accordion-header`. It contains a button that toggles the collapse.
- **Accordion Button**: The button has the class `accordion-button` and attributes `data-bs-toggle="collapse"` and `data-bs-target="#collapseID"` to control the collapse behavior.
- **Accordion Collapse**: The collapsible content is wrapped in a `<div>` with the class `accordion-collapse collapse`. It has an `id` (e.g., `collapseOne`) and attributes `aria-labelledby` and `data-bs-parent` for accessibility and grouping.

### 4. Customizing the Accordion

You can customize the accordion by adding your own styles or modifying the content within the accordion-body. Here is an example of adding custom styles:

```html
<style>
  .accordion-button {
    background-color: #f8f9fa;
    color: #495057;
  }

  .accordion-button:not(.collapsed) {
    background-color: #e9ecef;
    color: #000;
  }
</style>
```

### Conclusion

You have now created a basic accordion using Bootstrap 5. You can further customize it as per your requirements by modifying the HTML and adding custom CSS. Happy coding!