Back to subject
HTML5/HTML Dialog

Build a Small Dialog

Practical lesson

Let's create a native HTML dialog and see how the pieces fit together.

- <dialog> represents a dialog box or other interactive overlay. - Put the dialog content inside the opening and closing <dialog> tags. - Give users a clear way to close or cancel the dialog. - A dialog can contain normal HTML such as headings, paragraphs, forms, and buttons. - The open attribute can make the dialog visible when the page loads, although JavaScript is normally used to open and close dialogs dynamically. - A form can use method="dialog" when its controls should close the dialog as part of the native dialog behavior. - Do not use a dialog just to hide ordinary content; use it when the content needs a dialog-like interaction.

Read the example as a small self-contained UI component: the dialog holds the message and the button provides an action.

Try it yourself
Code

Starter example

Code
<dialog open>
  <p>Welcome to the site!</p>
  <form method="dialog">
    <button>Close</button>
  </form>
</dialog>
Live preview