Sub topic 2
Buttons or links: pick the element by what it does
This chapter assumes you know basic HTML elements and attributes, basic CSS (selectors, the box model, colors, pseudo-classes), and how to open your browser's developer tools. By the end you will be able to choose between <a> and <button> from what a control does, and you will know the errors that come from choosing wrongly.
The rule: navigate or act
The choice comes down to one question: what happens when the user activates this control?
- If it takes the user to another location, use a link. That location can be another page, a section of the same page, a file, or an email address.
- If it performs an action on the current page, use a button. Examples are saving a form, opening a dialog, toggling a menu, adding an item to a cart, or deleting a record.
Visual style does not decide the element. A link can look like a button, and a button can look like text. The element you choose decides what the browser, the keyboard and assistive technology do with it. Appearance is a separate layer that you add with CSS.
<p>
Read the <a href="/docs/getting-started">getting started guide</a>
before you continue.
</p>
<button type="button" class="btn" id="save">Save changes</button>
<a href="/signup" class="btn">Create an account</a>The first element is an inline link. The second is a button that acts on the page. The third is a link, because it goes to /signup. It shares the btn class, so it is drawn like a button, but it is still a link underneath.
What each element gives you for free
Native elements come with behavior that you would otherwise have to rebuild. The table compares them.
| Behavior | `<a href>` | `<button>` |
|---|---|---|
| Reachable with Tab | Yes | Yes |
| Activated by Enter | Yes | Yes |
| Activated by Space | No | Yes |
| Screen reader announces it as | "link" | "button" |
| Open in new tab, copy address, bookmark | Yes | No |
| Works with JavaScript disabled | Yes | Only as a form submit |
| Can submit a form | No | Yes |
| Can be disabled natively | No | Yes, with `disabled` |
The differences matter to users. A screen reader user hears the role, which tells them what to expect. Someone who hears "link" expects the page to change. Someone who hears "button" expects something to happen where they are. A sighted mouse user also relies on the link's behavior: they can right-click it, middle-click it to open a new tab, or hover to read the address in the status bar. None of this works on a button.
Common errors
A link used as a button
This is the most frequent mistake:
<a href="#" onclick="openMenu(); return false;">Menu</a>The href="#" is a placeholder. It does not go anywhere, so it is not a link. Problems follow:
- A screen reader announces "link", but activating it does not navigate.
- Space does not activate it, while users expect Space on something that acts like a button.
- If JavaScript fails, the click jumps to the top of the page.
- "Copy link address" copies a meaningless
#.
The fix is to use the element that matches the behavior:
<button type="button" onclick="openMenu()">Menu</button>A button used as a link
The reverse mistake is a button that changes the location:
<button onclick="location.href='/pricing'">See pricing</button>This breaks middle-click, "open in new tab", bookmarking and crawler discovery. It also fails outright if the script does not run. Use a link:
<a href="/pricing" class="btn">See pricing</a>A clickable `div` or `span`
A div with a click handler has no role, no keyboard focus and no key handling. You would need to add role="button", tabindex="0", and handlers for both Enter and Space. A real <button> gives you all of that with no extra code. Reach for a native element first.
A button without a type
Inside a <form>, a <button> defaults to type="submit". A button you meant as a plain action, such as "Show password", will submit the form and reload the page. Write type="button" on every button that is not meant to submit. Write type="submit" on the ones that are, so the intent is visible in the markup.
Links that open a new tab
If a link must open in a new tab, say so in the visible text or with an icon that has a text alternative. Add rel="noopener" for external destinations. Do this sparingly, because the user can already choose to open a new tab, and taking that choice away is disruptive.
Borderline cases
Some controls sit between navigation and action. Decide by the result for the user.
| Control | Element | Reason |
|---|---|---|
| "Sign up" that goes to a registration page | Link | It changes the location |
| "Create account" that submits the registration form | Button | It performs an action |
| "Skip to main content" | Link | It moves to a section of the page, using `href="#main"` |
| "Download report" that serves a file | Link | It points to a resource, optionally with the `download` attribute |
| "Generate report" that builds a file on demand | Button | It triggers work |
| Tabs that swap panels on one page | Button | They change what is shown without navigating |
| Pagination "Next" that loads a new URL | Link | Each page has its own address |
| "Load more" that appends results in place | Button | It acts on the current page |
A useful test is to ask whether the result has an address worth sharing. If a user could paste the result into a message and expect a friend to see the same thing, it is navigation and should be a link. If the result depends on the state of this session, it is an action and should be a button.
Single-page applications blur this line, because a route change is done by script. The rule still holds. A route change is navigation, so the control should render as a real <a href> with a real URL. The router can intercept the click. Do not replace the anchor with a button.
A decision flow
The following diagram turns the rule into steps you can apply to any control.
Styling a link like a button
Design often calls for a prominent call to action that navigates, such as "Create an account". The element stays a link. You add button styling through a shared class. This is the code from the third element above, together with the shared rule.
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
min-height: 44px;
padding: 0 1.25rem;
border: 2px solid #1d4ed8;
border-radius: 8px;
background: #1d4ed8;
color: #ffffff;
font: inherit;
font-weight: 600;
text-decoration: none;
cursor: pointer;
}Three properties in this rule exist because <a> and <button> have different browser defaults:
font: inheritmakes a<button>use the surrounding font. Browsers otherwise apply a system font to buttons.text-decoration: noneremoves the underline that links get by default.display: inline-flexwithmin-heightgives both elements the same box. Anchors are inline by default and do not respect height the way buttons do.
Be aware of one behavior difference that styling cannot hide: a link responds to Enter only, and a button responds to Enter and Space. If you style a link as a button, keyboard users who press Space will scroll the page instead of activating it. For most navigation that is acceptable, because the control is still announced as a link. Do not add a script to make Space activate a link, because that makes its role inconsistent.
The reverse is also possible. You can strip a button to look like text for low-priority actions, but keep a clear cue such as an underline or an icon so users can tell it is interactive. The later chapters on button and link visual design cover those cues.
Why not use ARIA roles instead?
You can write <a role="button"> or <div role="link">. The role changes what assistive technology announces, but it does not add behavior. An <a role="button"> still does not respond to Space, and a <div role="link"> still cannot be focused. ARIA makes a promise to the user, and your code must keep it. When a native element already matches the behavior, it is the better choice, because the browser keeps that promise for you.
Checking your choice
Use these checks in the browser:
- Press Tab until the control has focus, then press Enter. Then try Space. A button should respond to both, and a link to Enter.
- Right-click the control. A real link offers "Open in new tab" and "Copy link address".
- Open the accessibility tree in developer tools and read the role. It should match the behavior you intended.
- Disable JavaScript and test again. Links should still work.
Exercise
A "Show more" control on a product page reveals extra specifications in the same page and does not change the URL. Which element should you use, and what attribute prevents it from submitting a surrounding form?
Show answer
Use a <button>, because it performs an action on the current page and does not navigate. Add type="button" so it does not default to type="submit" inside a form.
Exercise
A designer wants "View all courses" to look like a large blue button. It opens the page at /courses. Should you use <a> or <button>, and which CSS properties help make the two elements look the same?
Show answer
Use <a href="/courses">, because it goes to another location. Give it a button class that sets display: inline-flex, a min-height, padding, background, border radius, font: inherit and text-decoration: none.
Exercise
You find <a href="#" onclick="toggleFilters()">Filters</a>. Name two problems this causes and give the fix.
Show answer
Possible problems are that a screen reader announces a link that does not navigate, Space does not activate it, and the page jumps to the top if the script fails. Replace it with <button type="button">Filters</button> and attach the handler to the button.