The Dialog Element — HTML

<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…

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:

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:

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?

💡 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.

What you will learn