Performance: Why expandable content saves space
Expandable content affects page load speed and visual stability by reducing initial content size and layout shifts.
<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"> Expand to read more </button> </h2> <div id="collapseOne" class="accordion-collapse collapse" aria-labelledby="headingOne" data-bs-parent="#accordionExample"> <div class="accordion-body"> Lots of text and images hidden until user expands. </div> </div> </div> </div>
<div class="content">Lots of text and images fully visible on page load without any collapsing or hiding.</div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| All content visible on load | High (many nodes rendered) | 1 large reflow on load | High paint cost | [X] Bad |
| Expandable content collapsed initially | Low (fewer visible nodes) | Reflow only on expand | Lower paint cost initially | [OK] Good |
.collapse to hide or show content when toggled..btn-expand, .expandable-content, and .hidden-section are not standard Bootstrap classes for this purpose.<button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#info">Show Info</button> <div id="info" class="collapse">Extra details here.</div>
data-bs-toggle="collapse" and targets the #info div to toggle its visibility.info has class collapse, so it is hidden initially and will show or hide when the button is clicked.<button class="btn btn-primary" data-toggle="collapse" data-target="#details">Toggle</button> <div id="details" class="collapse">Hidden content</div>
data-bs-, so data-toggle and data-target are outdated.data-bs-toggle="collapse" and data-bs-target="#details"..collapse sections with toggle buttons lets users expand only the answers they want to read, keeping the page clean.Pass the test and this pattern moves unknown