Sub topic 11
Buttons, links, details and dialog
This chapter assumes you know basic HTML syntax and can use browser developer tools. It covers the native interactive elements and what each one gives you for free.
Links navigate, buttons act
Choose between <a> and <button> by asking what happens on activation.
| Question | Element | Built-in behavior |
|---|---|---|
| Does it go to a URL or a place in the page? | `<a href>` | Focusable, Enter activates it, can open in a new tab, announced as a link |
| Does it run an action on the current page? | `<button>` | Focusable, Enter and Space activate it, announced as a button |
An <a> without an href is not focusable and is not exposed as a link. A link whose href is # and whose only job is to run JavaScript should be a button.
Why a styled div is not a button
A div with a click handler has no role, no keyboard support and no focus. Fixing that by hand needs tabindex, a role, and key handlers for Enter and Space. A native button does all of this without extra code.
<!-- 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>Always set type. Inside a form, a button defaults to type="submit", so a button meant for something else will submit the form. Use type="button" for plain actions.
You can restyle a button freely with CSS. Appearance is not a reason to avoid it.
details and summary
<details> is a disclosure widget. The first child <summary> is the visible label. Clicking it, or pressing Enter or Space while it has focus, shows or hides the rest of the content. No JavaScript or ARIA is needed.
<details>
<summary>Shipping information</summary>
<p>Orders ship within two business days.</p>
</details>Add the boolean open attribute to start expanded. Browsers expose the expanded or collapsed state to assistive technology. Use details for FAQs and optional extra information. Do not use it for site navigation menus or tabs, which have different expected behavior.
dialog
<dialog> is a native modal or non-modal window. Calling showModal() does the following:
- Places the dialog in the top layer, above other content.
- Moves focus into the dialog.
- Makes the rest of the page inert.
- Closes the dialog when the user presses Escape.
<button type="button" id="open">Delete account</button>
<dialog id="confirm">
<form method="dialog">
<p>Delete this account?</p>
<button value="cancel">Cancel</button>
<button value="delete">Delete</button>
</form>
</dialog>
<script>
const dialog = document.getElementById("confirm");
document.getElementById("open").addEventListener("click", () => {
dialog.showModal();
});
</script>A form with method="dialog" closes the dialog when submitted. The value of the clicked button is stored in dialog.returnValue. When the dialog closes, browsers return focus to the element that opened it.
Common errors
- Using
<a href="#">or<a href="javascript:void(0)">for actions. Use<button type="button">. - Wrapping a button inside a link, or a link inside a button. Interactive content must not nest.
- Removing the focus outline without providing a replacement style.
- Forgetting
type="button"on a button inside a form. - Building a custom modal from
divelements and then having to reimplement focus trapping.
Exercise
A "Print this page" control calls window.print() and does not change the URL. Which element should it be, and why?
Show answer
A <button type="button">. It performs an action on the current page rather than navigating, and a native button already provides focus, Enter and Space activation, and the button role. A link would be announced as navigation and would need a meaningful href.