Skip to content

Instantly share code, notes, and snippets.

@lovelymono
Created April 12, 2022 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lovelymono/d6245c0d0959dfe50ca53f25546070df to your computer and use it in GitHub Desktop.
Save lovelymono/d6245c0d0959dfe50ca53f25546070df to your computer and use it in GitHub Desktop.
Native dialog in HTML
<!-- You can use JSBin: https://jsbin.com/xoketogeda/edit?html,output to play around with it. -->
<!-- The dialog element is specified in HTML specification, §4.11.4 "The dialog
element": https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element. -->
<button onclick="document.querySelector('dialog').showModal()">Open dialog</button>
<dialog style="width: 50%;">
<h1>Native <code>&lt;dialog&gt;</code></h1>
<p>
Yes, it's native! No third-party libraries needed, no fancy things with <code>&lt;div&gt;</code>s
and CSS! We only need some JavaScript to open it, closing can be done by the browser already:
</p>
<pre><code>document.querySelector('dialog').showModal()</code></pre>
<form method="dialog">
<button>Close</button>
</form>
</dialog>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment