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

Initial Focus Placement

Determining where focus should move when a modal opens for optimal user experience.

Initial Focus Placement

You'll learn: Where focus should initially move when a modal dialog opens to create the best experience for keyboard and screen reader users.


When you open a modal dialog, focus can't just stay on the trigger button that opened it—users need to interact with the modal itself. The question is: where should focus land first? This decision significantly affects usability.

Why It Matters

Think of it like walking into a room: you need to know where you are and what's in front of you. If focus lands in the wrong place, keyboard users might be disoriented or miss critical information.

Common Strategies

Option 1: Focus the Modal Container
Set focus on the dialog element itself (with tabindex="-1"). This lets screen readers announce the modal's accessible name and description, giving users context before they encounter interactive elements.

<dialog id="myModal" aria-labelledby="modal-title">
  <h2 id="modal-title">Confirm Action</h2>
  <p>Are you sure you want to delete this item?</p>
  <button>Cancel</button>
  <button>Delete</button>
</dialog>
const modal = document.getElementById('myModal');
modal.showModal();
modal.focus(); // Focus the container itself

Option 2: Focus the First Focusable Element
Move focus to the first interactive control (like a close button or input field) if users need to act immediately.

Option 3: Focus a Specific Element
For forms, focus the first input field. For confirmation dialogs with destructive actions, some developers focus the safer option (like "Cancel") to prevent accidental deletions.

Choosing the Right Approach

Consider what the user needs to know first and what action they'll likely take. Context and purpose drive the decision.

Key Takeaway: When a modal opens, intentionally place focus where users can best understand the modal's purpose and take their next action—typically either the modal container itself or the first logical interactive element.