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
HTML Forms
HTML forms are used to collect user input and send it to a server for processing. They contain interactive controls like text fields, checkboxes, radio buttons, and submit buttons. Forms are a fundamental part of web interactivity, enabling everything from login pages and search bars to complex data entry interfaces.<a>
The <a> (anchor) element creates hyperlinks that allow users to navigate between pages, sections, or external resources. Links are fundamental to the web, enabling the interconnected nature of websites. The href attribute specifies the destination, while the link text tells users what to expect when they click.<details>, <summary>
The <details> element creates a native, collapsible disclosure widget — content that can be toggled open or closed by the user without any JavaScript. The <summary> element inside it defines the always-visible heading that acts as the toggle control. This is the standard way to build FAQ accordions or collapsible sections without hand-rolling the interaction logic.<progress>, <meter>
These two elements display quantities visually without needing custom-built progress bars. <progress> represents the completion progress of a task, like a file upload or multi-step form. <meter> represents a scalar value within a known range, like disk usage or a rating — it is for measuring a static value, not tracking progress toward completion.
HTML forms are used to collect user input and send it to a server for processing. They contain interactive controls like text fields, checkboxes, radio buttons, and submit buttons. Forms are a fundamental part of web interactivity, enabling everything from login pages and search bars to complex data entry interfaces.<a>
The <a> (anchor) element creates hyperlinks that allow users to navigate between pages, sections, or external resources. Links are fundamental to the web, enabling the interconnected nature of websites. The href attribute specifies the destination, while the link text tells users what to expect when they click.<details>, <summary>
The <details> element creates a native, collapsible disclosure widget — content that can be toggled open or closed by the user without any JavaScript. The <summary> element inside it defines the always-visible heading that acts as the toggle control. This is the standard way to build FAQ accordions or collapsible sections without hand-rolling the interaction logic.<progress>, <meter>
These two elements display quantities visually without needing custom-built progress bars. <progress> represents the completion progress of a task, like a file upload or multi-step form. <meter> represents a scalar value within a known range, like disk usage or a rating — it is for measuring a static value, not tracking progress toward completion.