Introduction to <dialog>tag

The <dialog>tag in HTML is a versatile element introduced in HTML5, designed to create interactive dialog boxes and modal windows on web pages. This element provides a structured and semantic way to display content such as alerts, confirmation messages, forms, and more, which require user interaction before continuing with the main page content.

Copy to clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>title of <dialog> tag</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="container">
      <h1>Welcome to Levoric Learn</h1>
      <button class="open-dialog-btn">Open Dialog</button>
  </div>

  <!-- Dialog Element -->
  <dialog class="custom-dialog" id="myDialog">
      <form method="dialog" class="dialog-content">
          <button type="button" class="dialog-close">&times;</button> <!-- Close button -->
          <h2>Subscribe to Our Newsletter</h2>
          <p>Stay updated with our latest news and offers. Enter your email below:</p>
          <input type="email" placeholder="Enter your email" required>
          <button type="submit" class="dialog-submit">Subscribe</button>
      </form>
  </dialog>
</body>
</html>
Copy to clipboard
/* General Styles */
    body {
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        background: linear-gradient(135deg, #f06, #4a90e2), url('levoric-learn-bg.png'); /* Vibrant gradient with custom background */
        background-size: cover, contain; /* Cover gradient and contain custom background */
        background-repeat: no-repeat; /* No repeat for the custom background */
        background-position: center; /* Center the custom background */
        color: #333;
        margin: 0;
        padding: 0;
        display: flex;
        justify-content: center; /* Center content horizontally */
        align-items: center; /* Center content vertically */
        height: 100vh; /* Full viewport height */
        overflow: hidden; /* Prevent scroll */
    }
    
    .container {
        text-align: center;
        background: rgba(255, 255, 255, 0.2); /* Semi-transparent background */
        backdrop-filter: blur(10px); /* Glassmorphism effect */
        padding: 30px;
        border-radius: 15px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2); /* Soft shadow for depth */
        position: relative; /* For positioning background elements */
        overflow: hidden;
    }
    
    h1 {
        color: #fff; /* White color for heading */
        margin-bottom: 20px;
        font-size: 2em;
        position: relative;
        z-index: 2; /* Ensure heading is above background elements */
    }
    
    .open-dialog-btn {
        padding: 12px 24px;
        font-size: 1em;
        color: #fff;
        background-color: #ff6347; /* Vibrant red-orange color */
        border: none;
        border-radius: 30px; /* Rounded button */
        cursor: pointer;
        box-shadow: 0 4px 20px rgba(255, 99, 71, 0.4); /* Vibrant shadow */
        transition: background-color 0.3s ease, box-shadow 0.3s ease; /* Smooth transition */
        z-index: 2; /* Ensure button is above background elements */
    }
    
    .open-dialog-btn:hover {
        background-color: #e5533d; /* Slightly darker on hover */
        box-shadow: 0 6px 25px rgba(255, 99, 71, 0.6); /* Deeper shadow on hover */
    }
    
    /* Dialog Styles */
    .custom-dialog {
        border: none;
        border-radius: 15px;
        padding: 0;
        max-width: 400px;
        width: 90%;
        box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3); /* Deeper shadow */
        background: rgba(255, 255, 255, 0.25); /* Semi-transparent background */
        backdrop-filter: blur(15px); /* Glassmorphism effect */
        animation: fadeInScale 0.4s ease-in-out; /* Enhanced fade-in animation */
        position: relative; /* For close button positioning */
        z-index: 3; /* Ensure dialog is above all other elements */
    }
    
    /* Close button styles */
    .dialog-close {
        position: absolute;
        top: 10px;
        right: 10px;
        background: transparent;
        border: none;
        font-size: 1.5em;
        color: #fff;
        cursor: pointer;
        transition: transform 0.3s ease; /* Smooth rotation transition */
    }
    
    .dialog-close:hover {
        transform: rotate(90deg); /* Rotate on hover for effect */
    }
    
    .dialog-content {
        padding: 30px; /* More padding for content */
        text-align: center;
    }
    
    .dialog-content h2 {
        color: #fff; /* White color for heading */
        margin-bottom: 15px;
        font-size: 1.7em; /* Larger font size for heading */
    }
    
    .dialog-content p {
        color: #eee; /* Light gray color for paragraph */
        margin-bottom: 20px;
    }
    
    .dialog-content input[type="email"] {
        width: 100%;
        padding: 12px;
        margin-bottom: 20px;
        border: none;
        border-radius: 25px;
        font-size: 1em;
        background-color: rgba(255, 255, 255, 0.8); /* Light background */
        color: #333;
        box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.1); /* Inner shadow for depth */
    }
    
    /* Dialog Submit Button */
    .dialog-submit {
        padding: 12px 24px;
        font-size: 1em;
        border: none;
        border-radius: 25px;
        background-color: #32cd32; /* Lime green color */
        color: #fff;
        cursor: pointer;
        box-shadow: 0 4px 15px rgba(50, 205, 50, 0.4); /* Soft shadow */
        transition: background-color 0.3s ease, box-shadow 0.3s ease; /* Smooth transition */
    }
    
    .dialog-submit:hover {
        background-color: #28a745; /* Darker green on hover */
        box-shadow: 0 6px 20px rgba(50, 205, 50, 0.6); /* Deeper shadow on hover */
    }
    
    /* Enhanced Fade-in Animation for Dialog */
    @keyframes fadeInScale {
        from {
            opacity: 0;
            transform: scale(0.8); /* Scale down effect */
        }
        to {
            opacity: 1;
            transform: scale(1); /* Full size */
        }
    }
    
    /* Decorative Elements */
    .container::before {
        content: '';
        position: absolute;
        width: 100px;
        height: 100px;
        background-color: rgba(255, 255, 255, 0.1); /* Light circle */
        border-radius: 50%;
        top: -20px;
        left: -20px;
        z-index: 1; /* Behind content */
    }
    
    .container::after {
        content: '';
        position: absolute;
        width: 200px;
        height: 200px;
        background-color: rgba(255, 255, 255, 0.1); /* Light circle */
        border-radius: 50%;
        bottom: -50px;
        right: -50px;
        z-index: 1; /* Behind content */
    }

Copy to clipboard

const dialog = document.getElementById('myDialog');
const openDialogBtn = document.querySelector('.open-dialog-btn');
const closeDialogBtn = document.querySelector('.dialog-close');

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

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

Global Attributes

AttributesDescription
accesskeySpecifies a shortcut key to activate or focus an element.
autocapitalizeControls whether and how text input is automatically capitalized.
classSpecifies one or more class names for the element, allowing CSS styling and JavaScript manipulation.
contenteditableSpecifies whether the content of an element is editable or not.
contextmenuSpecifies a context menu for the element.
dirDefines the text direction. Possible values are "ltr" (left-to-right), "rtl" (right-to-left), and "auto".
draggableSpecifies whether an element is draggable. Possible values are "true" or "false".
enterkeyhintSpecifies the action label or icon to be shown for the enter key on virtual keyboards.
hiddenIndicates that the element is not yet, or is no longer, relevant. The browser does not display elements that have the `hidden` attribute set.
idSpecifies a unique identifier for the element.
inputmodeProvides a hint to browsers for which virtual keyboard configuration to use when editing this element or its descendants.
isAllows you to specify the type of custom element.
langSpecifies the language of the element's content.
nonceA cryptographic nonce ("number used once") that can be used by Content Security Policy (CSP) to determine whether or not a given fetch will be allowed to proceed.
partSpecifies the element’s part in the shadow DOM.
slotAssigns a slot in a shadow DOM shadow tree.
spellcheckIndicates whether the element is subject to spell checking.
styleProvides inline CSS styling for the element.
tabindexSpecifies the tab order of the element.
titleAdds extra information about the element, displayed as a tooltip when hovering over the element.
translateSpecifies whether the content of the element should be translated. Possible values are "yes" or "no".
HTML AttributesGlobal AttributesEvent Attributes
To view the full list
To view the full list
To view the full list
elementsChrome BrowsersMicrosoft Edge BrowserFirefox BrowsersSafari BrowserOpera Browser
<dialog>37.079.098.015.424.0