HTML <dialog> Tag

The HTML <dialog> tag indicates a part of an application that the user can interact with to perform a task. For example a dialog box, inspector, or window.

The <dialog> element accepts a boolean attribute called open that sets the element to "active" and allows users to interact with it. If the attribute is omitted, you will need to use a script (such as JavaScript) to enable the dialog to open and close as required.

Syntax

The <dialog> tag is written as <dialog id=""></dialog> with the dialog content inserted between the start and end tags. The id attribute can be used to associate a script with the <dialog> element.

Like this:

Examples

Basic tag usage

Here's an example of the <dialog> element being used in its most basic form. The open attribute has been added, which means that the dialog is already open upon the page loading.

Opening/Closing the Dialog

In order for the user to be able to interact with the dialog box properly, you'll need to use a script (such as JavaScript). The HTMLDialogElement interface provides a number of methods and properties that allow you to control the dialog box (more about these below, under "The HTMLDialogElement Interface").

Styles

You can add styles to the <dialog> element by using CSS.

Modal Dialogs

You can create modal dialogs by using the showModal() method (as opposed to the show() method).

Modal dialogs create a mode where the main window can't be used until the user has interacted with the dialog (i.e. closed it). Most browsers tint the background color to a darker shade, which draws attention to the dialog box.

Note that you still need to use the close() method to close the dialog (i.e. there is no closeModal() method).

Styling Modal Dialogs

You can use the CSS ::backdrop pseudo class to change the color of the modal backdrop. That is, you can change the color or shading that the browser gives to the rest of the browser window when the modal dialog is open.

For example, dialog::backdrop {background-color: rgba(0, 255, 0, 0.5);} will make the backdrop green, with an opacity of 0.5. Like this:

The HTMLDialogElement Interface

As seen in the above examples, you can use JavaScript to control the <dialog> element. This is achieved via the HTMLDialogElement interface.

For more information on the HTMLDialogElement interface, see HTML5 <dialog> Tag.

Attributes

Attributes can be added to an HTML element to provide more information about how the element should appear or behave.

The <dialog> element accepts the following attributes.

AttributeDescription
openWhen this attribute is present, the <dialog> element is interactive and the user can interact with it. When the open attribute is not present, the <dialog> element is hidden from the user.

Global Attributes

The following attributes are standard across all HTML elements. Therefore, you can use these attributes with the <dialog> tag (except for tabindex), as well as with all other HTML tags.

Note that the tabindex attribute does not apply to <dialog> elements.

For a full explanation of these attributes, see HTML 5 global attributes.

Event Handlers

Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.

Most event handler content attributes can be used on all HTML elements, but some event handlers have specific rules around when they can be used and which elements they are applicable to.

For more detail, see HTML event handler content attributes.