A useful interface does not need to be complicated. Start with native HTML, then add JavaScript only when you need it.
What You Need to Know
Dialog can represent a nonmodal window or a modal that temporarily blocks the surrounding page. JavaScript methods such as show() and showModal() control how it opens.
In this example: make a settings dialog.
Try the Example
Take a look at the example. We’ll explain the important lines right below it.
<button type="button" id="open-help">Show modal help</button>
<dialog id="help-dialog">
<h2>Help</h2>
<p>Read the instructions before continuing.</p>
<form method="dialog"><button>Close</button></form>
</dialog>
<script>
const helpDialog = document.querySelector("#help-dialog");
document.querySelector("#open-help").addEventListener("click", () => helpDialog.showModal());
</script>
Let’s Read the Code
- <button>: The button offers an action; its type or command determines what happens.
- <dialog>: Dialog provides a browser-managed dialog container; modal display may need showModal or a supported command.
- type: Type changes what sort of control or resource the browser uses.
- id: An id uniquely identifies an element on this page.
What the example shows: ShowModal opens a modal dialog; show opens a nonmodal dialog that does not block interaction with the page.
What Happens in the Browser?
When this example includes JavaScript, it supplies behavior that ordinary HTML does not automatically provide. Always think about a fallback.
What makes it interactive? Look for the JavaScript that finds an element and reacts to an event. HTML provides the initial controls and content; the script updates the page. Try removing the script to see which part still works natively.
Your Turn
Change showModal() to show() and compare whether you can interact with the background.
Common Beginner Mistakes
- Assuming new HTML syntax works in every browser without testing.
- Use a clear close control and test where focus returns.
Key Takeaways
- Dialog can represent a nonmodal window or a modal that temporarily blocks the surrounding page.
- <button>: The button offers an action; its type or command determines what happens.
- Test the behavior you expect rather than trusting how the snippet looks in your editor.