CSS Dialog Light Dismiss with closedby Attribute

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.

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

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

Not ready for production without a fallback.

134+
134+
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+

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.

ESC