A modal is a popup window that appears on top of the main page content. It grabs the user's attention and requires interaction before returning to the main page.
Click to reveal answer
beginner
Why do we use overlays behind modals?
Overlays dim or blur the background content to focus the user's attention on the modal. They also prevent interaction with the page behind the modal.
Click to reveal answer
intermediate
How can you make a modal accessible for keyboard users?
Ensure the modal traps keyboard focus inside it, so users can tab only within the modal. Also, provide a clear way to close the modal using keyboard keys like Escape.
Click to reveal answer
beginner
Which Tailwind CSS utilities help center a modal on the screen?
Use fixed to position the modal, inset-0 to cover the screen, and flex with items-center and justify-center to center the modal content.
Click to reveal answer
intermediate
What is the role of aria-modal and role="dialog" in modals?
aria-modal="true" tells assistive technologies that the rest of the page is inert while the modal is open. role="dialog" identifies the modal as a dialog window for screen readers.
Click to reveal answer
What does an overlay behind a modal usually do?
ADim the background and block interaction
BMake the modal draggable
CAdd animations to the modal
DChange the page title
✗ Incorrect
An overlay dims the background and prevents users from interacting with the page behind the modal.
Which Tailwind class helps fix a modal to the viewport?
Astatic
Brelative
Cabsolute
Dfixed
✗ Incorrect
The fixed class positions the modal relative to the viewport, keeping it visible when scrolling.
How should keyboard focus behave when a modal is open?
AFocus should move freely around the page
BFocus should jump to the browser address bar
CFocus should be trapped inside the modal
DFocus should be disabled
✗ Incorrect
Focus trapping keeps keyboard users inside the modal until it is closed, improving accessibility.
Which ARIA attribute indicates a modal dialog?
Aaria-modal
Baria-hidden
Caria-label
Daria-live
✗ Incorrect
aria-modal="true" signals that the modal is active and the rest of the page is inert.
What Tailwind classes center content both vertically and horizontally?
Ainline-block align-middle
Bflex items-center justify-center
Cgrid place-items-start
Dblock text-center
✗ Incorrect
Using flex with items-center and justify-center centers content in both directions.
Explain how to create an accessible modal using Tailwind CSS.
Think about layout, accessibility, and user interaction.
You got /5 concepts.
Describe why overlays are important in modal patterns and how Tailwind helps implement them.
Consider visual focus and interaction blocking.
You got /4 concepts.
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
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.
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.
Final Answer:
To darken the background and focus user attention on the modal -> Option A
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
D. Using absolute instead of fixed for the main overlay
Solution
Step 1: Check overlay positioning
The main overlay uses absolute, which positions relative to nearest ancestor, not viewport.
Step 2: Fix overlay to cover viewport
Using fixed ensures the overlay covers the entire screen regardless of scroll or parent elements.
Final Answer:
Using absolute instead of fixed for the main overlay -> Option D
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
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.
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.
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