Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Follow Focus Through a Modal

In this lesson, you followed focus through the step ladder’s cancel dialog on My Reservations, the way Mei Lin Chen does with the keyboard:

  • Focus moves in. When Cancel step ladder opens the dialog as a modal, the dialog goes into the top layer, the backdrop dims the page, and focus lands on Keep it, the first element in the dialog that can take focus. That’s a safe start: an accidental Enter keeps the reservation. To start focus on another element, give that element the autofocus attribute.
  • Focus stays off the page. Tab moves between Keep it and Yes, cancel, and past the last button out to the browser’s own controls, then back to Keep it. Those controls belong to the browser, so that’s expected. While the modal is open, the rest of the page is inert: Cancel hedge trimmer never gets focus, and nothing behind the backdrop takes clicks.
  • A click on the backdrop doesn’t close it. A native modal stays open, so always give it a visible way to close, like Keep it. closedby="any" adds closing on an outside click, but it isn’t Baseline yet. Some popovers, which come later in this section, close on an outside click.
  • Focus comes back. The browser remembers which button opened the dialog. Esc or Keep it closes the dialog and returns focus to Cancel step ladder.
  • What a screen reader gets. With the dialog open, turn on Show accessibility tree at the top of the Accessibility pane of DevTools’ Elements panel. Chromium’s accessibility tree (November 29, 2026) holds one dialog, marked modal, named “Cancel the step ladder?” from its heading, with its text and its two buttons. The reservations behind it aren’t in the tree.
  • So on opening, a screen reader should announce a dialog with that name, then the Keep it button, and on closing, the Cancel step ladder button. Check it with your own screen reader.
  • The open attribute does none of this. Showing a dialog with open doesn’t move focus in: its buttons are part of the page, so Tab goes through them and on to Cancel hedge trimmer, and Esc does nothing. Nothing behind it is inert either, so the page’s buttons still take clicks.
  • When Keep it closes the dialog, no button opened it, so focus goes to the page itself. No focus ring shows and there’s no button for a screen reader to announce, so Mei Lin can’t tell where she is. The browser keeps her place where the dialog was, so the next Tab reaches Cancel hedge trimmer, but the modal had put her back on her own button.

Here’s the ladder’s button and dialog, unchanged from the last lesson (with its popover fallback attributes left out):

Language: HTML
<button type="button" commandfor="cancel-ladder"
  command="show-modal">Cancel step ladder</button>
<dialog id="cancel-ladder" class="cancel-dialog"
  aria-labelledby="cancel-ladder-title">
  <h2 id="cancel-ladder-title">Cancel the step ladder?</h2>
  <p>Another member can then reserve it for December 4.</p>
  <form action="reservation-cancelled.html">
    <input type="hidden" name="tool" value="step-ladder">
    <button type="button" commandfor="cancel-ladder"
      command="close">Keep it</button>
    <button>Yes, cancel</button>
  </form>
</dialog>

A modal dialog moves focus in, keeps it away from the inert page behind, and gives it back to the button that opened it.

00:00 In the last lesson, you gave the step ladder on My Reservations a cancel dialog that opens as a modal. Focus landed on Keep it only because it came first. Is that the right place, and how does focus find its way back?

00:14 For someone on the keyboard, the focus ring shows where they are on the page. If it jumps somewhere unexpected, they lose their place. So follow focus the way Mei Lin Chen does.

00:25 She has tabbed to the Cancel step ladder button.

00:29 She presses Enter. The dialog opens in the top layer, and the backdrop dims the page behind it. Focus moves into the dialog. It lands on the Keep it button, the first element in the dialog that can take focus.

00:43 That’s a safe place to start. If Mei Lin presses Enter again by accident, her ladder stays reserved.

00:50 To start focus somewhere else, you’d give that element the autofocus attribute. Behind the scenes, the browser also remembers which button opened the dialog.

01:00 You’ll need that in a moment.

01:03 She presses Tab, and focus moves to the Yes, cancel button. Another Tab takes focus out to the browser’s own controls. That’s the browser, not the page. One more, and focus is back on Keep it.

01:17 The Cancel hedge trimmer button never gets focus. While the modal is open, the rest of the page is inert: nothing behind the backdrop can take focus or clicks.

01:27 Watch out: a click on the dimmed page does nothing, so give every modal a visible way to close, like Keep it. A closedby attribute set to any adds closing on an outside click, but it isn’t Baseline yet.

01:44 Mei Lin presses Escape. The dialog closes, and focus goes back to the Cancel step ladder button, the one the browser remembered. The Keep it button closes it the same way, with focus back on the same button.

01:57 So she’s right where she started.

02:01 Now check what a screen reader gets. Open the dialog again. In DevTools, in the Elements panel’s Accessibility pane, turn on the full-page accessibility tree.

02:11 It holds one dialog, marked as modal. Its name comes from its heading. Inside are its text and its two buttons. The reservations behind it aren’t in the tree at all.

02:23 So on opening, a screen reader should announce a dialog with that name, then the Keep it button, where focus landed. On closing, it’s back on the Cancel step ladder button.

02:34 Try it with your own screen reader.

02:37 Compare the detour from the last lesson: the same dialog, shown with the open attribute instead of a command. Showing it doesn’t move focus in.

02:46 Mei Lin tabs through its buttons like any others, then on to the Cancel hedge trimmer button. Escape does nothing. Nothing behind it is inert, either, so the page’s buttons still take clicks.

02:58 She tabs back to Keep it and presses Enter, and the dialog closes. Your turn: pause the video and predict where focus goes. To the page itself. No button opened this dialog, so focus has nowhere to return.

03:14 No ring shows, and there’s no button for a screen reader to announce, so Mei Lin can’t tell where she is. The browser keeps her place where the dialog was, so her next Tab reaches Cancel hedge trimmer.

03:26 In contrast, the modal put her back on her own button. A modal dialog moves focus in, keeps it away from the inert page behind, and gives it back to the button that opened it.

03:38 The open attribute does none of that.

03:42 Mei Lin’s ladder now asks before it cancels, and keeping it returns her to the same button. Next, in Exercise: Build the Cancel Reservation Dialog, you’ll build a cancel dialog for My Reservations yourself.

03:55 Then walk its focus in your own live preview.

Become a Member to join the conversation.