HTML <dialog> Tag

The <dialog> tag is used to create a native dialog box, with which the user interacts, performing certain actions. For example, with this element, you can create pop-up messages or forms. By default, the dialog box is hidden from view, it is activated by the open attribute.

Syntax

The <dialog> tag comes in pairs, the content is placed between the opening (<dialog>) and the closing (</dialog>) tags.

In order to display/hide the content, we need Javascript API.

Example

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      dialog {
        width: 40%;
      }
    </style>
  </head>
  <body>
    <div>
      <dialog id="DialogExample">
        <p>
          Here is some text for example.
        </p>
        <button id="hide">Close dialog text</button>
      </dialog>
      <button id="show">Show dialog text</button>
    </div>
    <script type="text/JavaScript">
      (function() {  
             var dialog = document.getElementById('DialogExample');  
             document.getElementById('show').onclick = function() { dialog.show();  
         }; 
           document.getElementById('hide').onclick = function() { dialog.close();  
        }; 
      })();
    </script>
  </body>
</html>

Result

Attributes

Attribute Value Description
open open Indicates that the dialog box is active, and the user can interact with it.

The <dialog> tag also supports the Global Attributes and the Event Attributes.

Browser support

37+ 59+ 6+ 24+