Bird
Raised Fist0
Bootsrapmarkup~10 mins

Collapse component in Bootsrap - Browser Rendering Trace

Choose your learning style10 modes available
Render Flow - Collapse component
Load HTML with collapse markup
↓
Bootstrap JS reads data attributes
↓
Attach click event to toggle button
↓
On click, toggle 'show' class on collapse content
↓
Browser recalculates layout
↓
Animate height from 0 to full height
↓
Render expanded or collapsed content
The browser loads the HTML and Bootstrap's JavaScript detects the collapse toggle button. When clicked, it adds or removes the 'show' class on the content, triggering CSS transitions that animate the content's height, causing the content to expand or collapse visually.
Render Steps - 3 Steps
Code Added:<div class="collapse" id="collapseExample">...</div>
Before
[Button]

[No content visible]
→
After
[Button]

[Empty space where content will appear]
The collapse container is added but content is hidden by default with height 0, so nothing is visible below the button.
🔧 Browser Action:Creates DOM node for collapse container; applies CSS with height 0 and overflow hidden
Code Sample
A button toggles the visibility of a content box. The content is hidden initially and smoothly expands or collapses when the button is clicked.
Bootsrap
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
  Toggle Content
</button>
<div class="collapse" id="collapseExample">
  <div class="card card-body">
    This content is hidden by default and shown when toggled.
  </div>
</div>
Bootsrap
.collapse {
  height: 0;
  overflow: hidden;
  transition: height 0.35s ease;
}
.collapse.show {
  height: auto;
  overflow: visible;
}
Render Quiz - 3 Questions
Test your understanding
After applying step 2, what visual change do you see?
AButton disappears
BContent instantly appears without animation
CContent smoothly expands below the button
DContent remains hidden
Common Confusions - 3 Topics
Why does the content suddenly jump instead of smoothly expanding?
If the height is set directly to 'auto' without transition, the browser cannot animate from 0 to auto height smoothly, causing a jump. Bootstrap uses JavaScript to calculate height values for smooth animation.
💡 Transitions animate numeric values like fixed heights, not 'auto'. Bootstrap handles this internally.
Why can't I see the content even after clicking the toggle button?
If the 'show' class is not added or JavaScript is not working, the content stays collapsed with height 0 and overflow hidden, so it remains invisible.
💡 The 'show' class controls visibility by changing height and overflow.
Why does the collapse content affect layout spacing when hidden?
Because the collapse container still exists in the DOM but with height 0 and overflow hidden, it takes minimal vertical space but no visible content. Margins or padding inside may still affect spacing.
💡 Collapsed content occupies space only if padding or margins exist outside height 0 area.
Property Reference
PropertyValue AppliedVisual EffectCommon Use
height0Content hidden by collapsing height to zeroHide content smoothly
heightautoContent fully visible with natural heightShow content fully
overflowhiddenPrevents content from spilling out when collapsedKeep hidden content clipped
overflowvisibleAllows content to be fully seen when expandedShow full content
transitionheight 0.35s easeSmooth animation of height changesAnimate expand/collapse
Concept Snapshot
Bootstrap Collapse component hides or shows content by toggling the 'show' class. The container has height 0 and overflow hidden when collapsed. Adding 'show' changes height to auto and overflow visible, revealing content. CSS transition on height creates smooth expand/collapse animation. JavaScript toggles the 'show' class on button clicks. Accessibility uses aria-expanded and aria-controls attributes.

Practice

(1/5)
1. What is the main purpose of the Bootstrap Collapse component?
easy
A. To hide or show content smoothly when a user clicks a control
B. To create a fixed navigation bar at the top of the page
C. To add animations to images on hover
D. To validate form inputs automatically

Solution

  1. Step 1: Understand the Collapse component function

    The Collapse component is designed to toggle visibility of content smoothly, usually triggered by user actions like clicks.
  2. Step 2: Compare options with the component's purpose

    Only To hide or show content smoothly when a user clicks a control describes hiding or showing content smoothly, which matches the Collapse component's role.
  3. Final Answer:

    To hide or show content smoothly when a user clicks a control -> Option A
  4. Quick Check:

    Collapse toggles content visibility = B [OK]
Hint: Collapse means hide/show content smoothly on click [OK]
Common Mistakes:
  • Confusing Collapse with navigation or animation components
  • Thinking Collapse validates forms
  • Assuming Collapse is for styling images
2. Which attribute is required on a button to toggle a Bootstrap Collapse element?
easy
A. data-target="collapse"
B. data-toggle="collapse"
C. data-bs-toggle="collapse"
D. data-bs-target="collapse"

Solution

  1. Step 1: Identify the correct Bootstrap 5 attribute for toggling Collapse

    Bootstrap 5 uses the prefix data-bs- for its JavaScript plugins, so the toggle attribute is data-bs-toggle="collapse".
  2. Step 2: Eliminate outdated or incorrect attributes

    Options A and B use syntax without bs prefix (Bootstrap 4 or earlier). data-bs-target="collapse"'s data-bs-target="collapse" specifies the target element, not the toggle action.
  3. Final Answer:

    data-bs-toggle="collapse" -> Option C
  4. Quick Check:

    Toggle attribute in Bootstrap 5 = data-bs-toggle="collapse" [OK]
Hint: Use data-bs-toggle="collapse" for toggling in Bootstrap 5 [OK]
Common Mistakes:
  • Using data-toggle instead of data-bs-toggle
  • Confusing data-bs-target with toggle attribute
  • Omitting the data-bs prefix
3. What will happen when the button below is clicked?
<button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#info">Toggle Info</button>
<div id="info" class="collapse">Hidden content here</div>
medium
A. Nothing will happen because the button lacks a click event
B. The div with id "info" will smoothly appear or disappear
C. The button will change color but content stays hidden
D. The div will be removed from the page permanently

Solution

  1. Step 1: Analyze the button attributes

    The button has data-bs-toggle="collapse" and data-bs-target="#info", linking it to the div with id "info".
  2. Step 2: Understand the effect on the target div

    The div has class "collapse", so clicking the button toggles its visibility with a smooth animation.
  3. Final Answer:

    The div with id "info" will smoothly appear or disappear -> Option B
  4. Quick Check:

    Button toggles collapse div visibility = A [OK]
Hint: Button with data-bs-toggle toggles target collapse div [OK]
Common Mistakes:
  • Thinking button needs JavaScript event handlers manually
  • Assuming button changes style only
  • Believing div is removed instead of toggled
4. Identify the error in this Bootstrap Collapse code snippet:
<button class="btn btn-secondary" data-bs-toggle="collapse" data-bs-target="#panel">Show</button>
<div id="panel" class="collaps">Content</div>
medium
A. The div has a typo in the class name; it should be "collapse"
B. The button is missing the data-bs-toggle attribute
C. The div id does not match the data-bs-target
D. The button should use data-toggle instead of data-bs-toggle

Solution

  1. Step 1: Check the button attributes

    The button correctly uses data-bs-toggle="collapse" and targets #panel.
  2. Step 2: Inspect the div class and id

    The div has id "panel" matching the target, but the class is "collaps" instead of "collapse". This typo prevents the collapse behavior.
  3. Final Answer:

    The div has a typo in the class name; it should be "collapse" -> Option A
  4. Quick Check:

    Class must be "collapse" for Bootstrap to work [OK]
Hint: Check class spelling: must be "collapse" not "collaps" [OK]
Common Mistakes:
  • Misspelling the collapse class
  • Confusing data-bs-toggle with data-toggle
  • Mismatching id and data-bs-target
5. You want to create an accordion with two sections using Bootstrap Collapse. Which code snippet correctly ensures only one section is open at a time?
<div class="accordion" id="accordionExample">
  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">Section 1</button>
    </h2>
    <div id="collapseOne" class="accordion-collapse collapse show" data-bs-parent="#accordionExample">
      <div class="accordion-body">Content 1</div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">Section 2</button>
    </h2>
    <div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
      <div class="accordion-body">Content 2</div>
    </div>
  </div>
</div>
hard
A. The divs lack the collapse class, so sections won't toggle
B. Missing data-bs-toggle attribute on buttons prevents collapse
C. Using aria-expanded="true" disables collapse functionality
D. This code correctly uses data-bs-parent to allow only one open section

Solution

  1. Step 1: Check for accordion structure and attributes

    The code uses data-bs-parent="#accordionExample" on collapse divs, which ensures only one section is open at a time.
  2. Step 2: Verify buttons and collapse classes

    Buttons have data-bs-toggle="collapse" and target correct divs. Collapse divs have the correct classes and IDs.
  3. Final Answer:

    This code correctly uses data-bs-parent to allow only one open section -> Option D
  4. Quick Check:

    data-bs-parent controls accordion behavior = C [OK]
Hint: Use data-bs-parent on collapse divs for accordion effect [OK]
Common Mistakes:
  • Omitting data-bs-parent causes multiple sections open
  • Forgetting collapse class on content divs
  • Misusing aria-expanded attribute
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard