Sub topic 11
Buttons, Links, Details and Dialog
This chapter covers the native interactive elements: button, a, details with summary, and dialog. It assumes you know basic HTML syntax, can open browser developer tools, and have seen the landmark and form chapters. Each element ships with focus, keyboard behavior and an accessible role. A styled div has none of these unless you rebuild them in JavaScript and ARIA.
Button or link: the deciding question
Ask what happens when the user activates the control.
- If it navigates to a URL (another page, a section of the same page, a file), use
awith anhref. - If it performs an action on the current page (save, open a menu, toggle a panel, submit a form), use
button.
The two elements differ in role, keyboard activation and browser features.
| Feature | `<a href>` | `<button>` |
|---|---|---|
| Accessibility role | link | button |
| Activated by | Enter | Enter and Space |
| Middle-click, "open in new tab", copy link address | Yes | No |
| Appears in a screen reader's links list | Yes | No |
| Submits a form | No | Yes (default type) |
Screen reader users often browse a list of links or a list of buttons. A link that triggers an action, or a button that navigates, breaks the expectation that the role sets.
<!-- Navigation: a link -->
<a href="/pricing">See pricing</a>
<!-- Action: a button -->
<button type="button" id="save">Save draft</button>Why not a div?
The earlier chapters introduced the comparison below. Here it is in full.
<!-- Avoid: no role, no keyboard support, not focusable -->
<div class="btn" onclick="save()">Save</div>
<!-- Prefer: focusable, keyboard operable, announced as a button -->
<button type="button" onclick="save()">Save</button>To make the div behave, you would need role="button", tabindex="0", a keydown handler for Enter and Space, a disabled state and focus styling. The button gives you all of that for free.
The button element in detail
The type attribute
A button inside a form defaults to type="submit". This is a common source of bugs: a "Cancel" or "Add row" button submits the form and reloads the page.
| `type` | Behavior |
|---|---|
| `submit` | Submits the owning form (default) |
| `button` | Does nothing by itself. Use it with JavaScript |
| `reset` | Resets form fields to their initial values |
Set type="button" explicitly on every button that is not meant to submit.
Disabled buttons
A disabled button is removed from the tab order and ignores clicks. This is correct for a form that cannot be sent yet, but it has a cost. Keyboard and screen reader users cannot focus the button to learn why it is unavailable. Where possible, leave the button enabled and show an error message when the user activates it.
Buttons that look like links, and the reverse
CSS can restyle either element, and the semantics stay the same. A button styled as a text link is still a button. An a styled as a big colored box is still a link. Choose by behavior, then style it.
Links without href
An a with no href is a placeholder. It is not focusable and has no link role. Do not use <a href="#" onclick="..."> for actions. Use a button.
Links that work well
- Write link text that makes sense out of context. "Read the pricing guide" works in a links list. "Click here" does not.
- If a link opens a new tab with
target="_blank", tell the user in the text or an accessible label. Modern browsers implyrel="noopener"fortarget="_blank", but writing it is harmless. - Use
downloadon a link to a file you want saved rather than displayed. - Do not nest interactive content. A
buttoninside ana, or the reverse, is invalid, and browsers handle it inconsistently.
details and summary
The details element is a native disclosure widget. The first child summary is the always-visible label. The rest of the content is shown or hidden when the user toggles it.
<details>
<summary>Shipping information</summary>
<p>Orders ship within two business days.</p>
</details>What you get without any script:
- The summary is focusable and toggles with Enter and Space.
- The browser exposes the expanded or collapsed state to assistive technology.
- Content in a closed
detailsis hidden from the page and from most assistive technology, and it is skipped in the tab order.
Useful attributes and events
openmakes the widget start expanded. The browser adds and removes it as the user toggles, so you can read or set it from JavaScript.- The
toggleevent fires after the state changes. - Giving several
detailselements the samenamevalue makes them behave as an exclusive group, so opening one closes the others. Browser support for this is recent, and older browsers simply let all of them stay open, which degrades acceptably.
<details name="faq" open>
<summary>How do I reset my password?</summary>
<p>Use the "Forgot password" link on the sign-in page.</p>
</details>
<details name="faq">
<summary>How do I change my email?</summary>
<p>Open Account settings and choose Email.</p>
</details>Limits of details
summarymust be the first child ofdetails. Without it, the browser inserts a default label.- Avoid putting headings or interactive elements inside
summary. Its role already acts like a button, and children are treated as plain text by many screen readers. If the label needs to be a heading, put thedetailsinside a section that has its own heading. - It is not a menu, tab panel or accordion with managed arrow-key navigation. For a plain show/hide, it fits. For complex widgets, you need more design work.
dialog
The dialog element represents a dialog box or other interactive overlay. It has two modes, and the choice between them matters.
| Method | Mode | Behavior |
|---|---|---|
| `show()` | Non-modal | Opens in place. The rest of the page stays interactive |
| `showModal()` | Modal | Opens in the top layer, makes the rest of the page inert, traps focus, and closes on Esc |
| `close()` | Both | Closes the dialog |
Setting only the open attribute in markup shows a non-modal dialog. For a modal one, you must call showModal() from script.
<button type="button" id="open-dialog">Delete account</button>
<dialog id="confirm" aria-labelledby="confirm-title">
<form method="dialog">
<h2 id="confirm-title">Delete your account?</h2>
<p>This cannot be undone.</p>
<button value="cancel" autofocus>Cancel</button>
<button value="delete">Delete</button>
</form>
</dialog>
<script>
const dialog = document.getElementById("confirm");
document.getElementById("open-dialog").addEventListener("click", () => {
dialog.showModal();
});
dialog.addEventListener("close", () => {
if (dialog.returnValue === "delete") {
console.log("Account deletion confirmed");
}
});
</script>What the browser does for a modal dialog
- Moves the dialog to the top layer, above everything else, so you don't fight
z-index. - Makes the rest of the document inert. Content behind it cannot be focused or clicked.
- Moves focus into the dialog, to the element with
autofocusor to the first focusable element. - Closes on Esc and returns focus to the element that opened it.
- Provides a
::backdroppseudo-element you can style.
Forms inside dialogs
A form with method="dialog" closes the dialog on submit and does not send a request. The value of the clicked submit button becomes dialog.returnValue, and a close event fires. This lets you handle results without custom click handlers.
Dialog gotchas
- Give every dialog an accessible name with
aria-labelledbyoraria-label. - Put
autofocuson the safest control, such as Cancel in a destructive confirmation, rather than letting focus land on "Delete". - Do not use
dialogfor tooltips or dropdown menus. Those have other patterns. - Esc fires a
cancelevent first. CallpreventDefault()only when you have a strong reason, because users expect Esc to work. - Before
showModal()support, authors useddivwithrole="dialog"and wrote their own focus trap. New code should usedialog.
How the elements map to the accessibility tree
You can confirm each mapping in your browser's accessibility panel. Inspect the element, then open the Accessibility tab. For a details, toggle it and watch the expanded state change.
When ARIA is still needed
Native elements cover a lot, but they do not cover everything.
- Use
aria-expandedandaria-controlson abuttonthat shows or hides a region you built yourself, for example a site menu. You must updatearia-expandedfrom script. - Use
aria-pressedon a toggle button, such as "Mute". - Use
aria-describedbyto attach extra help text to a dialog or button.
Do not add role="button" to a button or role="link" to an a. It is redundant, and a wrong role overrides the native one.
<button type="button" aria-expanded="false" aria-controls="menu">Menu</button>
<ul id="menu" hidden>
<li><a href="/articles">Articles</a></li>
<li><a href="/about">About</a></li>
</ul>Common mistakes
| Mistake | Fix |
|---|---|
| `div` or `span` with a click handler | Use `button` |
| `<a href="#">` used to run script | Use `button type="button"` |
| Button inside a form submits unexpectedly | Add `type="button"` |
| Removing the focus outline with `outline: none` and nothing in its place | Provide a visible `:focus-visible` style |
| `div` with `role="dialog"` and a hand-built focus trap | Use `dialog` with `showModal()` |
| Dialog without a name | Add `aria-labelledby` pointing to its heading |
| Links named "click here" or "more" | Write descriptive link text |
Check your work
- Press Tab through the page. Every interactive element should receive visible focus in a sensible order.
- Activate each control with the keyboard. Buttons respond to Enter and Space, links to Enter, and
summaryto both. - Open a modal dialog. Try to Tab outside it, press Esc, and check that focus returns to the trigger.
- Inspect each control in the accessibility panel and confirm its role, name and state.
Exercise
A "Add another phone number" control inside a profile form inserts a new input row using JavaScript. A developer wrote <a href="#" onclick="addRow()">Add another phone number</a>. Name two problems and give the corrected markup.
Show answer
First, the control performs an action, not navigation, so it should be a button. A screen reader announces "link", and the link does not behave like a button (Space does not activate it). Second, href="#" changes the URL and may scroll the page to the top. Because the element is inside a form, a real button needs type="button" so it does not submit the form. Corrected: <button type="button" onclick="addRow()">Add another phone number</button>.
Exercise
You open a dialog by adding the open attribute in HTML. Users can still click and Tab to the page behind it, and Esc does nothing. Why, and what do you change?
Show answer
The open attribute shows the dialog as non-modal. Modal behavior (top layer, inert background, focus trap, Esc to close) only comes from calling showModal() in script. Remove open and call dialog.showModal() from the trigger button's click handler.