How to display a dialog box on the page?

The dialog box is an essential UI component that provides feedback to users, collects input, or displays important information. HTML offers multiple ways to implement dialog boxes, from the native <dialog> element to traditional JavaScript methods like prompt() and confirm().

Dialog boxes serve various purposes including user confirmations, data input forms, notifications, and authentication prompts. They help create focused user interactions by overlaying content above the main page.

Syntax

Following is the syntax for the HTML <dialog> element

<dialog id="dialogId">
   Dialog content here
</dialog>

To control the dialog programmatically

// Open dialog as modal
dialog.showModal();

// Open dialog as non-modal
dialog.show();

// Close dialog
dialog.close();

Using the HTML Dialog Element

The HTML <dialog> element provides a semantic way to create modal and non-modal dialog boxes. It includes built-in accessibility features, backdrop styling, and focus management. The element supports methods like showModal() for modal dialogs and show() for non-modal dialogs.

Example Basic Dialog Box

Following example demonstrates a simple dialog box with open and close functionality

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Basic Dialog Box</title>
   <style>
      dialog {
         border: 2px solid #4CAF50;
         border-radius: 8px;
         padding: 20px;
         font-family: Arial, sans-serif;
         max-width: 400px;
      }
      dialog::backdrop {
         background-color: rgba(0, 0, 0, 0.5);
      }
      button {
         padding: 8px 16px;
         margin: 5px;
         border-radius: 4px;
         border: 1px solid #ccc;
         cursor: pointer;
      }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h3>HTML Dialog Element Demo</h3>
   <button id="open-dialog">Open Dialog</button>

   <dialog id="my-dialog">
      <h2>Welcome to TutorialsPoint</h2>
      <p>This is a native HTML dialog box with semantic markup and built-in accessibility features.</p>
      <button id="close-dialog">Close</button>
   </dialog>

   <script>
      const openButton = document.getElementById('open-dialog');
      const dialog = document.getElementById('my-dialog');
      const closeButton = document.getElementById('close-dialog');

      openButton.addEventListener('click', () => {
         dialog.showModal();
      });

      closeButton.addEventListener('click', () => {
         dialog.close();
      });

      // Close dialog when clicking backdrop
      dialog.addEventListener('click', (e) => {
         if (e.target === dialog) {
            dialog.close();
         }
      });
   </script>
</body>
</html>

The dialog appears as a modal overlay with a semi-transparent backdrop. Users can close it by clicking the close button or clicking outside the dialog area.

HTML Dialog Element Demo
[Open Dialog]

(When clicked, shows modal dialog with green border:)
Welcome to TutorialsPoint
This is a native HTML dialog box with semantic markup and built-in accessibility features.
[Close]

Using Window Dialog Methods

JavaScript provides built-in methods like alert(), confirm(), and prompt() for simple dialog interactions. These create native browser dialogs that are automatically styled but offer limited customization.

Example Window Dialog Methods

Following example demonstrates different types of JavaScript dialog methods

<!DOCTYPE html>
<html lang="en">
<head>
   <title>JavaScript Dialog Methods</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h3>JavaScript Dialog Methods</h3>
   <button onclick="showAlert()">Show Alert</button>
   <button onclick="showPrompt()">Show Prompt</button>
   <button onclick="showConfirm()">Show Confirm</button>
   <p id="result"></p>

   <script>
      function showAlert() {
         alert("Welcome to TutorialsPoint!");
         document.getElementById('result').textContent = "Alert displayed";
      }

      function showPrompt() {
         const name = prompt("Enter your name:");
         if (name !== null && name !== "") {
            document.getElementById('result').textContent = "Hello, " + name + "!";
         } else {
            document.getElementById('result').textContent = "Prompt canceled or empty";
         }
      }

      function showConfirm() {
         const result = confirm("Do you want to continue learning HTML?");
         if (result === true) {
            document.getElementById('result').textContent = "Great! Keep learning.";
         } else {
            document.getElementById('result').textContent = "Maybe next time.";
         }
      }
   </script>
</body>
</html>

Each method serves a different purpose: alert() displays information, prompt() collects user input, and confirm() gets yes/no responses from users.

JavaScript Dialog Methods
[Show Alert] [Show Prompt] [Show Confirm]

(Result text appears here based on user interaction)

Custom Styled Dialog

For more control over appearance and behavior, custom dialog implementations using HTML, CSS, and JavaScript provide flexibility while maintaining accessibility standards.

Example Form Dialog

Following example shows a dialog containing a form for user input

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Form Dialog Example</title>
   <style>
      dialog {
         border: none;
         border-radius: 10px;
         box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
         padding: 0;
         max-width: 350px;
      }
      dialog::backdrop {
         background-color: rgba(0, 0, 0, 0.6);
      }
      .dialog-header {
         background-color: #2196F3;
         color: white;
         padding: 15px 20px;
         margin: 0;
         border-radius: 10px 10px 0 0;
      }
      .dialog-content {
         padding: 20px;
      }
      .form-group {
         margin-bottom: 15px;
      }
      label {
         display: block;
         margin-bottom: 5px;
         font-weight: bold;
      }
      input[type="text"], input[type="email"] {
         width: 100%;
         padding: 8px;
         border: 1px solid #ddd;
         border-radius: 4px;
         box-sizing: border-box;
      }
      .dialog-actions {
         text-align: right;
         margin-top: 20px;
      }
      button {
         padding: 8px 16px;
         margin-left: 10px;
         border: none;
         border-radius: 4px;
         cursor: pointer;
      }
      .primary { background-color: #2196F3; color: white; }
      .secondary { background-color: #f1f1f1; color: #333; }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h3>User Registration Dialog</h3>
   <button onclick="openRegistration()">Open Registration</button>
   <p id="output"></p>

   <dialog id="registration-dialog">
      <h3 class="dialog-header">User Registration</h3>
      <div class="dialog-content">
         <form id="registration-form">
            <div class="form-group">
               <label for="username">Username:</label>
               <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
               <label for="email">Email:</label>
               <input type="email" id="email" name="email" required>
            </div>
            <div class="dialog-actions">
               <button type="button" class="secondary" onclick="closeRegistration()">Cancel</button>
               <button type="submit" class="primary">Register</button>
            </div>
         </form>
      </div>
   </dialog>

   <script>
      const registrationDialog = document.getElementById('registration-dialog');
      const registrationForm = document.getElementById('registration-form');
      const output = document.getElementById('output');

      function openRegistration() {
         registrationDialog.showModal();
         document.getElementById('username').focus();
      }

      function closeRegistration() {
         registrationDialog.close();
         registrationForm.reset();
      }

      registrationForm.addEventListener('submit', (e) => {
         e.preventDefault();
         const username = document.getElementById('username').value;
         const email = document.getElementById('email').value;
         output.textContent = `Registration successful! Welcome, ${username} (${email})`;
         closeRegistration();
      });
   </script>
</body>
</html>

This example demonstrates a professional-looking registration dialog with form validation, proper focus management, and styled appearance.

User Registration Dialog
[Open Registration]

(Dialog shows with blue header, form fields for username/email, and Cancel/Register buttons)
Registration successful! Welcome, john (john@email.com)
Dialog Implementation Methods HTML Dialog ? Semantic HTML ? Built-in accessibility ? Backdrop support ? Focus management ? Custom styling JS Methods ? alert(), confirm() ? prompt() ? Native browser UI ? Simple to use ? Limited styling Custom Dialog ? Full control ? Advanced styling ? Complex interactions ? More development ? Framework needed

Browser Compatibility

Updated on: 2026-03-16T21:38:54+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements