Bird
Raised Fist0
Tailwindmarkup~20 mins

Modal and overlay patterns in Tailwind - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Modal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ rendering
intermediate
2:00remaining
What is the visible result of this modal code?
Given this Tailwind CSS modal snippet, what will the user see when the modal is open?
Tailwind
<div class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center">
  <div class="bg-white p-6 rounded shadow-lg max-w-sm w-full">
    <h2 class="text-xl font-bold mb-4">Modal Title</h2>
    <p class="mb-4">This is the modal content.</p>
    <button class="bg-blue-600 text-white px-4 py-2 rounded">Close</button>
  </div>
</div>
AA centered white box with a shadow on a semi-transparent black background covering the entire screen.
BA white box in the top-left corner with no background overlay.
COnly a black screen with no visible modal content.
DA white box centered but without any shadow or background overlay.
Attempts:
2 left
💡 Hint
Look at the classes controlling position and background colors.
❓ accessibility
intermediate
2:00remaining
Which attribute improves accessibility for this modal?
You have a modal dialog. Which attribute should you add to the modal container to help screen readers identify it as a dialog?
Tailwind
<div class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center" role="dialog" aria-modal="true">
  <div class="bg-white p-6 rounded shadow-lg max-w-sm w-full">
    <h2 class="text-xl font-bold mb-4">Modal Title</h2>
    <p class="mb-4">This is the modal content.</p>
    <button class="bg-blue-600 text-white px-4 py-2 rounded">Close</button>
  </div>
</div>
Atabindex="-1" aria-hidden="true"
Baria-describedby="close-button"
Caria-live="polite" role="alert"
Drole="dialog" aria-modal="true"
Attempts:
2 left
💡 Hint
Think about how screen readers know this is a modal dialog.
❓ selector
advanced
2:00remaining
Which Tailwind selector targets the modal overlay only?
You want to style only the overlay background of a modal with Tailwind. The overlay has class 'fixed inset-0 bg-black bg-opacity-50'. Which selector targets only this overlay?
A.modal > div
B.bg-white.p-6.rounded.shadow-lg
C.fixed.inset-0.bg-black.bg-opacity-50
D.flex.items-center.justify-center
Attempts:
2 left
💡 Hint
Look for the classes that create the overlay background.
❓ layout
advanced
2:00remaining
How to center modal content vertically and horizontally?
Which Tailwind utility classes center the modal box both vertically and horizontally inside the overlay?
Agrid place-items-center
Bflex items-center justify-center
Cblock mx-auto my-auto
Dabsolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2
Attempts:
2 left
💡 Hint
Think about flexbox centering utilities.
🧠 Conceptual
expert
2:00remaining
Why use aria-modal="true" in modals?
What is the main reason to add aria-modal="true" to a modal dialog container?
AIt tells assistive technologies that the rest of the page is inert and only the modal content should be accessed.
BIt automatically traps keyboard focus inside the modal without extra scripting.
CIt makes the modal content visible on all screen sizes.
DIt disables scrolling on the page behind the modal.
Attempts:
2 left
💡 Hint
Think about how screen readers treat the rest of the page when a modal is open.

Practice

(1/5)
1. What is the main purpose of using an overlay behind a modal in Tailwind CSS?
easy
A. To darken the background and focus user attention on the modal
B. To make the modal draggable
C. To add animation effects to the modal
D. To increase the modal's width automatically

Solution

  1. Step 1: Understand the role of overlays

    Overlays are used to cover the page content with a semi-transparent layer, making the modal stand out.
  2. Step 2: Connect overlay effect to user focus

    The darkened background helps users focus on the modal content by visually separating it from the rest of the page.
  3. Final Answer:

    To darken the background and focus user attention on the modal -> Option A
  4. Quick Check:

    Overlay = focus on modal [OK]
Hint: Overlay darkens background to highlight modal content [OK]
Common Mistakes:
  • Thinking overlay controls modal size
  • Confusing overlay with animation
  • Assuming overlay makes modal draggable
2. Which Tailwind CSS class combination correctly creates a fixed full-screen overlay with 50% black opacity?
easy
A. fixed inset-0 bg-black bg-opacity-50
B. absolute top-0 left-0 w-full h-full bg-gray-500 opacity-50
C. relative inset-0 bg-black opacity-50
D. fixed inset-0 bg-black opacity-25

Solution

  1. Step 1: Identify fixed full-screen overlay classes

    Use fixed and inset-0 to cover the entire viewport.
  2. Step 2: Apply black background with 50% opacity

    bg-black sets black color, bg-opacity-50 sets 50% opacity correctly in Tailwind.
  3. Final Answer:

    fixed inset-0 bg-black bg-opacity-50 -> Option A
  4. Quick Check:

    Fixed + inset-0 + bg-black + bg-opacity-50 = overlay [OK]
Hint: Use fixed + inset-0 + bg-black + bg-opacity-50 for overlay [OK]
Common Mistakes:
  • Using absolute instead of fixed for overlay
  • Using opacity-50 instead of bg-opacity-50
  • Missing inset-0 to cover full screen
3. Given this Tailwind modal snippet, what will be the visible background color behind the modal content?
<div class="fixed inset-0 bg-black bg-opacity-70">
  <div class="fixed inset-0 bg-white bg-opacity-30"></div>
  <div class="relative p-6 bg-gray-100 rounded">Modal Content</div>
</div>
medium
A. A dark black overlay with 70% opacity only
B. A white overlay with 30% opacity only
C. A combined effect of black 70% and white 30% overlays
D. No overlay visible, only modal content

Solution

  1. Step 1: Analyze overlay layers

    The outer div has black with 70% opacity, and inside it is another fixed div with white 30% opacity overlay.
  2. Step 2: Understand overlay stacking

    Both overlays stack, so the visible background is a mix of black 70% and white 30% overlays combined behind the modal content.
  3. Final Answer:

    A combined effect of black 70% and white 30% overlays -> Option C
  4. Quick Check:

    Multiple overlays stack visually [OK]
Hint: Multiple overlays stack and combine colors visually [OK]
Common Mistakes:
  • Ignoring inner overlay effect
  • Assuming only outer overlay shows
  • Thinking overlays cancel each other
4. Identify the error in this Tailwind modal overlay code:
<div class="absolute inset-0 bg-black bg-opacity-60">
  <div class="fixed inset-0 bg-white bg-opacity-20"></div>
  <div class="relative p-4 bg-white rounded">Modal</div>
</div>
medium
A. Overlay colors are reversed
B. Missing rounded corners on overlay
C. Incorrect padding on modal content
D. Using absolute instead of fixed for the main overlay

Solution

  1. Step 1: Check overlay positioning

    The main overlay uses absolute, which positions relative to nearest ancestor, not viewport.
  2. Step 2: Fix overlay to cover viewport

    Using fixed ensures the overlay covers the entire screen regardless of scroll or parent elements.
  3. Final Answer:

    Using absolute instead of fixed for the main overlay -> Option D
  4. Quick Check:

    Main overlay must be fixed for full screen [OK]
Hint: Use fixed, not absolute, for full-screen overlays [OK]
Common Mistakes:
  • Confusing absolute and fixed positioning
  • Ignoring overlay coverage on scroll
  • Assuming padding affects overlay behavior
5. You want to create an accessible modal with Tailwind that traps keyboard focus inside and closes on pressing Escape. Which approach is best?
hard
A. Use absolute inset-0 bg-gray-700 for overlay and rely on default browser focus behavior
B. Use fixed inset-0 bg-black bg-opacity-50 for overlay, add role="dialog" aria-modal="true", and add JavaScript to trap focus and listen for Escape key
C. Use fixed inset-0 bg-black opacity-50 and add tabindex="-1" on modal without JavaScript
D. Use relative bg-black bg-opacity-50 overlay and add aria-hidden="true" on modal

Solution

  1. Step 1: Use correct overlay and ARIA roles

    Fixed overlay with semi-transparent black background is standard. Adding role="dialog" and aria-modal="true" makes the modal accessible.
  2. Step 2: Implement keyboard focus trap and Escape key handling

    JavaScript is needed to keep keyboard focus inside the modal and close it on Escape key press for accessibility.
  3. Final Answer:

    Use fixed inset-0 bg-black bg-opacity-50 for overlay, add role="dialog" aria-modal="true", and add JavaScript to trap focus and listen for Escape key -> Option B
  4. Quick Check:

    Accessible modal = ARIA roles + focus trap + Escape key [OK]
Hint: Add ARIA roles and JS focus trap for accessible modals [OK]
Common Mistakes:
  • Skipping ARIA roles for accessibility
  • Not trapping keyboard focus inside modal
  • Using relative instead of fixed overlay
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