You tap "Delete account", and a box appears in the middle of the screen: "Delete the account?". The page behind it becomes darker and can't be clicked until you answer. It is like an ATM screen that asks you to confirm before you can continue.
To make a box that appears on top of the page, like that "Delete the account?" box, use <dialog>. You can open a dialog as a modal: a box that locks the page behind it until the visitor answers. The browser itself handles the dark background, the Esc key for closing, and the focus (the part of the page that receives what you type).
A dialog, an open button and a close button
<button onclick="document.getElementById('confirmation').showModal()">Delete account</button>
<dialog id="confirmation">
<h2>Delete the account?</h2>
<p>All your notes go with it.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="delete">Delete</button>
</form>
</dialog>
Part by part:
<dialog id="confirmation">is the dialog box. The title, text and buttons go between<dialog>and</dialog>. A dialog stays hidden until it is opened. So when the page loads, you only see the "Delete account" button.id="confirmation"is this dialog's unique name. The open button (the "Delete account" button) uses this name to find the dialog.- The "Delete account" button is outside the dialog. Its
onclickattribute holds a JavaScript command that runs when the button is clicked. The command has two parts:document.getElementById('confirmation')finds the element whose id isconfirmation, then.showModal()opens that element as a modal. <form method="dialog">is a form made for dialogs. The buttons inside it close the dialog instead of sending data to a server. The browser saves thevalueof the clicked button in the dialog'sreturnValue. From there, your JavaScript code can later tell whether the visitor chose Cancel or Delete.
The result: click "Delete account" and the dialog appears in the middle of the screen with a dark background behind it. Click Cancel or Delete, or press Esc, and the dialog closes. A click on the dark background does not close the dialog. If you want a click outside the dialog box to close it too, add closedby="any" to the opening <dialog> tag. Chrome, Edge and Firefox support it, Safari does not yet.
In the Lab you will see a short version of that command: confirmation.showModal(). It means the same thing, because the browser automatically makes the name confirmation from the dialog's id.
Another way to make the open button, with no JavaScript:
<button commandfor="confirmation" command="show-modal">Delete account</button>
commandfor holds the dialog's id, and command="show-modal" opens it as a modal. A close button uses the same commandfor, with command="close". This works in all modern browsers since December 2025. It also needs no onclick, which a strict CSP blocks (a security rule covered in the Content Security Policy lesson). This lesson still uses onclick with showModal(), because the Lab and the exercises use it, and it also works in older browsers.
showModal() or show()
There are two ways to open a dialog:
showModal()opens the dialog as a modal. There is a dark background behind the dialog (called the backdrop). The rest of the page can't be clicked or reached with the Tab key (the keyboard key that moves between buttons and fields). Focus is locked inside the dialog, and Esc closes the dialog.show()opens a plain dialog, not a modal. There is no dark background, the page behind it can still be used, and Esc doesn't close it.
If the visitor has to answer before continuing, use showModal().
Closing a dialog
There are two ways. First, a button inside a <form method="dialog">, like the example above, with no JavaScript. Second, run .close() from a button:
<button onclick="document.getElementById('confirmation').close()">Close</button>
⚠️ Why won't the dialog appear, or act like a modal?
- The dialog doesn't appear. Check that the open button runs
showModal(), and that the id written ingetElementByIdis exactly the same as the dialog's id. - The open button placed inside the dialog. The button is hidden together with the dialog, so nobody can click it. Put it outside the dialog.
- Thinking
<dialog open>is a modal. Theopenattribute only shows the dialog likeshow()does: no dark background, and Esc doesn't close it. - A hash sign in
getElementById. WritegetElementById('confirmation'), notgetElementById('#confirmation'). The hash sign (#) is part of CSS selectors (the way CSS points at an element), andgetElementByIdonly needs the id itself.
💡 You can style the dark background. Use the CSS
dialog::backdrop { background: rgb(0 0 0 / 0.5); }to change the colour behind a modal.
TL;DR: <dialog id="..."> is a box on top of the page that stays hidden until it is opened. An open button outside the dialog runs showModal(). This opens the dialog as a modal: there is a dark background, the focus (what you type) is locked inside the dialog, and Esc closes it. show() only shows a plain dialog. Close a dialog with a button inside <form method="dialog"> or with .close().
⚡ A box on top of the page
Write:
<button onclick="document.getElementById('confirmation').showModal()">Delete account</button>
<dialog id="confirmation">
<h2>Delete the account?</h2>
<p>All your notes go with it.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="delete">Delete</button>
</form>
</dialog>
Meaning: A <dialog> is a box on top of the page that stays hidden until it's opened. Its open button is outside the dialog: the button finds the dialog by its id, then runs showModal().
⚡ showModal() or show()
Guess first: opened with show(), can the page behind still be clicked?
Yes. show() only shows a plain dialog: no dark background, and Esc doesn't close it.
Meaning: showModal() opens a modal: there is a dark background, the page behind can't be clicked, the focus (what you type) is locked inside the dialog, and Esc closes the dialog. If the visitor has to answer before continuing, use showModal().
⚡ Closing a dialog
Meaning: A button inside <form method="dialog"> closes the dialog with no JavaScript, and the clicked button's value is saved in returnValue. The other way: run .close() from a button's onclick.