Bootstrap uses the data-bs-parent attribute on collapse elements to group them. This attribute points to the accordion container, so when one panel opens, others close automatically.
Option C uses the correct Bootstrap 5 attributes: data-bs-toggle and data-bs-target, proper element nesting, and the show class on the open panel.
<div class="accordion" id="accordionExample"> <div class="accordion-item"> <h2 class="accordion-header" id="headingOne"> <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne"> Item 1 </button> </h2> <div id="collapseOne" class="accordion-collapse collapse" aria-labelledby="headingOne" data-bs-parent="#accordionExample"> <div class="accordion-body"> Content 1 </div> </div> </div> <div class="accordion-item"> <h2 class="accordion-header" id="headingTwo"> <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="true" aria-controls="collapseTwo"> Item 2 </button> </h2> <div id="collapseTwo" class="accordion-collapse collapse show" aria-labelledby="headingTwo" data-bs-parent="#accordionExample"> <div class="accordion-body"> Content 2 </div> </div> </div> </div>
show class and aria-expanded values.The second item has the show class on its collapse div and aria-expanded="true" on its button, so it is open. The first item is collapsed and closed.
Bootstrap adds the show class to the .accordion-collapse.collapse element when it is open. So selecting .accordion-collapse.collapse.show targets only open panels.
aria-expanded and aria-controls. What is their main purpose?ARIA attributes like aria-expanded tell screen readers if the button controls an open or closed panel. aria-controls links the button to the panel it toggles. This helps users with disabilities understand the accordion's state.