Sub topic 3
Buttons, Links, Details and Dialog
Assume basic HTML syntax, browser developer tools, and the difference between HTML structure and CSS appearance. This chapter focuses on choosing native elements before adding ARIA.
Buttons, links, details and dialog
Use a <button> to perform an action in the current page. Use an <a> element to move to a URL. Use <details> for expandable content that does not need custom behavior. Use <dialog> for a modal or non-modal conversation with the user.
<button type="button" id="open-help">Open help</button>
<a href="/settings">Account settings</a>
<details>
<summary>Shipping information</summary>
<p>Orders usually ship within two business days.</p>
</details>
<dialog id="help">
<h2>Help</h2>
<p>Choose an option from the settings page.</p>
<button type="button" id="close-help">Close</button>
</dialog>
<script>
const help = document.querySelector("#help");
document.querySelector("#open-help").addEventListener("click", () => {
help.showModal();
});
document.querySelector("#close-help").addEventListener("click", () => {
help.close();
});
</script>A styled <div> does not become a button because it looks like one. It lacks the button role, keyboard behavior, and focus behavior provided by native HTML.
How browsers expose semantics to assistive technology
Browsers parse HTML into an accessibility tree. This tree contains roles, names, states, and relationships. A button is exposed as a button. A labelled link is exposed as a link. The browser can expose whether a <details> element is open.
ARIA: when native HTML is not enough
ARIA adds or changes accessibility information. Use it when no suitable native element exists, such as for a custom widget. Do not use ARIA to repair incorrect element choices. A native button is safer than a <div role="button">.
A process for choosing the right element
- Identify the content's purpose.
- Choose the native element with that purpose.
- Test its keyboard and focus behavior.
- Add ARIA only for a genuine semantic gap.
- Style the element with CSS instead of replacing it.
Common mistakes and how to fix them
- Use
<button>instead of a clickable<div>. - Use
<a href="...">instead of a button for navigation. - Give every form control an associated
<label>. - Do not skip heading levels merely to obtain a visual size.
- Do not add conflicting roles, such as
role="link"to a button.
Checking your markup with devtools and validators
Inspect the accessibility tree in browser developer tools. Check each control's role, accessible name, focus order, and state. Test with Tab, Enter, and Space. Run an HTML validator to find invalid nesting and missing required attributes. An automated check helps, but it cannot replace keyboard and screen-reader testing.
Exercise
Which element should submit a form, and which element should navigate to another page?
Show answer
Use a <button type="submit"> to submit the form and an <a href="..."> to navigate.