SHARE
Facebook X Pinterest WhatsApp

Simple Guide to HTML Dialog Element

Written By
thumbnail
Bradley Jones
Bradley Jones
Aug 26, 2019

The <dialog> element was added to HTML in order to identify a part of a web page or application that a user interacts with such as a dialog box, window, or inspector.

This is a newer element, so it is not supported in all browsers. At the time this article was written, <dialog> was supported in the following:

  • Chrome
  • Chrome Android
  • Android WebView
  • Opera
  • Samsung Internet Android

Note that this was not supported in Firefox, Edge, Safari, or Internet Explorer at the time this was written. Because <dialog> is not yet supported by all browsers, if you choose to use it, you should include fallback logic.

The best way to learn about an HTML element is to jump into a bit of markup code! Listing 1 presents a very simple listing that you can launch in your browser to see the basics of this element in action.

Listing 1: Using the HTML <dialog> element

<html>
<body>
<h1>A Simple Dialog....</h1>
<dialog open>
  <p>Greetings, one and all!</p>
</dialog>
</body>
</html>

In this listing, you can see that a simple page is presented with a single heading and a paragraph greeting that is embedded in a dialog box. When you execute this on a browser that supports this new command you will see something similar to Figure 1. On a browser that is not supported, the <dialog> element will be ignored and you’ll either not see the greeting, or it will be presented as standard text after the heading.

HTML_dialog_element_1
Figure 1: Basic use of HTML <dialog>

There is one more important item you should have noted in Listing 1. The dialog element has an attribute included. If you don’t include an “open” attribute to state that the dialog should be rendered as open, then the assumption will be for a closed dialog and thus it won’t be displayed. You can try this by removing the “open” from the code and rendering the page again.

The dialog presented in Listing 1 simply displayed text. The <dialog> element can contain more complex markup. One of the intents is to allow webpages to create and display popup or model windows without launching a new browser tab or window. This is helpful when building mobile applications. This functionality can be accomplished by combining a little bit of JavaScript with the <dialog> element as shown in Listing 2.

Listing 2: Opening and Closing an HTML <dialog> element

<html>
<body>
<h1>My Page with a Dialog!</h1>
<hr />

<dialog> 
  <p>This is informative information, a menu, or 
  some other functionality you want in a model window 
  or dialog!</p> 
  <button id="closeMe">Close Dialog</button> 
</dialog> 

<button id="openMe">Show Me the Dialog!</button> 

<script>  
  var dialog = document.querySelector('dialog'); 
  document.querySelector('#openMe').onclick = function() 
  { 
     dialog.show(); 
  }; 
  document.querySelector('#closeMe').onclick = function() 
  { 
     dialog.close(); 
  }; 
</script>
<p>This web page shows a simple pop-up dialog</p>
</body>
</html>

In this new listing, you can see that a <dialog> element is included without the ‘open’ attribute. This means the dialog will not be displayed initially as shown in Figure 2.

HTML_dialog_element_2
Figure 2: A webpage with a closed dialog

In this example, the dialog is opened using a bit of JavaScript. By clicking on the “Show Me the Dialog” button, you cause code to execute that opens the dialog with a call to a “show()” method. This method is basically the same as setting the attribute of the dialog to “open”. Once you click the button and call show(), you end up with a page that looks like Figure 3.

HTML_dialog_element_3
Figure 3: The opened dialog

The dialog in this listing contains more than just text. It also includes a button that is also associated to the JavaScript in the listing. Thus, when you click on the “Close Dialog” button, a call to the close() method of the dialog is executed and the dialog shuts down.

With a few lines of code that include some JavaScript and the <dialog> element, you can create pop-ups that stay within the context of the current webpage. This functionality should help make user experiences much better on mobile web apps once it has rolled out to all of the major browsers. Until then, as with any new functionality use with care by making sure you check for compatibility!

Recommended for you...

Best VR Game Development Platforms
Enrique Corrales
Jul 21, 2022
Best Online Courses to Learn HTML
Ronnie Payne
Jul 7, 2022
Working with HTML Images
Octavia Anghel
Jun 30, 2022
Web 3.0 and the Future Of Web Development
Rob Gravelle
Jun 23, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.