Bird
Raised Fist0
Bootsrapmarkup~20 mins

Offcanvas component in Bootsrap - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Offcanvas Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ rendering
intermediate
2:00remaining
What is the visual output of this Offcanvas component code?

Look at this Bootstrap Offcanvas HTML snippet. What will you see when you click the button?

Bootsrap
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#demoOffcanvas" aria-controls="demoOffcanvas">Open Menu</button>
<div class="offcanvas offcanvas-start" tabindex="-1" id="demoOffcanvas" aria-labelledby="demoOffcanvasLabel">
  <div class="offcanvas-header">
    <h5 id="demoOffcanvasLabel">Menu</h5>
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <p>Welcome to the sidebar menu!</p>
  </div>
</div>
AA dropdown menu appears below the button with the text 'Welcome to the sidebar menu!'
BA modal popup appears in the center with the text 'Welcome to the sidebar menu!'
CNothing happens because the button lacks a JavaScript event listener.
DA sidebar slides in from the left with a header 'Menu' and a close button, showing the text 'Welcome to the sidebar menu!'
Attempts:
2 left
💡 Hint

Think about what offcanvas-start means in Bootstrap.

❓ selector
intermediate
1:30remaining
Which CSS selector targets the close button inside the Offcanvas component?

Given the Offcanvas HTML below, which CSS selector correctly selects the close button?

<div class="offcanvas" id="myOffcanvas">
  <div class="offcanvas-header">
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
</div>
Abutton.btn-close[data-bs-dismiss="offcanvas"]
B#myOffcanvas .btn-close
C.offcanvas .btn-close[data-bs-dismiss="modal"]
D.offcanvas-header > button[data-bs-toggle]
Attempts:
2 left
💡 Hint

Look for the attribute that tells the button to close the offcanvas.

🧠 Conceptual
advanced
1:30remaining
What ARIA attribute ensures the Offcanvas component is accessible to screen readers?

Which ARIA attribute is essential to link the Offcanvas container with its label for accessibility?

Aaria-expanded
Baria-labelledby
Caria-hidden
Daria-controls
Attempts:
2 left
💡 Hint

Think about how screen readers know the title of the offcanvas.

❓ layout
advanced
1:30remaining
How to make the Offcanvas appear from the bottom instead of the left?

Given this Offcanvas code, which class change makes it slide up from the bottom?

Bootsrap
<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvasExample" aria-labelledby="offcanvasExampleLabel">
  <!-- content -->
</div>
AChange <code>offcanvas-start</code> to <code>offcanvas-bottom</code>
BAdd <code>offcanvas-top</code> class alongside <code>offcanvas-start</code>
CReplace <code>offcanvas-start</code> with <code>offcanvas-end</code>
DAdd <code>offcanvas-right</code> class
Attempts:
2 left
💡 Hint

Bootstrap uses directional classes like offcanvas-start and offcanvas-bottom.

📝 Syntax
expert
2:00remaining
What error occurs if you omit the tabindex="-1" attribute on the Offcanvas container?

Consider this Offcanvas snippet missing tabindex="-1". What happens when you try to open it?

Bootsrap
<div class="offcanvas offcanvas-start" id="offcanvasExample" aria-labelledby="offcanvasExampleLabel">
  <!-- content -->
</div>
AThe offcanvas opens but the close button does not work.
BThe offcanvas fails to open and throws a JavaScript error.
CThe offcanvas opens but keyboard focus is not trapped inside, causing accessibility issues.
DThe offcanvas opens but the screen reader cannot detect it.
Attempts:
2 left
💡 Hint

Think about keyboard navigation and focus management.

Practice

(1/5)
1. What is the main purpose of the Bootstrap offcanvas component?
easy
A. To hide content off-screen and slide it in when triggered
B. To create a fixed header at the top of the page
C. To add a footer with copyright information
D. To display modal popups in the center of the screen

Solution

  1. Step 1: Understand the offcanvas component role

    The offcanvas component is designed to keep content hidden off-screen initially.
  2. Step 2: Recognize its behavior on trigger

    When triggered, it slides in to show the hidden content, improving page focus.
  3. Final Answer:

    To hide content off-screen and slide it in when triggered -> Option A
  4. Quick Check:

    Offcanvas = hidden sliding content [OK]
Hint: Offcanvas slides content in from off-screen [OK]
Common Mistakes:
  • Confusing offcanvas with modal popups
  • Thinking it creates fixed headers or footers
  • Assuming it always shows content without sliding
2. Which of the following is the correct way to toggle an offcanvas component using a button in Bootstrap?
easy
A. Toggle
B. Toggle
C. Toggle
D. Toggle

Solution

  1. Step 1: Identify the correct data attribute for offcanvas toggle

    Bootstrap uses data-bs-toggle="offcanvas" to toggle offcanvas components.
  2. Step 2: Confirm the target attribute

    The data-bs-target attribute points to the offcanvas element's ID, e.g., #myOffcanvas.
  3. Final Answer:

    <button data-bs-toggle="offcanvas" data-bs-target="#myOffcanvas">Toggle</button> -> Option C
  4. Quick Check:

    Toggle offcanvas = data-bs-toggle="offcanvas" [OK]
Hint: Use data-bs-toggle="offcanvas" to toggle offcanvas [OK]
Common Mistakes:
  • Using data-toggle instead of data-bs-toggle
  • Using modal or dropdown toggle attributes
  • Missing the correct data-bs-target selector
3. What will happen when the following code is rendered and the button is clicked?
<button class="btn btn-primary" data-bs-toggle="offcanvas" data-bs-target="#demoOffcanvas">Open</button>
<div class="offcanvas offcanvas-start" tabindex="-1" id="demoOffcanvas">
  <div class="offcanvas-header">
    <h5>Menu</h5>
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">Content here</div>
</div>
medium
A. Nothing happens because the offcanvas is missing a JavaScript import
B. A modal popup appears in the center with 'Menu' and 'Content here'
C. The button triggers a dropdown menu instead of offcanvas
D. A sidebar slides in from the left showing 'Menu' and 'Content here'

Solution

  1. Step 1: Analyze the button attributes

    The button uses data-bs-toggle="offcanvas" and targets #demoOffcanvas, so it triggers the offcanvas component.
  2. Step 2: Understand the offcanvas element classes

    The offcanvas has class offcanvas-start, meaning it slides in from the left side.
  3. Final Answer:

    A sidebar slides in from the left showing 'Menu' and 'Content here' -> Option D
  4. Quick Check:

    offcanvas-start = slide from left [OK]
Hint: offcanvas-start slides from left, triggered by data-bs-toggle [OK]
Common Mistakes:
  • Confusing offcanvas with modal popups
  • Forgetting to include Bootstrap JS for functionality
  • Assuming offcanvas appears as dropdown
4. Identify the error in this offcanvas toggle button code:
<button class="btn btn-secondary" data-bs-toggle="offcanvas" data-target="#sidePanel">Show</button>
<div class="offcanvas offcanvas-end" tabindex="-1" id="sidePanel">
  <div class="offcanvas-header">
    <h5>Side Panel</h5>
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">Details here</div>
</div>
medium
A. The offcanvas element is missing the class show
B. The button uses incorrect attribute data-target instead of data-bs-target
C. The button should have type="submit" to work
D. The offcanvas ID should not contain uppercase letters

Solution

  1. Step 1: Check button data attributes for toggling offcanvas

    Bootstrap 5 requires data-bs-target for toggling, not data-target.
  2. Step 2: Verify other attributes and classes

    The offcanvas element and button classes are correct; no need for show class initially.
  3. Final Answer:

    The button uses incorrect attribute data-target instead of data-bs-target -> Option B
  4. Quick Check:

    Use data-bs-target, not data-target [OK]
Hint: Bootstrap 5 uses data-bs-target, not data-target [OK]
Common Mistakes:
  • Using old Bootstrap 4 attribute names
  • Adding show class manually on offcanvas
  • Assuming button needs type="submit"
5. You want an offcanvas panel that slides in from the bottom and closes when the user clicks outside it or presses Escape. Which combination of classes and attributes should you use?
hard
A. Use offcanvas-bottom class on the offcanvas and ensure tabindex="-1" is set
B. Use offcanvas-top class and add data-bs-backdrop="false"
C. Use offcanvas-end class and omit tabindex attribute
D. Use offcanvas-start class and add data-bs-scroll="true"

Solution

  1. Step 1: Choose the correct offcanvas position class

    To slide in from the bottom, use the offcanvas-bottom class.
  2. Step 2: Ensure accessibility and closing behavior

    Setting tabindex="-1" allows keyboard focus and enables closing with Escape key and outside clicks by default.
  3. Final Answer:

    Use offcanvas-bottom class on the offcanvas and ensure tabindex="-1" is set -> Option A
  4. Quick Check:

    Bottom offcanvas = offcanvas-bottom + tabindex="-1" [OK]
Hint: Bottom offcanvas uses offcanvas-bottom and tabindex="-1" [OK]
Common Mistakes:
  • Using offcanvas-top instead of offcanvas-bottom
  • Disabling backdrop with data-bs-backdrop="false"
  • Omitting tabindex attribute causing keyboard issues
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