HTML - <dialog> tag



HTML <dialog> tag is used to represent the dialog box, alert box, or subwindow.

It has an attribute named open, which indicates that the dialog is active and can be interacted with. If the open attribute is not set, the dialog box will not appear on the screen view, and the user cannot see it. You can use the dialog element to give some alert messages to the user on clicking any form button.

Syntax

   <dialog>.....</dialog>

Attribute

HTML data tag supports Global and Event attributes of HTML. Accept a specific attribute as well which is listed below.

Attribute Value Description
open open Indicates that the dialog is active and can be interacted with.

Examples of HTML dialog Tag

Bellow examples will illustrate the usage of dialog tag. Where, when and how to use dialog tag to create dialog box, alert box, or subwindow.

Creating Dialog Box

The following program illustrates the use of the HTML <dialog> tag. We are not using an 'open' attribute inside <dialog> tag. So after the execution of the program, no dialog box will be displayed because, by default, it is closed.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML dialog box</title>
</head>
<body>
   <!--create a dialog element-->
   <p>Example of the HTML 'dialog' element</p>
   <dialog>Alert</dialog>
</body>
</html>

Default Open Dialog Box

Following is another example of the HTML <dialog> box. Here, we are creating a dialog box using the <dialog> tag. We use the ‘open’ attribute inside the ‘dialog’ element, so when we execute the program a dialog box will be displayed on the screen view.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML dialog box</title>
</head>
<body>
   <p>Example of the HTML 'dialog' element</p>
   <!--create a dialog element-->
   <dialog open>Dialog box!</dialog>
</body>
</html>

Styling Dialog Box

In the following example, we are creating a dialog box using the HTML <dialog> tag. We use the CSS to style the created dialog box.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML dialog box</title>
   <style>
      dialog {
         width: 100px;
         height: 50px;
         background-color: red;
         color: white;
         display: grid;
         place-items: center;
         border-radius: 5px;
      }
   </style>
</head>
<body>
   <p>Example of the HTML 'dialog' element</p>
   <!--create a dialog element-->
   <dialog open>Alert box!</dialog>
</body>
</html>

Creating a Fomr in Dialog Box

In this example, we are using the HTML <dialog> tag to create a dialog box, and we create a "form" within the dialog element that contains a button named "OK", the dialog box will disappear when the user clicks on the OK button.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML dialog box</title>
   <style>
      dialog {
         background-color: red;
         color: white;
      }
   </style>
</head>
<body>
   <p>Example of the HTML 'dialog' element</p>
   <!--create a dialog element-->
   <dialog open>
      <p>Warning!</p>
      <form method="dialog">
      <button>Ok</button>
      </form>
   </dialog>
</body>
</html>

Supported Browsers

Tag Chrome Edge Firefox Safari Opera
dialog Yes 37.0 Yes 79.0 Yes 98.0 Yes 15.4 Yes 24.0
html_tags_reference.htm
Advertisements