<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">Toggle</button> <div class="collapse collapse-horizontal" id="collapseExample"> <div class="card card-body" style="width: 200px;"> This content collapses horizontally. </div> </div>
The collapse-horizontal class in Bootstrap 5 makes the collapse animation happen horizontally, expanding or collapsing the width of the content smoothly. The button toggles this effect on the target element.
The element with id myCollapse also has the class collapse-horizontal. To select the .card-body inside it, use #myCollapse.collapse-horizontal .card-body. This selects the element with both the id and class, then its descendant .card-body.
The aria-expanded attribute on the toggle button indicates whether the collapsible content is currently expanded (true) or collapsed (false). This helps screen readers understand the state of the content.
<button class="btn btn-primary" data-bs-toggle="collapse">Toggle</button>. What happens when you click it?Without the data-bs-target or href attribute, Bootstrap's collapse plugin does not know which element to toggle, so clicking the button does nothing.
collapse-horizontal, how does the collapsing content affect the surrounding elements' layout differently than the default vertical collapse?Horizontal collapse changes the width of the collapsing element, causing adjacent elements to shift horizontally. Vertical collapse changes height, affecting vertical spacing. This difference impacts how the page layout adjusts during the animation.