HTML dialog Tag: How to Create Dialog Boxes

html dialog tag

The <dialog> tag is used to build modal popups in HTML. It shows alerts and messages without third-party scripts.

Understand the <dialog> Tag in HTML

The <dialog> tag creates a pop-up box. It stays above the rest of the page. It works with or without user input. You can place forms or messages inside it.

  • A modal dialog blocks page actions and forces the user to respond before going back.
  • A non-modal dialog stays open and allows user input on the rest of the page.

Here are the benefits of the native dialogs:

  • Works without extra scripts
  • Manages focus automatically
  • Closes on Esc or background click
  • Accepts forms with a return value

The basic tag:

<dialog>Dialog content</dialog>

Required and optional parts:

  • Start with <dialog> and end with </dialog>
  • The open attribute shows the dialog
  • Add buttons or forms inside

You can add the open attribute in HTML or with JavaScript. The dialog becomes visible.

For example:

<dialog open>This box shows</dialog>

Use JavaScript to show or hide it:

dialog.setAttribute("open", "");
dialog.removeAttribute("open");

How to Show and Close a Dialog

Use .show() for non-modal:

This opens the dialog but allows background interaction.

dialog.show();

Use .showModal() for modal:

This opens the dialog and blocks background input.

dialog.showModal();

Use .close() to dismiss:

Call this method to hide the dialog.

dialog.close();

Close the dialog on Esc or click:

Modal dialogs close on Esc or background click, and the browsers handle this. You can block this when you click on cancel events.

Use Forms Inside <dialog> Tag in HTML

Add a form with method=”dialog”:

This skips a full page reload and returns a value.

<form method="dialog">
  <button value="yes">Yes</button>
  <button value="no">No</button>
</form>

Capture user choice with returnValue:

Use the returnValue to detect the selected option.

dialog.addEventListener("close", () => {
 console.log(dialog.returnValue)
});

Validate or skip validation on close:

Use standard forms for validation. Use dialog.close() to skip checks.

The closedby Attribute

This attribute tracks what closed the dialog. It shows how the user exited.

Here is a list that shows you the potential value for closedby attribute:

  • none: ignore Esc or background clicks
  • closerequest: allow Esc or background
  • any: accept all closing methods

Use it to decide how to process the dialog exit.

Here is an example:

<dialog closedby="closerequest"></dialog>

Style the Dialog and Accessibility

Here you can use the backdrop (::) for overlay. To do that, add a dark layer behind the modal.

dialog::backdrop {
  background: rgba(0, 0, 0, 0.5);
}

Use CSS to fade dialogs in or out for transitions and animations:

@keyframes showUp {
  from { opacity: 0; }
  to { opacity: 1; }
}

dialog[open] {
  animation: showUp 0.3s ease;
}

Keyframe example for open/close

@keyframes hideFade {
  from { opacity: 1; }
  to { opacity: 0; }
}

You can assign this before removing the dialog.

Move focus to the dialog or a button inside.

dialog.querySelector("button").focus();

Use autofocus on key elements

<input autofocus>

This puts the cursor in the input box.

Screen readers detect the dialog as active. Hide background content.

<dialog aria-modal="true"></dialog>
<main inert></main>

Open and close with buttons

document.getElementById("openBtn").onclick = () => dialog.showModal();
document.getElementById("closeBtn").onclick = () => dialog.close();

Listen for when the user exits the dialog.

dialog.addEventListener("close", () => {
  console.log("User exited with:", dialog.returnValue);
});

Stop the form from submitting by default.

form.onsubmit = (e) => {
  e.preventDefault();
};

HTML <dialog> Tag Browser Support

Supported browsers and versions

BrowserVersion
Chrome37+
Edge79+
Firefox98+
Safari15.4+

Chrome supports closedby attribute. Other browsers do not.

Use a fixed-position div with custom styles and hidden classes and JavaScript to mimic modal behavior.

You can also use the dialog for these cases:

  • Confirm delete or cancel
  • Show login or register forms
  • Display settings or alerts

Examples of HTML <dialog> Tag

Confirm User Action

<dialog id="confirmBox">
  <p>Delete file?</p>
  <form method="dialog">
    <button value="yes">Yes</button>
    <button value="no">No</button>
  </form>
</dialog>

The dialog opens and shows a yes/no choice. The close event captures the user’s choice.

Input Field With Return Value

<dialog id="inputBox">
  <form method="dialog">
    <label>Username: <input name="username"></label>
    <button value="submit">Submit</button>
  </form>
</dialog>

This shows a form with a field. You log the form data after submission.

Auto Show Modal on Load

window.onload = () => {
  document.getElementById("autoModal").showModal();
};

This opens a modal once the page loads. Use this for notices.

Custom Button to Close

<dialog id="noticeBox">
  <p>New update available.</p>
  <button onclick="noticeBox.close()">Close</button>
</dialog>

Click the button to close the box using JavaScript.

Wrapping Up

In this article, you learned how the <dialog> tag creates pop-up boxes. You also saw how to show, style, and close them with forms or buttons.

Here is a quick recap:

  • Use .show() or .showModal() to control dialogs
  • Add <form method="dialog"> to return a value
  • Listen for the close event to process input
  • Style the backdrop with ::backdrop
  • Add autofocus, aria-modal, and inert for better access

FAQs

How do I open a <dialog> with JavaScript?

Use this method:
document.querySelector("dialog").showModal();

What is the difference between <dialog>.show() and .showModal()?

.show() displays the dialog but allows interaction with the background page. .showModal() displays the dialog as a modal and blocks interaction with the rest of the page.

How can I read the selected value in a <dialog>?

Use the returnValue property:
dialog.addEventListener("close", () => {
  console.log(dialog.returnValue);
});

Can I use a form inside a <dialog>?

Yes. Use method="dialog" and buttons with values.

Similar Reads

HTML object Tag: How to Embed External Objects with Examples

Use the HTML object tag to embed different types of external files into your webpage. It works for videos and…

HTML link Tag: Linking Stylesheets

The link tag in HTML connects a web page to external resources like stylesheets or icons. It's placed in the…

HTML Mark Tag: How to Highlight Text

The mark tag highlights text in HTML and shows that the text has relevance in the content. It adds a…

HTML autocomplete Attribute: How to Use it with form and input

The form autocomplete attribute in HTML helps users fill out forms faster because it shows them saved input for common…

How to Build Responsive HTML Email Templates

Email templates are still the key tools for work and brands in HTML. A simple email can reach many people,…

HTML Link Attributes Guide for Beginners

HTML link attributes tell the browser how a link works. They set where the link goes, how it opens, and…

HTML Aside Tag: How to Create a Side Content

You use the <aside> tag to mark extra content in HTML. It does not replace the main text and shows…

HTML Attributes with Examples for Beginners

HTML attributes add extra detail to elements so the browser knows how to handle them. Here you will see examples…

HTML lang Attribute: How it Works with Examples

The HTML lang attribute tells browsers and tools what language the page uses. It helps users and search engines understand…

Comments in HTML: How They Work with Examples

Comments in HTML help you explain your code. You can add notes without showing anything in the browser. Understand How…

Previous Article

HTML data Tag: Machine-Readable Data

Next Article

HTML details Tag: How to Toggle Hidden Content

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.