HTML - <dialog> tag



The 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.

Following are the supported attributes of the HTML <dialog> tag −

S.No Attribute & Description
1

open

Indicates that the dialog is active and can be interacted with

Syntax

Following is the syntax of the <dialog> tag −

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

Example

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>

When we run the above code, it will generate an output consisting of the text displayed on the webpage.

Example

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>

On running the above code, the output window will pop up, consisting of text along with a dialog box displayed on the webpage.

Example

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>

When we run the above code, it will generate an output displaying the text along with a CSS applied to it on the webpage.

Example

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>

When we run the above code, it will generate an output displaying the text along with a dialog box and a button on the webpage.

html_tags_reference.htm
Advertisements