Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS MATH

HTML Tutorial

HTML HOME HTML Introduction HTML Editors HTML Basic HTML Elements HTML Attributes HTML Headings HTML Paragraphs HTML Styles HTML Project I HTML Formatting HTML Quotations HTML Comments HTML Colors HTML CSS HTML Project II HTML Links HTML Images HTML Favicon HTML Page Title HTML Tables HTML Lists HTML Project III HTML Block & Inline HTML Div HTML Classes HTML Id HTML Project IV HTML Buttons HTML Iframes HTML JavaScript HTML File Paths HTML Head HTML Layout HTML Responsive HTML Project V HTML Computercode HTML Semantics HTML Project VI HTML Dialog HTML Style Guide HTML Entities HTML Symbols HTML Emojis HTML Charsets HTML URL Encode HTML vs. XHTML

HTML Forms

HTML Forms HTML Form Attributes HTML Form Elements HTML Input Types HTML Input Attributes Input Form Attributes HTML Project VII

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Video HTML Audio HTML YouTube HTML Project VIII

Web Accessibility

Web Accessibility ARIA Attributes Keyboard Focus and tabindex Landmarks Skip Links Color Contrast

Web APIs

Web APIs Geolocation API Drag and Drop API Popover API Web Storage API Web Workers API Server-Sent Events API

HTML Cert

HTML Certificate

HTML Examples

HTML Examples HTML Videos HTML Editor HTML Quiz HTML Exercises HTML Challenges HTML Projects HTML Website HTML Syllabus HTML Study Plan HTML Interview Prep HTML Summary

HTML References

HTML Tag List HTML Attributes HTML Global Attributes HTML Browser Support HTML Event Attributes HTML Colors HTML Canvas HTML Audio/Video HTML Doctypes HTML Character Sets HTML URL Encode HTML Language Codes HTML Country Codes HTTP Status Codes HTTP Request Methods PX to EM Converter Keyboard Shortcuts

HTML Dialog


Share

HTML <dialog> Element

The HTML <dialog> element creates a box that pops up on top of the page, like a message, a question, or a small form.

The browser does most of the work for you. It keeps the dialog on top, can dim the page behind it, closes it when the user presses Esc, and moves keyboard focus into it.

Dialogs are often used for:

  • confirm boxes, like "Are you sure you want to delete this?"
  • login or sign-up forms
  • important messages the user must read

Here, we show a simple dialog example:

Example

A dialog box. Here, one button opens the dialog and another button inside it closes it:

<button id="openBtn">Open dialog</button>

<dialog id="myDialog">
  <p>Hello! I am a dialog.</p>
  <button id="closeBtn">Close</button>
</dialog>

<script>
const dialog = document.getElementById("myDialog");
document.getElementById("openBtn").addEventListener("click", () => {
  dialog.showModal();
});
document.getElementById("closeBtn").addEventListener("click", () => {
  dialog.close();
});
</script>
Try it Yourself »

How it Works

  • The <dialog> element holds the content. It stays hidden at first
  • showModal() opens the dialog on top of the page
  • close() closes it again


Modal and Non-Modal Dialogs

There are two ways to open a dialog, and they behave differently:

Example

dialog.showModal();  // modal
dialog.show();  // non-modal
  • A modal dialog takes over the page. The user must deal with it before doing anything else.
  • A non-modal dialog stays open while the user keeps working on the page.
  showModal() show()
Type Modal Non-modal
Rest of the page Blocked until the dialog closes Still usable
Backdrop Yes, can be styled with ::backdrop No
Closes with Esc Yes No
Good for Confirm boxes, forms, important messages Small helper boxes, chat windows

Closing a Dialog

There are several ways to close a dialog.

With JavaScript

Use the JavaScript close() method:

Example

dialog.close();

With a Form

A form with method="dialog" closes the dialog when it is submitted. No JavaScript is needed:

Example

<dialog open>
  <form method="dialog">
    <p>Do you want to save your changes?</p>
    <button value="no">No</button>
    <button value="yes">Yes</button>
  </form>
</dialog>

With the ESC key

A modal dialog closes when the user presses Esc. This works by itself.

By Clicking Outside It

The closedby="any" attribute lets users close the dialog by clicking anywhere on the backdrop:

Example

<dialog id="myDialog" closedby="any">
  <p>Hello! I am a dialog.</p>
  <button id="closeBtn">Close</button>
</dialog>
Try it Yourself »

Styling a Dialog

You can style a <dialog> element with CSS, just like any other element:

Example

dialog {
  border: none;
  border-radius: 8px;
  padding: 20px;
  max-width: 400px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}
Try it Yourself »

Style the Dialog backdrop

When a dialog is opened with showModal(), a backdrop covers the page behind it. Style it with the CSS ::backdrop pseudo-element:

Example

dialog::backdrop {
  background: rgba(0, 0, 0, 0.5); /* dims the page */
}
Try it Yourself »

Here we style the backdrop with a gradient:

Example

dialog::backdrop {
  background-color: #1fc8db; /* for browsers that do not support gradients */
  background-image: linear-gradient(140deg, #EADEDB 0%, #BC70A4 50%, #BFD641 75%);
  opacity:0.75;
}
Try it Yourself »

Open a Dialog Without JavaScript

Newer browsers let you open and close a dialog with HTML only, using the commandfor and command attributes on the <button> element:

Example

<button commandfor="myDialog" command="show-modal">Open dialog</button>

<dialog id="myDialog">
<p>Hello! No JavaScript needed.</p>
<button commandfor="myDialog" command="close">Close</button>
</dialog>
Try it Yourself »
  • commandfor is the id of the dialog the button controls
  • command says what to do: show-modal opens it, and close closes it

Note: These attributes are new. Check browser support, and use the JavaScript methods if you need to support older browsers.


Use <dialog> or Popover?

Both show content on top of the page, but they are made for different jobs.

The popover is explained in the Popover API chapter.

  Modal <dialog> Popover
Blocks the page Yes No
Closes when you click outside Only with closedby="any" Yes, by default
Needs JavaScript Optional No
Good for Questions the user must answer, forms, warnings Menus, tooltips, hints, notifications

A simple rule: If the user must respond before going on, use a modal <dialog>. If the content is just extra help, use a popover.


Summary

  • <dialog> makes a box that appears on top of the page
  • Open it with showModal() or show()
  • Close it with close(), a form with method="dialog", or the Esc key
  • Style the dimmed area behind it with ::backdrop
  • Use a modal dialog when the user must respond, and a popover for lighter content

HTML Tag Reference

Tag Description
<dialog> Creates a dialog box that pops up on top of the page

For a complete list of all available HTML tags, visit our HTML Tag Reference.


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.