<dialog>
The dialog element creates a native HTML dialog box (modal or popup). You can control its visibility using the JavaScript methods showModal() and close(), or with the open attribute.
Syntax
<!-- Non-modal dialog -->
<dialog open>
<p>Dialog content</p>
</dialog>
<!-- Modal dialog (opened via JavaScript) -->
<dialog id="myDialog">
<p>Modal content</p>
<button id="closeBtn">Close</button>
</dialog>
<button id="openBtn">Open Dialog</button>
<script>
const dialog = document.getElementById('myDialog');
document.getElementById('openBtn').addEventListener('click', () => {
dialog.showModal(); // Open as a modal
});
document.getElementById('closeBtn').addEventListener('click', () => {
dialog.close(); // Close the dialog
});
</script>
Attributes and Methods
| Attribute / Method | Description |
|---|---|
| open | An HTML attribute that makes the dialog visible. Without this attribute, the dialog is hidden. |
| showModal() | Opens the dialog as a modal. A semi-transparent overlay is shown behind it, and content outside the dialog cannot be clicked. |
| show() | Opens the dialog as a non-modal. Content behind the dialog remains interactive. |
| close() | Closes the dialog. An optional argument sets the value of the returnValue property. |
| returnValue | A property that holds the return value when the dialog is closed. It is typically set to the value of the submit button that was clicked. |
| ::backdrop | A pseudo-element that lets you style the background overlay displayed behind a modal dialog using CSS. |
Sample Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
/* Dialog styles */
dialog {
border: none;
border-radius: 8px;
padding: 24px;
box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
max-width: 400px;
width: 90%;
}
dialog h2 {
margin-top: 0;
}
/* Background overlay */
dialog::backdrop {
background: rgba(0, 0, 0, 0.5);
}
.dialog-buttons {
display: flex;
gap: 8px;
justify-content: flex-end;
margin-top: 16px;
}
</style>
</head>
<body>
<button id="openBtn">Confirm Delete</button>
<!-- Confirmation dialog -->
<dialog id="confirmDialog">
<h2>Are you sure you want to delete?</h2>
<p>This action cannot be undone.</p>
<div class="dialog-buttons">
<!-- Using a form tag allows the dialog to be closed with Enter or a submit button -->
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="confirm">Delete</button>
</form>
</div>
</dialog>
<p id="result"></p>
<script>
const dialog = document.getElementById('confirmDialog');
const result = document.getElementById('result');
// Open the dialog
document.getElementById('openBtn').addEventListener('click', () => {
dialog.showModal();
});
// Handle the dialog closing
dialog.addEventListener('close', () => {
if (dialog.returnValue === 'confirm') {
result.textContent = 'Deleted.';
} else {
result.textContent = 'Cancelled.';
}
});
</script>
</body>
</html>
Result
Clicking the "Confirm Delete" button dims the background and displays the modal dialog in the center of the screen. Clicking "Delete" shows "Deleted." on the page, and clicking "Cancel" shows "Cancelled."
<!-- Flow --> Click "Confirm Delete" button ↓ ┌─────────────────────┐ │ Are you sure you want to delete? │ │ This action cannot be undone. │ │ [Cancel] [Delete] │ └─────────────────────┘ (Background is dimmed and non-clickable) Click "Delete" → Dialog closes → "Deleted." is shown Click "Cancel" → Dialog closes → "Cancelled." is shown
Overview
The dialog element, introduced in HTML 5.2, lets you create confirmation dialogs and modal windows natively in HTML — no JavaScript or CSS workarounds required. Previously, you had to build modals manually using div elements and CSS, or rely on plugins like jQuery UI. The dialog element handles accessibility automatically, including focus management, keyboard navigation, and screen reader support.
A modal dialog opened with showModal() closes automatically when the user presses the Escape key. Focus is also trapped inside the dialog (focus trap), so pressing Tab cannot move focus to content behind it. These behaviors are critical for accessibility, and while replicating them in a custom div-based modal would require a significant amount of JavaScript, the dialog element handles all of this natively in the browser.
Placing a form method="dialog" inside the dialog makes form submission trigger the dialog's close() method. The value attribute of the submit button that was clicked is stored in dialog.returnValue, so you can determine in JavaScript which button closed the dialog. The ::backdrop pseudo-element lets you freely customize the overlay style shown behind the modal.
Browser Compatibility
36 or earlier ×
Android Browser
42+ ○
36 or earlier ×
Chrome Android
42+ ○
36 or earlier ×
Firefox Android
103+ ○
97 or earlier ×If you find any errors or copyright issues, please contact us.