The HTML <dialog> element creates a native dialog box. Use showModal() when the user must interact with the dialog before returning to the page, or show() for a non-modal dialog.
For most cases, pair the element with a clearly labeled button, provide an obvious way to close it, and keep important page content outside the dialog unless the interaction genuinely requires a modal.
<button type="button" id="openDialog">Open dialog</button>
<dialog id="myDialog">
<h2>Confirm action</h2>
<p>Continue with this action?</p>
<button type="button" id="closeDialog">Close</button>
</dialog>
const dialog = document.getElementById('myDialog');
document.getElementById('openDialog').addEventListener('click', () => {
dialog.showModal();
});
document.getElementById('closeDialog').addEventListener('click', () => {
dialog.close();
});
showModal() opens the dialog as a modal. The rest of the document becomes inactive until the dialog is closed.
dialog.showModal() opens a modal dialog.dialog.show() opens a non-modal dialog and leaves the rest of the page interactive.dialog.close() closes an open dialog. An optional return value can be supplied.dialog.show();
dialog.close('confirmed');
console.log(dialog.returnValue);
Use a modal only when the task requires the user's attention before they continue. For ordinary information, inline content or a non-modal disclosure may be less disruptive.
A form inside a dialog can use method="dialog". Submitting that form closes the dialog without sending the form data to a server.
<dialog id="confirmDialog">
<form method="dialog">
<p>Delete this item?</p>
<button value="cancel">Cancel</button>
<button value="delete">Delete</button>
</form>
</dialog>
The selected button's value becomes the dialog's returnValue. If the action must update server data, handle that separately and validate it on the server.
The Boolean open attribute controls whether a dialog is displayed. However, for modal dialogs, prefer showModal() instead of manually adding open, because modal behavior involves more than visibility.
<dialog open>
This dialog is displayed as a non-modal dialog.
</dialog>
dialog {
padding: 1.25rem;
border: 1px solid #ccc;
border-radius: 0.5rem;
max-width: 32rem;
}
dialog::backdrop {
background: rgb(0 0 0 / 0.5);
}
The ::backdrop pseudo-element applies only when a dialog is shown in the top layer, such as with showModal().
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>HTML dialog example</title>
<style>
dialog {
padding: 1.25rem;
border: 1px solid #ccc;
border-radius: 0.5rem;
max-width: 30rem;
}
dialog::backdrop {
background: rgb(0 0 0 / 0.5);
}
</style>
</head>
<body>
<button type="button" id="openDialog">Open dialog</button>
<dialog id="myDialog">
<h2>Welcome</h2>
<p>This is a native modal dialog.</p>
<button type="button" id="closeDialog">Close</button>
</dialog>
<script>
const dialog = document.getElementById('myDialog');
document.getElementById('openDialog').addEventListener('click', () => dialog.showModal());
document.getElementById('closeDialog').addEventListener('click', () => dialog.close());
</script>
</body>
</html>
Good uses include confirmations, short focused forms, settings panels, and decisions that temporarily interrupt the main task. Avoid using a modal for content that could simply appear in the page, especially if users need to compare it with surrounding information.
Use <details> when users should expand or collapse supporting content within the document. Use <dialog> when the content is a separate interaction, especially when modal behavior is required.
Dialogs are controlled with JavaScript methods and events. Continue with JavaScript events or JavaScript functions when building reusable dialog controls.
open as a replacement for showModal() when true modal behavior is required.Call the dialog element's showModal() method. This places it in the top layer and prevents interaction with the rest of the document until it closes.
Use show(). The dialog is displayed, but the rest of the page remains interactive.
Call dialog.close(), submit a form using method="dialog", or provide another supported close interaction.
Yes. Pass a value to close() or use buttons inside a method="dialog" form, then read the dialog's returnValue.
No. A dialog is an interaction component, not an SEO technique. Keep essential page information easy for users to find in the normal document flow unless it genuinely belongs inside the interaction.
HTML5 Tags Types of HTML tagsAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.