Expandable content helps keep a webpage clean and neat by hiding extra details until you want to see them.
0
0
Why expandable content saves space in Bootsrap
Introduction
When you have a long list but want to show only the main points first.
When you want to save space on a small screen like a phone.
When you want users to focus on important info and reveal more only if needed.
When you have FAQs and want to show answers only when a question is clicked.
When you want to organize content in sections that can open or close.
Syntax
Bootsrap
<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"> Section Title </button> </h2> <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample"> <div class="accordion-body"> Hidden content that expands when clicked. </div> </div> </div> </div>
This example uses Bootstrap's accordion component to create expandable sections.
The data-bs-toggle="collapse" attribute controls the expand/collapse behavior.
Examples
A simple button that toggles extra content below it.
Bootsrap
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#extraInfo" aria-expanded="false" aria-controls="extraInfo"> Show More </button> <div class="collapse" id="extraInfo"> <div class="card card-body"> Extra details appear here. </div> </div>
An FAQ style expandable section using Bootstrap accordion.
Bootsrap
<div class="accordion" id="faqAccordion"> <div class="accordion-item"> <h2 class="accordion-header" id="faq1"> <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#answer1" aria-expanded="false" aria-controls="answer1"> What is expandable content? </button> </h2> <div id="answer1" class="accordion-collapse collapse" aria-labelledby="faq1" data-bs-parent="#faqAccordion"> <div class="accordion-body"> Content that can be shown or hidden to save space. </div> </div> </div> </div>
Sample Program
This page shows a button that reveals hidden content when clicked, using Bootstrap's collapse feature.
Bootsrap
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Expandable Content Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <main class="container my-5"> <h1>Why Expandable Content Saves Space</h1> <p>Click the button below to see more details.</p> <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#moreInfo" aria-expanded="false" aria-controls="moreInfo"> Show More </button> <div class="collapse mt-3" id="moreInfo"> <div class="card card-body"> Expandable content hides extra information until you want to see it. This keeps the page tidy and easier to read. </div> </div> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
OutputSuccess
Important Notes
Expandable content improves user experience by reducing clutter.
Always use accessible attributes like aria-expanded and aria-controls for screen readers.
Bootstrap's built-in JavaScript handles the show/hide animations smoothly.
Summary
Expandable content saves space by hiding details until needed.
Bootstrap makes it easy to add expandable sections with simple classes and attributes.
Use expandable content to keep pages clean and user-friendly.