Course contentsShow
← Web Development
Lesson 1160 of 2,98221. Accessible ComponentsFree lesson

Scrolling and Large Modal Content

Managing scrollable content within modals while maintaining focus trap and accessibility.

Scrolling and Large Modal Content

What you'll learn: How to handle modals with content that exceeds the viewport height while keeping focus and accessibility intact.

The Challenge

When a modal contains more content than fits on screen—like a long terms-of-service document or a detailed form—you need scrolling. But here's the catch: you must ensure only the modal scrolls (not the page behind it), keyboard navigation stays within the modal, and screen readers understand the structure.

Making Modals Scrollable

Think of a modal like a window frame. When the content inside is too tall, the interior should scroll, not the whole page.

For native <dialog> elements shown with showModal(), the browser already prevents background scrolling. You control the dialog's scrollability with CSS:

dialog {
  max-height: 80vh; /* Don't cover entire screen */
  overflow-y: auto; /* Enable vertical scroll when needed */
}

This creates a scrollable area within the modal. The focus trap (which you already implemented) continues working because it's on the dialog container, not affected by internal scrolling.

Key Considerations

Scrollable region identification: If your modal has a scrollable section (like a content area separate from header/footer), ensure screen readers announce it properly. The entire dialog is already focusable, so internal scrolling typically requires no extra ARIA.

Keyboard accessibility: Arrow keys and Page Up/Down naturally scroll the focused element. Since your focus trap keeps keyboard users inside the modal, they can scroll without escaping.

Visual clarity: Style your scrollable area so users see there's more content (shadows, fade effects, or visible scrollbars help).

Key Takeaway: Use max-height and overflow-y: auto on modals to enable internal scrolling while your existing focus trap keeps users contained—the background stays locked, and accessibility remains intact.