Codectionary / Developer documentation / HTML

<dialog>

The <dialog> element represents a native modal or non-modal dialog box, such as a confirmation prompt, settings panel, or alert. It comes with built-in modal behavior (via the showModal() JavaScript method) including a backdrop and focus trapping, removing the need to hand-build accessible modal logic from scratch.

Syntax

<dialog id="myDialog">
  <p>Dialog content</p>
  <button onclick="this.closest('dialog').close()">Close</button>
</dialog>

Examples

Basic Modal Dialog

A dialog opened and closed via JavaScript.

<dialog id="confirmDialog">
  <p>Are you sure you want to delete this item?</p>
  <button onclick="document.getElementById('confirmDialog').close()">Cancel</button>
  <button onclick="document.getElementById('confirmDialog').close()">Delete</button>
</dialog>
<button onclick="document.getElementById('confirmDialog').showModal()">Delete Item</button>

Non-modal Dialog

Using show() instead of showModal() for a non-blocking dialog.

<dialog id="tipDialog">
  <p>Tip: You can use keyboard shortcuts to navigate faster.</p>
</dialog>
<script>
  document.getElementById("tipDialog").show();
</script>

Styled Dialog

A dialog with custom appearance and backdrop styling.

<dialog style="border: none; border-radius: 12px; padding: 24px; max-width: 400px;">
  <h3>Settings</h3>
  <p>Dialog content goes here.</p>
</dialog>
<style>
  dialog::backdrop { background: rgba(0,0,0,0.5); }
</style>

Attributes

  • open (boolean): Reflects whether the dialog is currently visible

Best practices

  • Use showModal() rather than the open attribute directly when you need modal behavior with a backdrop and focus trapping
  • Always provide a clear way to close the dialog, such as a close button or the Escape key (built in by default)
  • Style the ::backdrop pseudo-element to dim the rest of the page behind a modal dialog
  • Prefer the native <dialog> over a hand-built modal — it handles focus management and accessibility concerns you would otherwise have to build yourself

At a glance

Purpose
Structure and meaning for web content
File extension
.html · .htm
Runs in
Web browsers
Usually used with
CSS and JavaScript

Specifications & further reading

Related HTML documentation