What is the HTML <dialog> tag?

Learn about the HTML <dialog> tag: create native, accessible, and customizable modal windows without complex JavaScript.
10 min read
Believemy logo

Introduction to the <dialog> tag

The <dialog> tag represents a revolution in creating modern web interfaces. It allows you to create dialog boxes, modal windows, and popups natively, without relying on complex JavaScript libraries. This semantic tag provides an elegant solution to a recurring web development problem: how to display content above a page while properly managing accessibility and user interactions.

Before <dialog> arrived, developers had to create their own modal systems using <div> elements, custom CSS, and extensive JavaScript to handle opening, closing, focus management, and accessibility. The <dialog> tag significantly simplifies this process by offering native functionality built into the browser. If you want to master this tag and many other modern HTML elements, our HTML course will guide you step by step through your learning journey.

Good to know

💡 The <dialog> tag is now supported by all modern browsers and represents the recommended method for creating accessible modal windows.

 

Definition and syntax of the <dialog> tag

Basic syntax

The <dialog> tag is a flow element that can contain any HTML content. It requires both an opening and closing tag. By default, the content of a <dialog> element is hidden until it is explicitly opened.

HTML
<dialog id="myDialogBox">
  <h2>Dialog box title</h2>
  <p>Your message content here.</p>
  <button>Close</button>
</dialog>

This basic structure creates a dialog box that will be invisible by default. To display it, you will need to use either the open attribute or the associated JavaScript methods.

 

Main attributes

The <dialog> tag accepts all global HTML attributes but has one particularly important specific attribute:

AttributeDescriptionValue
openIndicates that the dialog box is visible and interactiveBoolean (presence = true)

The open attribute is a boolean attribute. Its mere presence on the element indicates that the dialog box is open and visible to the user.

HTML
<!-- Dialog box closed by default -->
<dialog id="closedDialog">
  <p>I am hidden.</p>
</dialog>

<!-- Dialog box open on page load -->
<dialog id="openDialog" open>
  <p>I am visible immediately.</p>
</dialog>

 

Difference between dialog and modal

It is essential to understand the difference between a simple dialog box and a modal. The <dialog> tag can operate in both modes:

TypeBehaviorOpening method
Dialog (non-modal)User can interact with the rest of the pageshow() or open attribute
ModalBlocks interaction with the rest of the page, creates a backdropshowModal()
Warning

Warning: using the open attribute directly in HTML does not open the box in modal mode. For modal behavior, you must use the JavaScript showModal() method.

 

Associated JavaScript methods

The show() method

The show() method opens the dialog box in non-modal mode. The user can continue to interact with page elements behind the dialog box.

JAVASCRIPT
const dialog = document.getElementById('myDialogBox');

// Open in non-modal mode
dialog.show();

// The rest of the page remains interactive

 

The showModal() method

The showModal() method opens the dialog box in modal mode. In this mode, a semi-transparent backdrop appears behind the box, and the user cannot interact with the rest of the page until the modal is closed.

JAVASCRIPT
const believemyDialog = document.getElementById('courseRegistration');

// Open in modal mode
believemyDialog.showModal();

// A backdrop appears and blocks interactions

Modal mode offers several automatic advantages:

  • Focus is automatically trapped inside the dialog box
  • The Escape key automatically closes the modal
  • A ::backdrop pseudo-element is created for styling the background
  • Keyboard navigation is handled accessibly

 

The close() method

The close() method closes the dialog box, whether opened in modal mode or not. You can optionally pass a return value that will be stored in the element's returnValue property.

JAVASCRIPT
const dialog = document.getElementById('purchaseConfirmation');

// Close without return value
dialog.close();

// Close with a return value
dialog.close('confirmed');

// Retrieve the return value
console.log(dialog.returnValue); // 'confirmed'

 

Practical examples

Simple example: confirmation box

Here is a complete example of a confirmation dialog box, very useful for asking the user to validate an important action such as deleting an account or registering for a course.

HTML
<!-- Trigger button -->
<button id="btnOpenConfirmation">Delete my account</button>

<!-- Confirmation dialog box -->
<dialog id="deleteConfirmation">
  <h2>Confirm deletion</h2>
  <p>Are you sure you want to delete your Believemy account?</p>
  <p>This action is irreversible.</p>
  <div class="action-buttons">
    <button id="btnCancel" type="button">Cancel</button>
    <button id="btnConfirm" type="button">Confirm</button>
  </div>
</dialog>
JAVASCRIPT
const dialog = document.getElementById('deleteConfirmation');
const btnOpen = document.getElementById('btnOpenConfirmation');
const btnCancel = document.getElementById('btnCancel');
const btnConfirm = document.getElementById('btnConfirm');

// Open the modal
btnOpen.addEventListener('click', () => {
  dialog.showModal();
});

// Cancel and close
btnCancel.addEventListener('click', () => {
  dialog.close('cancelled');
});

// Confirm and close
btnConfirm.addEventListener('click', () => {
  dialog.close('confirmed');
  // Execute the deletion action
});

// Listen for close
dialog.addEventListener('close', () => {
  console.log('Result:', dialog.returnValue);
});

 

Advanced example: form in a modal

Dialog boxes are particularly suitable for displaying registration, login, or contact forms. Here is a complete example with form handling.

HTML
<button id="btnRegister">Register for the course</button>

<dialog id="registrationModal">
  <form method="dialog">
    <h2>HTML Course Registration</h2>
    
    <label for="firstName">First name</label>
    <input type="text" id="firstName" name="firstName" required>
    
    <label for="email">Email</label>
    <input type="email" id="email" name="email" required>
    
    <label for="level">Your current level</label>
    <select id="level" name="level">
      <option value="beginner">Beginner</option>
      <option value="intermediate">Intermediate</option>
      <option value="advanced">Advanced</option>
    </select>
    
    <div class="form-actions">
      <button type="button" id="btnCloseForm">Cancel</button>
      <button type="submit" value="registration">Confirm registration</button>
    </div>
  </form>
</dialog>

Note the use of method="dialog" on the form. This special attribute allows the form to automatically close the dialog box upon submission, while passing the submit button's value as the returnValue.

JAVASCRIPT
const registrationModal = document.getElementById('registrationModal');
const btnRegister = document.getElementById('btnRegister');
const btnCloseForm = document.getElementById('btnCloseForm');

btnRegister.addEventListener('click', () => {
  registrationModal.showModal();
});

btnCloseForm.addEventListener('click', () => {
  registrationModal.close('cancelled');
});

registrationModal.addEventListener('close', () => {
  if (registrationModal.returnValue === 'registration') {
    // Process the registration
    console.log('Registration validated!');
  }
});

 

Closing by clicking on the backdrop

By default, clicking on the backdrop (dark background) does not close the modal. However, you can implement this behavior very easily.

JAVASCRIPT
const modal = document.getElementById('myModal');

modal.addEventListener('click', (event) => {
  // If the click is on the dialog itself (not on its content)
  if (event.target === modal) {
    modal.close();
  }
});
Good to know

This technique works because the backdrop is technically part of the <dialog> element. A click directly on the dialog (and not on its children) indicates a click on the backdrop.

 

Styling the <dialog> tag with CSS

Basic styles

The <dialog> tag has default browser styles that you will generally want to customize. Here is how to create an elegant and modern modal.

CSS
/* Basic dialog box styles */
dialog {
  border: none;
  border-radius: 12px;
  padding: 2rem;
  max-width: 500px;
  width: 90%;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
}

/* Remove default outline if necessary */
dialog:focus {
  outline: none;
}

/* Appearance animation */
dialog[open] {
  animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

 

Styling the backdrop

The ::backdrop pseudo-element allows you to customize the background that appears behind the modal. This is a very powerful feature for creating immersive interfaces.

CSS
/* Backdrop style */
dialog::backdrop {
  background-color: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

/* Backdrop animation */
dialog::backdrop {
  animation: backdrop-fade 0.3s ease-out;
}

@keyframes backdrop-fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
Warning

The ::backdrop pseudo-element is only available when the modal is opened with showModal(). It does not appear with show() or the open attribute.

 

Complete style example

Here is a complete style example for a professional modal suitable for a platform like Believemy.

CSS
/* Main modal */
.believemy-modal {
  border: none;
  border-radius: 16px;
  padding: 0;
  max-width: 480px;
  width: 90%;
  overflow: hidden;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}

/* Modal header */
.believemy-modal header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 1.5rem 2rem;
}

.believemy-modal header h2 {
  margin: 0;
  font-size: 1.25rem;
}

/* Modal body */
.believemy-modal .content {
  padding: 2rem;
}

/* Modal footer */
.believemy-modal footer {
  padding: 1rem 2rem;
  background: #f8f9fa;
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
}

/* Custom backdrop */
.believemy-modal::backdrop {
  background: linear-gradient(
    135deg,
    rgba(102, 126, 234, 0.5) 0%,
    rgba(118, 75, 162, 0.5) 100%
  );
  backdrop-filter: blur(8px);
}

 

Accessibility of the <dialog> tag

Native accessibility advantages

One of the greatest advantages of the <dialog> tag is its native accessibility support. Unlike custom JavaScript solutions, it automatically handles many important aspects:

  • Implicit ARIA role: the tag automatically has the dialog role
  • Focus management: focus is trapped inside the modal in modal mode
  • Escape key closure: the Escape key automatically closes the modal
  • Screen reader announcements: content is correctly announced

 

Accessibility best practices

For optimal accessibility, you should still follow certain best practices.

HTML
<dialog id="accessibleModal" aria-labelledby="modalTitle" aria-describedby="modalDescription">
  <h2 id="modalTitle">Registration confirmation</h2>
  <p id="modalDescription">
    You are about to register for the HTML course.
    Do you confirm this action?
  </p>
  <button autofocus>Confirm</button>
  <button>Cancel</button>
</dialog>

The aria-labelledby and aria-describedby attributes allow assistive technologies to correctly announce the modal's title and description. The autofocus attribute on the first button ensures that focus is placed in the right location when opening.

 

Browser compatibility

The <dialog> tag is now widely supported by all modern browsers. Here is a summary table of compatibility.

BrowserMinimum versionSupport
Chrome37+✅ Full
Firefox98+✅ Full
Safari15.4+✅ Full
Edge79+✅ Full
Opera24+✅ Full
Safari iOS15.4+✅ Full
Good to know

For older browsers, a polyfill is available: dialog-polyfill. However, with the current near-universal support, it is rarely necessary.

 

Common mistakes to avoid

Forgetting the difference between show() and showModal()

A common mistake is using show() thinking you will get modal behavior. Remember that only showModal() creates true modal behavior with backdrop and focus trapping.

JAVASCRIPT
// ❌ Does not display backdrop, no focus trapping
dialog.show();

// ✅ Complete modal behavior
dialog.showModal();

 

Manipulating the open attribute directly

It is tempting to manipulate the open attribute directly via JavaScript, but this does not trigger the associated events and can cause problems.

JAVASCRIPT
// ❌ Does not trigger expected behaviors
dialog.setAttribute('open', '');
dialog.removeAttribute('open');

// ✅ Use the appropriate methods
dialog.showModal();
dialog.close();

 

Not handling closure correctly

Always make sure to provide a clear way to close the modal, even if the Escape key works automatically.

HTML
<dialog id="completeModal">
  <button class="close-btn" aria-label="Close">×</button>
  <h2>Modal content</h2>
  <p>Don't forget the visible close button!</p>
  <button class="action-btn">Validate</button>
</dialog>

 

Frequently asked questions about the <dialog> tag

Question

Can I have multiple modals open at the same time?

Technically yes, you can open multiple <dialog> elements simultaneously. Modals stack on top of each other in the order they were opened. However, for a good user experience, it is recommended to display only one modal at a time. If you need a confirmation within a modal, close the first one before opening the second, or use a content replacement approach.

 

Question

How do I prevent closure by the Escape key?

You can intercept the cancel event that is triggered before Escape closure and call preventDefault() to cancel it.

JAVASCRIPT
dialog.addEventListener('cancel', (event) => {
  event.preventDefault();
  // The modal stays open
});

Use this feature carefully, as blocking Escape closure can harm accessibility and frustrate users.

 

Question

Can the <dialog> tag replace modal libraries?

In most cases, yes. The <dialog> tag offers all essential features: opening, closing, backdrop, accessibility, and focus management. For very advanced cases requiring complex animations or specific behaviors, you might need additional code, but dedicated libraries rarely become necessary.

 

Question

How can I learn HTML and CSS in depth?

To master HTML, CSS, and all modern tags like <dialog>, we recommend following our complete HTML course. This training will guide you step by step through learning all HTML tags, modern CSS techniques, and best practices for creating professional and accessible websites.

Related terms

Discover our hTML and CSS glossary

Browse the terms and definitions most commonly used in HTML and CSS development.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.