Short answer first. To build a modal with the HTML dialog element, put your content inside <dialog>, add a form with method="dialog" for the buttons, and open it with dialog.showModal(). The browser puts it on top of everything, makes the rest of the page inert, moves focus inside, closes it on Escape and sends focus back to the button that opened it. You add the styling, a backdrop click and a scroll lock.
If you're a beginner, go from the top and open the live demo near the end once you've read the steps. You'll see every piece working together. If you've built modals with a div and a focus-trap library before, skip to the section about what the browser does the moment you call showModal(). That's the part that will make you delete code. And if you only care about the edge cases, the Plan B section near the end is where modals that look finished break for real people.
This is the sibling of the responsive hamburger menu article. There I told you a menu is not a modal and must not trap your focus. Today is the other side of that sentence: a modal is exactly the thing that does hold your focus, and the good news is that you don't have to build the holding yourself anymore.
info
Tested with: jsdom (a browser simulation that runs in Node) on 2026-11-20. jsdom doesn't implement showModal(), so the demo's own script was run against a small stand-in that follows the HTML spec steps (focus return, returnValue, the queued close event, Escape as a cancel). All 18 checks passed, and all 46 CSS declarations were checked against the CSS grammar. The browser behaviour itself (top layer, inert page, Escape, focus) comes from the HTML spec and MDN, linked in each step. Open the demo below with a mouse and with the keyboard and see it for yourself.
Let's agree on the word first, because people use "modal" for every box that pops up. A modal is a piece of the page that stops you from using the rest of the page until you deal with it. "Delete this list?" is a modal. "Your session expired, sign in again" is a modal. A little "Copied" message in the corner that fades away on its own is not a modal, because you can keep working while it's there.
Think of a clerk at a counter who slides a form across to you and says: sign this before we continue. The shop is still there behind you. You can see it. But nothing else happens until you sign, or say no and take the form back. That's a modal. The page is still visible, often dimmed, and it's waiting for one answer.
The opposite is a non-modal dialog. A small chat window in the corner of a support page is a dialog, but you can still scroll the article and click links next to it. Both use the same HTML element. The difference is one method call, and we'll get to it in step 2.
tip
Ask yourself one question before you build any popup: does the person have to answer this before doing anything else? If yes, it's a modal. If no, it's something else (a menu, a tooltip, a toast, a non-modal dialog), and making it modal will only annoy people.
Here's the thing. For years, a modal on most sites was a div with a high z-index and a dark overlay behind it. It looked right. But looking right was maybe a third of the job. The rest is invisible until you use a keyboard or a screen reader, and that's where most hand-made modals fell apart.
Write down everything a real modal has to do, and the list gets long fast:
- Show above everything else on the page, even above a sticky header with its own z-index.
- Stop clicks, taps and keyboard focus from reaching the page behind it.
- Move focus inside when it opens, so a keyboard user starts in the right place.
- Keep Tab and Shift+Tab from wandering out onto the hidden page.
- Close on Escape, because that's what everyone expects.
- When it closes, put focus back where the person was, so they don't lose their place.
- Tell screen readers that this is a dialog and that the rest of the page doesn't count right now.
With a div, every line of that list is your code. You'd fight z-index wars. You'd add aria-modal and role="dialog" by hand. You'd write a focus trap that finds every focusable element inside, catches Tab on the last one and jumps back to the first. You'd remember which button opened the modal so you could focus it again. You'd set inert or aria-hidden on everything else. Most people installed a library for this, and that was a reasonable choice, because the hand-made version usually missed two or three items.
The W3C's modal dialog pattern describes these same jobs for anyone who builds the box from scratch. The reason this article is short on JavaScript is simple: the dialog element with showModal() does almost the whole list for you. It has been available across browsers since March 2022, according to MDN's dialog page. So you're not reaching for something experimental here. It's a normal tool now.
When I started, I built pages from my head until they looked good. No plan, and no thought for the person using a keyboard. A modal is where that habit hurts the most, because the version that looks finished is the dangerous one. So we'll build the one that actually is finished.
In the hamburger menu article I said: no focus trap. If you press Tab past the last link in an open menu, focus just moves on to the page, and that's fine. A menu is a shortcut to links. It doesn't demand an answer.
A modal is different. If you open "Delete this list?" and Tab carries you out onto the page behind it, you're now pressing buttons on a page you can't properly see, with a question still hanging open on top. That's confusing for everybody and much worse for someone who can't see the screen. So the rule flips: a menu must let focus go, a modal must keep it.
Same idea, opposite answer, and the reason is the question we asked at the start: does the person have to answer before doing anything else? Keep that question. It decides almost every detail below.
We'll build a confirm box for deleting a reading list. It's small, it's common, and it has everything: a question, a safe answer, a dangerous answer, and a result we need to read afterwards.
<button type="button" id="open-confirm">Delete list</button>
<dialog id="confirm" aria-labelledby="confirm-title">
<form method="dialog">
<h2 id="confirm-title">Delete this list?</h2>
<p>"Weekend reading" and every link in it will be gone.</p>
<div class="actions">
<button value="cancel" autofocus>Cancel</button>
<button value="delete" class="danger">Delete</button>
</div>
</form>
</dialog>
<p id="status" role="status"></p>Let's go line by line.
The first button is the thing that opens the modal. It's a real button with type="button". That type matters out of habit: a button inside a form defaults to type="submit", and you don't want the opener to submit anything. This one isn't inside a form, but I write the type every time so I never have to think about it.
Then the dialog itself. On its own, a dialog without the open attribute is hidden, so this markup shows nothing until we call a method on it. The id="confirm" is how our script finds it. The aria-labelledby="confirm-title" points at the heading inside, so a screen reader announces the dialog by its question, "Delete this list?", instead of reading out an unnamed dialog.
Inside, there's a form with method="dialog". This is the clever bit, and it's the one people don't know about. A normal form sends data to a server. A form with method="dialog" sends nothing. When it's submitted, it just closes the dialog it lives in, and it remembers which button was pressed. No JavaScript needed for closing at all.
The two buttons have no type, so they're submit buttons, and each one has a value. When you press Cancel, the dialog closes and remembers "cancel". When you press Delete, it closes and remembers "delete". We'll read that answer in step 4.
Cancel has autofocus. When the modal opens, focus will land on Cancel, not on Delete. That's on purpose: if someone opens this by accident and hits Enter, they cancel instead of deleting their list. For a destructive question, the safe answer gets the focus. MDN's dialog page makes the same suggestion: put autofocus on the element that makes the most sense to start with.
Last, the paragraph with role="status" is where we'll write the result. role="status" makes screen readers announce new text there politely, without interrupting what they're reading.
warning
Don't put tabindex on the dialog element itself. MDN is direct about it: the tabindex attribute must not be used on dialog. The dialog isn't something you interact with; the buttons and fields inside it are.
const openButton = document.getElementById('open-confirm');
const dialog = document.getElementById('confirm');
openButton.addEventListener('click', () => {
dialog.returnValue = '';
dialog.showModal();
});Two lookups and one click handler. The first line finds the opener button. The second finds the dialog. The click handler does two things.
dialog.returnValue = '' clears the answer from last time. That line looks pointless, and it's the one that saves you from a real bug. I'll explain it properly in step 4, because that's where it bites.
dialog.showModal() opens the dialog as a modal. There's also a method called show(), and the difference between the two is the whole difference between modal and non-modal:
- showModal() opens a modal: the rest of the page becomes inert, the dialog goes on top of everything, Escape closes it, and the ::backdrop behind it can be styled.
- show() opens a non-modal dialog: the page behind stays fully usable and Escape doesn't close it by default.
- Setting the open attribute by hand (dialog.open = true, or open in the HTML) gives you the non-modal version too, and MDN recommends the methods instead of toggling the attribute.
That last point catches a lot of people. They see that a dialog with open in the HTML is visible, so they toggle the attribute with JavaScript, and they get a box that looks like a modal but behaves like a sticky note. No inert page, no Escape, no backdrop. If you want a modal, call showModal(). Every time.
This is the part that would have been a hundred lines of your code a few years ago. Nothing to type in this step. Just read what you get for free, because you need to know it's there so you don't build it a second time.
It goes on top of everything
A modal dialog is placed in what the spec calls the top layer. The top layer sits above the normal page, so your sticky header with z-index 1000 can't cover it. No z-index war. You can read the steps in the HTML spec's dialog section, which says showModal() "makes it the top-most modal dialog".
The rest of the page becomes inert
Inert means: you can see it, but you can't click it, focus it or select it, and screen readers skip it. MDN puts it plainly: everything outside the modal dialog is inert and interactions outside the dialog are blocked. That's the line in our list about stopping clicks and focus. And because the page behind is inert, Tab can't wander onto it. That's your focus trap, done.
Focus moves inside
When it opens, focus goes to the element with autofocus if there is one. If not, the browser looks for the first focusable thing inside, and if it finds nothing, it focuses the dialog itself. In our case that's the Cancel button, because we marked it.
Escape closes it
Pressing Escape on a modal dialog fires a cancel event and then closes it. If two modals are open on top of each other, Escape closes only the top one. MDN notes the browser handles that too.
Focus comes back to where you were
When a modal closes, the browser puts focus back on the element that had it before the modal opened. In our case: the Delete list button. In the spec, this is the step that runs the focusing steps for the "previously focused element" when the dialog was modal. For a keyboard user, this is huge. Without it, closing a dialog throws them back to the top of the page and they have to Tab all the way down again.
info
So that list of seven jobs from earlier? The browser now does six of them: the top layer, the inert page, focus in, Tab kept inside, Escape, and focus back. The dialog element also carries the dialog role for screen readers. What's left for you is the look, a backdrop click if you want one, the scroll lock, and reading the answer.
const status = document.getElementById('status');
dialog.addEventListener('close', () => {
if (dialog.returnValue === 'delete') {
status.textContent = 'List deleted.';
} else {
status.textContent = 'Nothing deleted.';
}
});The close event fires every time the dialog closes, no matter how. Cancel button, Delete button, Escape, a backdrop click we'll add in step 6: all of them end up here. That's why we listen to close instead of putting a click handler on each button. One place, every path.
Inside, dialog.returnValue holds the value of the button that submitted the form. Press Delete, and it's "delete". Press Cancel, and it's "cancel". Then we write the result into the status paragraph. In a real app this is where you'd send the delete request to your server.
Now the bug I promised. What's returnValue after someone presses Escape? You'd guess an empty string, or "cancel". Neither. Escape closes the dialog without a result, and the spec says the returnValue is only set when there is a result. So it keeps whatever it was before.
Picture this. Emily opens the dialog and presses Delete. returnValue becomes "delete". Later she opens the same dialog for another list, changes her mind and presses Escape. If we didn't clear the value, returnValue would still say "delete" from last time, and our close handler would happily delete the second list. She pressed Escape and lost her list.
That's why step 2 sets returnValue to an empty string right before showModal(). Every opening starts with a clean answer, and Escape lands in the else branch: "Nothing deleted." In the test for this article, a dialog closed with no result kept the old "delete", exactly as the spec says. With the reset in place, Escape after a delete deleted nothing.
tip
The close event doesn't fire inside close() itself; the browser queues it for a moment later, and it puts focus back on the opener before that. So by the time your close handler runs, focus is already on the Delete list button. Keep that in mind: it matters in the Plan B section.
dialog {
padding: 0;
border: none;
border-radius: 0.75rem;
width: min(26rem, calc(100% - 2rem));
max-height: calc(100% - 2rem);
overflow: auto;
box-shadow: 0 20px 50px rgb(0 0 0 / 0.3);
}
dialog form { padding: 1.5rem; }
dialog::backdrop { background: rgb(15 23 42 / 0.6); }
.actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 1.5rem; }
.danger { background: #b91c1c; border-color: #b91c1c; color: #fff; }Before we change anything, know what the browser gives you. The HTML spec's rendering section has default styles for the dialog: it's centred with margin: auto, it gets a solid border and padding of 1em, and a modal one is fixed to the screen with a maximum width and height a little smaller than the screen. So it's already centred. You don't need flexbox or a transform trick for that.
Now the rules. padding: 0 removes the default padding from the dialog and we put the padding on the form inside instead. That looks like a style choice, but it's really for step 6: with no padding, the only part of the dialog that isn't covered by the form is nothing at all, which makes the backdrop click work cleanly. Hold that thought.
border: none drops the default border, because a soft shadow looks better here. border-radius rounds the corners. width: min(26rem, calc(100% - 2rem)) means: be 26rem wide, but on a narrow phone, be the screen width minus 1rem on each side. max-height and overflow: auto let a long dialog scroll inside itself instead of running off the screen. The box-shadow lifts it off the page.
dialog::backdrop is the layer between the dialog and the page. It only exists for modals opened with showModal(), and it covers the whole screen. The default is a very light tint, almost invisible. We make it a dark blue-grey at 60% so the page fades back and the question comes forward. If you've seen modals with a blurred page behind them, this is where that comes from, with backdrop-filter.
The last two rules are just the buttons: the row of actions pushed to the right with a small gap, and a red Delete button so it looks like the dangerous choice it is. Cancel stays plain. Colour isn't the only signal here, because the words say "Delete" too, which is what matters for anyone who doesn't see red.
People expect a click on the dark area outside a modal to close it. The browser doesn't do this for a showModal() dialog by default. MDN describes the default as behaving like closedby="closerequest": Escape and your own code close it, a click outside doesn't. So we add it.
dialog.addEventListener('click', (event) => {
if (event.target === dialog) {
dialog.close('cancel');
}
});Here's the trick, and it's a nice "aha". When you click the backdrop, the click lands on the dialog element itself, because the backdrop belongs to the dialog. When you click something inside the dialog, like the heading or a paragraph, the event's target is that heading or paragraph, not the dialog.
So event.target === dialog means: the click didn't land on any of the content. And because we set padding: 0 in step 5 and gave the padding to the form, the form covers the whole inside of the box. A click on the empty space near the edge of the box still hits the form, not the dialog. The only way to get the dialog as a target is to click outside the box. That's why the padding moved.
dialog.close('cancel') closes the dialog with the result "cancel". Unlike Escape, close() with an argument does set returnValue, so a backdrop click is an honest "no", and the close handler from step 4 says "Nothing deleted."
Should every modal close on a backdrop click? No. For a quick "Are you sure?" it's friendly. For a long form where someone has typed three paragraphs, one stray click outside throws all of it away. In that case, leave this listener out. There's also a newer attribute, closedby="any", that asks the browser to do this light dismiss for you, but MDN's closedBy page marks it "Limited availability" and not Baseline yet. So the four lines of JavaScript stay for now.
The page behind a modal is inert, so you can't click it. But nothing in the dialog part of the spec says the page stops scrolling. Don't count on it. If the page scrolls under an open modal, especially on a phone, the person loses their place, and closing the modal drops them somewhere they don't recognise.
html:has(dialog:modal) { overflow: hidden; }One line. Read it as a sentence: if the html element has a dialog inside it that is currently open as a modal, hide the overflow of the page. :has() is the "parent that contains" selector, available across browsers since December 2023 according to MDN's :has page. :modal matches a dialog opened with showModal(), and it has been available since September 2022.
The nice part: there's no JavaScript to remember. You don't add a class when you open and remove it when you close, and you can't forget the removal on one of the four ways the dialog can close. The moment the dialog stops being a modal, the selector stops matching and the page scrolls again.
A small cost to know about: on desktops where the scrollbar takes up space, hiding it can make the page shift sideways by the scrollbar's width while the modal is open. If that bothers you, scrollbar-gutter: stable on the html element reserves that space all the time. I'd only add it if you actually see the jump on your site.
dialog[open] { animation: pop-in 150ms ease-out; }
@keyframes pop-in {
from { opacity: 0; transform: translateY(0.5rem) scale(0.98); }
to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: reduce) {
dialog[open] { animation: none; }
}dialog[open] matches the dialog while it's open, so the animation runs each time it opens. pop-in fades it in from fully transparent and moves it up from half a rem lower, slightly smaller, to its normal place. 150 milliseconds is quick enough not to slow anyone down. ease-out means it starts fast and settles gently.
The media query is the part I always add. Some people set their system to reduce motion, because movement on screen makes them dizzy or sick. prefers-reduced-motion lets you hear that setting. For them, the dialog just appears. Nothing is lost: the animation was decoration, not information.
This only animates the opening. Animating the closing is harder, because the browser removes the dialog the moment it closes, before any animation could run. There are newer CSS features for exit animations, but they're a topic of their own, and a dialog that disappears instantly is perfectly fine. Don't let the closing animation be the reason you ship a worse modal.
Here's the whole thing as one working demo. There are two lists so you can see focus return to the right Delete button. Try it three ways: with a mouse, then with only the keyboard (Tab to a Delete button, Enter, then Escape or Tab and Enter), then delete a list for real and watch where focus goes. Scroll the page first and try to scroll again while the dialog is open.
<main class="page">
<h1 id="lists-title" tabindex="-1">Your reading lists</h1>
<ul class="lists">
<li data-name="Weekend reading">
<span>Weekend reading</span>
<button type="button" class="delete">Delete</button>
</li>
<li data-name="CSS tricks to try">
<span>CSS tricks to try</span>
<button type="button" class="delete">Delete</button>
</li>
</ul>
<p id="status" role="status">Nothing deleted yet.</p>
<p class="filler">This text is here so the page is taller than the frame. Scroll it before you open the dialog, then try to scroll again while the dialog is open.</p>
<p class="filler">Emily keeps her lists short. James keeps everything. Olivia deletes a list the moment she finishes it.</p>
<p class="filler">End of the page.</p>
</main>
<dialog id="confirm" aria-labelledby="confirm-title">
<form method="dialog">
<h2 id="confirm-title">Delete this list?</h2>
<p>"<span id="confirm-name"></span>" and every link in it will be gone.</p>
<div class="actions">
<button value="cancel" autofocus>Cancel</button>
<button value="delete" class="danger">Delete</button>
</div>
</form>
</dialog>.page { max-width: 32rem; }
.lists { list-style: none; padding: 0; margin: 0; }
.lists li {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
padding: 0.75rem 0;
border-bottom: 1px solid #ddd;
}
.filler { margin-top: 6rem; color: #555; }
button {
font: inherit;
min-height: 2.75rem;
padding: 0.5rem 1rem;
border: 1px solid #888;
border-radius: 0.5rem;
background: #fff;
cursor: pointer;
}
button:focus-visible { outline: 3px solid #2563eb; outline-offset: 2px; }
dialog {
padding: 0;
border: none;
border-radius: 0.75rem;
width: min(26rem, calc(100% - 2rem));
max-height: calc(100% - 2rem);
overflow: auto;
box-shadow: 0 20px 50px rgb(0 0 0 / 0.3);
}
dialog form { padding: 1.5rem; }
dialog h2 { margin: 0 0 0.5rem; font-size: 1.25rem; }
dialog::backdrop { background: rgb(15 23 42 / 0.6); }
.actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 1.5rem; }
.danger { background: #b91c1c; border-color: #b91c1c; color: #fff; }
html:has(dialog:modal) { overflow: hidden; }
dialog[open] { animation: pop-in 150ms ease-out; }
@keyframes pop-in {
from { opacity: 0; transform: translateY(0.5rem) scale(0.98); }
to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: reduce) {
dialog[open] { animation: none; }
}const dialog = document.getElementById('confirm');
const nameSlot = document.getElementById('confirm-name');
const status = document.getElementById('status');
const title = document.getElementById('lists-title');
let pending = null;
document.querySelectorAll('.delete').forEach((button) => {
button.addEventListener('click', () => {
pending = button.closest('li');
nameSlot.textContent = pending.dataset.name;
dialog.returnValue = '';
dialog.showModal();
});
});
dialog.addEventListener('close', () => {
if (dialog.returnValue === 'delete' && pending) {
status.textContent = `Deleted "${pending.dataset.name}".`;
pending.remove();
title.focus();
} else {
status.textContent = 'Nothing deleted.';
}
pending = null;
});
dialog.addEventListener('click', (event) => {
if (event.target === dialog) dialog.close('cancel');
});The demo has two things the steps didn't. Each list item carries its name in data-name, and a variable called pending remembers which item the open dialog is about, so one dialog serves every Delete button. And when a list is really deleted, focus moves to the heading. That last one has a story, and it's the first item in the Plan B section.
A modal with the HTML dialog element is a dialog tag with a form inside, a button that calls showModal(), and a close listener that reads returnValue. The browser takes care of the hard part: on top of everything, page inert, focus in, Escape out, focus back. You add how it looks, a backdrop click, one line of CSS for the scroll lock, and a reset of returnValue so a stale answer can't sneak through.
That's it. The thing that used to need a library is now about twenty lines of JavaScript, most of which is your own app logic, not modal plumbing.
Everything above works. These are the places where it stops working the moment real people and real code get involved.
The button that opened the modal is gone
In the demo, pressing Delete removes the list item, and the Delete button that opened the dialog goes with it. Remember the tip in step 4: by the time the close handler runs, the browser has already moved focus back to that button. Then we remove the button. The focused element is gone, so focus falls back to the body of the page. The person's place is lost, and a keyboard user has to find their way back, wondering what happened.
if (dialog.returnValue === 'delete' && pending) {
status.textContent = `Deleted "${pending.dataset.name}".`;
pending.remove();
title.focus();
}The fix is the last line. After removing the item, we move focus to the heading of the list ourselves. The heading has tabindex="-1" in the demo's HTML, which lets a script focus it without adding it to the Tab order. Any sensible place works: the next item in the list, the heading, a "List deleted" message. The rule is: if you remove the opener, you choose where focus goes. The browser can't guess.
Escape on a form someone has half filled in
Escape closes a modal, and that's usually right. But if your modal is a "Write a note" box and someone has typed half a page, one accidental Escape loses it all. The cancel event fires before the close, so you can stop it:
const note = document.getElementById('note');
dialog.addEventListener('cancel', (event) => {
if (note.value.trim() !== '') {
event.preventDefault();
note.focus();
}
});If the note field has text, event.preventDefault() keeps the dialog open and we put focus back in the field. If it's empty, Escape closes as normal.
One honest limit. The spec doesn't let a page hold a modal open forever. Its close request rules say the cancel event is only cancelable if the page has had a recent user interaction, and if the person sends a close request twice in a row without one in between, "the request definitely succeeds". That's on purpose, so no site can trap you. So treat this as a polite "are you sure?", not a lock, and save drafts somewhere if they really matter.
The dialog that's taller than the phone
A long terms text or a big form in a modal can be taller than a small phone screen. The browser already limits a modal's height, and our max-height with overflow: auto lets it scroll inside. Test it on a real phone in landscape, where the screen is very short. If the modal needs more than one screen of content on a phone, ask yourself if it should be a page instead of a modal. Often it should.
A modal for something that isn't a question
Newsletter popups that appear after five seconds, "We use cookies" boxes over the whole page, a modal to show a single image. Every one of these blocks the person from what they came for. A modal is for a decision the person has to make now, usually because they started it themselves by clicking something. If nobody asked for it, it probably shouldn't be modal. That's not just style. It's how people decide whether they trust your site.
React and other frameworks
In React, the same rules apply. Keep the dialog in the page, hold a ref to it, and call showModal() and close() from your code when your state changes, instead of rendering the open attribute from a prop. If you render open, you get the non-modal version from step 2 and lose the inert page and Escape. And listen to the close event to update your state, because Escape and a backdrop click close the dialog without asking React first. If lists of items in components are still new to you, the render-lists article shows the one-component-many-items pattern the demo's Delete buttons follow.
The confirm box is not the security check
The dialog asks a person if they're sure. It doesn't decide whether they're allowed to delete the list. Anyone can skip your dialog and send the delete request to your server directly. The server must check that this person owns this list, every time. Same idea as in the login form validation article: the browser helps the honest user, the server protects you from everyone else.
warning
If you remove the default outline from buttons inside the dialog, keyboard users can't see which answer is focused, and in a delete confirm that's a real risk. Keep a clear :focus-visible style, like the 3px blue outline in the demo.
There's a newer way to open the dialog straight from the HTML. A button can point at the dialog by id and say what to do with it:
<button type="button" commandfor="confirm" command="show-modal">
Delete list
</button>commandfor names the dialog's id, and command="show-modal" does the same as calling showModal(). There's a matching command="close". This is the Invoker Commands API, and MDN lists it as Baseline 2025, newly available since December 2025, which means it works in current browsers but may not in older ones that people still use. If you use it, keep a script fallback, or wait until it's widely available. You'd still need a close listener to read the answer, so most real modals keep a few lines of JavaScript anyway.
A related method, requestClose(), closes a dialog the same way Escape does, cancel event first, so your "unsaved note" check from the Plan B section also runs for your own close buttons. The spec describes it alongside close(). Check its support on MDN before relying on it; for most confirm boxes, the form with method="dialog" is all you need.
If what you need is a box that doesn't block the page, like a dropdown or a small info panel, look at the popover attribute instead. I covered when popover fits a menu in the hamburger menu article. Rule of thumb: an answer you need now is a dialog with showModal(); something someone can ignore is a popover or a non-modal dialog.
If the HTML in step 1 felt new, start with HTML and CSS for beginners, which explains elements, attributes and selectors from zero. Then the responsive hamburger menu is the perfect partner to this one: same button, same aria thinking, the opposite answer on focus. And if your modal sits under a header that stays at the top, the sticky header and hero article shows why the top layer is such a relief: no z-index fight.
Then build one. Take a delete button you already have, put a dialog behind it, and test it three ways: with a mouse, with only the keyboard, and with a screen reader if you have one on your computer (most systems have one built in). If all three feel right, you've built a better modal than a lot of the ones you'll click today.
A good programmer thinks a few steps ahead. Here, a few steps ahead means asking where focus goes after the button is gone, what Escape does to a stale answer, and what the server checks when someone skips the dialog. Ask those three every time and your modals will be fine.
You can do this and you have to. If you weren't capable I wouldn't spend my time on you. Just don't give up and you'll always have me on your side.
