Live Preview
A dialog opened with the open attribute:

By the end of this tutorial, you’ll build native modal and non-modal dialogs with <dialog>—no custom overlay divs required.
Structure dialog content with forms and close buttons.
Open blocking modals with backdrop and focus trap.
Open non-modal panels that don’t block the page.
Display a dialog visible on page load.
Close dialogs from submit buttons natively.
Style the modal overlay behind the dialog.
<dialog> Tag?The dialog element (<dialog>) represents a dialog box or other interactive component. It can be modal (blocks the page with a backdrop) or non-modal (floats without blocking). By default, a dialog is hidden until opened.
Modern browsers include built-in backdrop, focus management, and Escape-to-close for modal dialogs opened with showModal()—no custom overlay divs required.
Common uses include confirmation prompts, sign-up forms, cookie notices, image lightboxes, settings panels, and any UI that needs a focused overlay.
Place dialog content inside opening and closing dialog tags:
<dialog id="myDialog">
<p>Dialog message here.</p>
<form method="dialog">
<button>Close</button>
</form>
</dialog>open attribute.showModal() for blocking modals with backdrop and focus trap.form method="dialog" so submit buttons close the dialog automatically.::backdrop pseudo-element on modal dialogs.| Feature | Syntax / Rule | Notes |
|---|---|---|
| open | open | Visible on page load (non-modal) |
| showModal() | dialog.showModal() | Opens with backdrop |
| show() | dialog.show() | Non-modal panel |
| close() | dialog.close() | Closes the dialog |
| Form close | method="dialog" | Submit closes dialog |
| Backdrop | dialog::backdrop | Styles modal overlay |
How you open a dialog determines its behavior:
| Method | Type | Behavior |
|---|---|---|
showModal() | Modal | Backdrop, Escape closes, focus trapped |
show() | Non-modal | No backdrop, page stays usable |
open attribute | Non-modal | Visible immediately on load |
| Use cases | Alerts, forms | Confirmations, panels, notices |
Open and close dialogs with the open attribute or JavaScript methods:
open BooleanMakes the dialog visible on page load as a non-modal dialog.
<dialog open>showModal() ModalOpens as a modal with backdrop, focus trap, and Escape to close.
dialog.showModal()show() OptionalOpens as a non-modal panel without blocking page interaction.
dialog.show()close() MethodCloses the dialog. Optional return value sets returnValue.
dialog.close('ok')method="dialog" FormSubmit buttons inside the form close the dialog automatically.
<form method="dialog">::backdrop CSSPseudo-element for styling the modal overlay behind the dialog.
dialog::backdrop { ... }Use form method="dialog" so submit buttons close the dialog automatically. The button’s value becomes dialog.returnValue.
open attribute, showModal(), form dialogs, and non-modal panels with copy-ready code and live previews.
A dialog opened with the open attribute:
Show a dialog on page load with the open attribute.
<dialog open>
<p>This dialog is open by default.</p>
<form method="dialog">
<button>Close</button>
</form>
</dialog>Use <dialog> for confirmation prompts, sign-up forms, cookie notices, image lightboxes, settings panels, and any UI that needs a focused overlay without building modal logic from scratch.
Open a modal dialog from a button click using JavaScript.
<button type="button" id="openBtn">Open modal</button>
<dialog id="welcomeDialog">
<p>Please confirm you want to continue.</p>
<form method="dialog">
<button value="confirmed">Confirm</button>
</form>
</dialog>
<script>
const dialog = document.getElementById('welcomeDialog');
document.getElementById('openBtn').addEventListener('click', () => {
dialog.showModal();
});
</script>Collect user input in a modal form that closes on submit.
<dialog id="signupDialog">
<form method="dialog">
<label>Name <input type="text" name="name" required></label>
<button type="submit" value="cancel">Cancel</button>
<button type="submit" value="save">Save</button>
</form>
</dialog>Use show() when the dialog should not block the rest of the page.
<button type="button" id="showBtn">Show panel</button>
<dialog id="infoPanel">
<p>Non-modal helper panel.</p>
<form method="dialog"><button>Dismiss</button></form>
</dialog>
<script>
document.getElementById('showBtn').addEventListener('click', () => {
document.getElementById('infoPanel').show();
});
</script>Customize the dialog box and its modal backdrop with standard CSS:
border-radius Rounded dialog box::backdrop Dim modal overlaybox-shadow Elevated panel lookpadding Spacious content area/* Dialog and backdrop styling */
dialog {
border: none;
border-radius: 12px;
padding: 1.5rem;
box-shadow: 0 20px 40px rgba(15, 23, 42, 0.15);
}
dialog::backdrop {
background: rgba(15, 23, 42, 0.5);
}Live styled dialog (open attribute preview)
Native dialogs include helpful built-in behavior:
Content lives inside dialog, hidden by default.
showModal(), show(), or the open attribute reveals it.
Escape, backdrop click, or method="dialog" form closes it.
Focused overlays without custom overlay divs and manual focus traps.
The <dialog> element is fully supported in all modern browsers. Internet Explorer does not support it.
Chrome, Firefox, Safari, Edge, and Opera all support the dialog element with showModal(), backdrop, and form method="dialog". Provide a fallback for legacy IE.
Bottom line: Safe for all modern production environments. For legacy IE, use a polyfill or custom modal fallback.
The <dialog> tag provides native modal and non-modal overlays. Use showModal() for blocking dialogs, form method="dialog" for close buttons, and ::backdrop for styling—a modern alternative to custom modal divs.
showModal() for confirmationsmethod="dialog" forms::backdrop for modals<dialog>Bookmark these before you ship — they’ll keep your modal UX native and accessible.
<dialog> creates built-in dialog boxes.
Opens blocking modals with backdrop.
ModalNon-modal panels without blocking.
PatternSubmit buttons close the dialog natively.
FormsStyle the modal overlay behind the box.
DesignUse a polyfill for legacy environments.
CompatshowModal() opens a modal with a backdrop and focus trap. show() opens a non-modal panel that does not block the page.dialog.close(), press Escape in a modal, click the backdrop, or submit a form method="dialog" inside the dialog.Practice showModal(), forms, and close patterns in the interactive HTML editor.
6 people found this page helpful