HTML Beginner

Dialog light dismiss without click-outside listeners

Closing a dialog when the user clicks outside required a JavaScript click listener on the backdrop. The closedby attribute gives dialogs popover-style light dismiss behavior natively.

Modern
3 lines
1<dialog closedby="any">
2  Click outside or press ESC to close
3</dialog>
Old 12 lines
1/* JS: detect click outside dialog bounds */
2dialog.addEventListener('click', (e) => {
3  const rect = dialog.getBoundingClientRect();
4  if (e.clientX < rect.left ||
5      e.clientX > rect.right ||
6      e.clientY < rect.top ||
7      e.clientY > rect.bottom) {
8    dialog.close();
9  }
10});
Limited availability 69% global usage

This feature is not Baseline because it does not work in some of the most widely-used browsers.

134+
134+
Interop 2026 focus area ? Learn more β†’
click outside or press ESC β€” no JS listener
closedby="any"

Click outside or press ESC to close. No click-outside JavaScript needed.

closedby="any" β€” Chrome 134+
Not supported in this browser
closedby="any" requires Chrome 134+
Kinsta

Your first month is free

Managed WordPress hosting for faster sites.

Learn more
⚑

One attribute

Add closedby="any" and the browser handles backdrop clicks and ESC key. No JS event wiring.

✦

Consistent behavior

Works like popover light dismiss. Users get the same close-on-click-outside pattern everywhere.

∞

Three modes

none (default), closerequest (ESC only), or any (ESC + backdrop click). Pick the right behavior per dialog.

Lines Saved
12 β†’ 1
One HTML attribute
Old Approach
JS click listener
getBoundingClientRect checks
Modern Approach
closedby attribute
Native light dismiss

How it works

The <dialog> element didn't have a built-in way to close when clicking outside. Developers had to add a click event listener, check if the click was outside the dialog's bounding rect (since clicking the ::backdrop still fires on the dialog), and then call dialog.close(). This was error-prone and needed extra code for ESC handling too.

The closedby attribute, available from Chrome 134, brings the popover's light dismiss pattern to dialogs. Set closedby="any" for full light dismiss (backdrop click + ESC), closedby="closerequest" for ESC-only, or closedby="none" to disable user-triggered closing entirely. The browser handles all the hit-testing and keyboard events natively.

New CSS drops.

Join 600+ readers who've survived clearfix hacks.

ESC