1. html
  2. /tags
  3. /dialog

<dialog>

Definition

The <dialog> element is an element in HTML that represents a dialog or conversation. It can be used to display a message or a form, for example, and is often used in conjunction with JavaScript to create interactive user experiences.

The <dialog> element is typically used to contain other HTML elements, such as text or form fields, which are used to create the dialog or conversation. It can also be used to create modal dialogs, which are dialogs that take up the entire screen and must be dismissed before the user can interact with the rest of the page.

Examples

Here is a demo of the <dialog> element in use:

<dialog id="myDialog">
	<h1>Hello World!</h1>
	<p>This is a simple dialog box.</p>
</dialog>

This example creates a <dialog> element with an id of "myDialog" and contains a heading and a paragraph inside. To make the dialog visible to the user, you can use JavaScript to call the show() method on the dialog element, like this:

const dialog = document.getElementById("myDialog");
dialog.show();

This will make the dialog appear on the screen, allowing the user to view and interact with it. To hide the dialog, you can call the close() method on the dialog element, like this:

const dialog = document.getElementById("myDialog");
dialog.close();

This will make the dialog disappear from the screen. The <dialog> element provides a simple and convenient way to create dialogs and modal dialogs in HTML.

Attributes

AttributeDescriptionDeprecated
openSpecifies whether the dialog is currently open or not.No
returnValueSpecifies the return value of the dialog when it is closed.No
methodSpecifies the method to use when submitting a form inside the dialog.Yes
actionSpecifies the URL to submit the form data to when the dialog is closed.Yes
enctypeSpecifies the encoding type to use when submitting the form data.Yes
autocompleteSpecifies whether the form inside the dialog should have autocomplete enabled or not.Yes
accept-charsetSpecifies the character sets to use when submitting the form data.Yes

Note that some of the attributes, such as method, action, enctype, autocomplete, and accept-charset, are deprecated and should not be used in new developments. These attributes were used in older versions of HTML to specify form-related properties, but have been replaced by more modern approaches in HTML5.

Best Practices

  • Use the <dialog> element to create a dialog box or popup window that is to be used for short messages or interactions.
  • Use the open attribute to specify whether the dialog box is visible or not.
  • Use the <dialog> element in combination with other semantic elements, such as <form>, <button>, and <label>, to create a meaningful and interactive dialog box.
  • Use CSS to style the <dialog> element and its contents to create a consistent and user-friendly design.
  • Use JavaScript to manipulate the open attribute of the <dialog> element in order to control the visibility of the dialog box.
  • Use appropriate ARIA attributes, such as role and aria-label, to make the dialog box accessible to users of assistive technologies.

Accessibility Considerations

When designing a page that uses the <dialog> element, it is important to consider accessibility for users with disabilities. This includes making sure that the content inside the <dialog> element is properly marked up and can be accessed by assistive technologies such as screen readers.

Here are a few specific considerations to keep in mind:

  • Use appropriate semantic elements to mark up the content inside the <dialog> element. This includes using heading elements to structure the content and using <label> elements for form fields.
  • Make sure the <dialog> element has a title attribute that accurately describes the content inside it. This will be used by screen readers to identify the <dialog> element and provide context to users.
  • Use the aria-labelledby attribute to associate the <dialog> element with the element that contains its title. This will help screen readers properly announce the title and content of the <dialog> element.
  • Use the aria-describedby attribute to associate the <dialog> element with an element that contains a description of its content. This will provide additional context to users of assistive technologies.
  • Use the tabindex attribute to specify the order in which the focus should move when the user navigates using the keyboard. This will ensure that users can easily access the content inside the <dialog> element.

Browser Compatibility

ChromeFirefoxSafariInternet ExplorerMicrosoft EdgeOpera
YesYesYesYesYesYes