How do I trap focus in a modal?
Question
Answer
It is always recommended to use native elements when possible, such as the native dialog element. Then, the browser will handle focus and trap it within the modal. However, if you are using a custom modal element and want to trap keyboard focus, ensure that all modal elements are within a dialog container and that the dialog container has role="dialog" and aria-modal="true”.
To learn more about managing focus, check out Accessible Web's "Keyboard Navigation & Focus Management" course.
Free Tools to Continue Learning
Accessible Web RAMP
Monitor your website and manage your progress towards WCAG conformance.
Accessible Web Academy
Career specific courses to help power up your accessibility knowledge.