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. It builds on the idea that an element's appearance should signal what it will do. Before styling anything, you have to choose the right element.
The rule: navigate or act
A link (<a href>) takes the user to another location. That location can be another page, a section of the same page, a file, or an email client. A button (<button>) performs an action on the current page, such as saving, opening a dialog, or submitting a form.
Ask one question: after the click, does the user go somewhere, or does something happen where they are?
| The control does this | Use | Why |
|---|---|---|
| Opens another page or a section | `<a href>` | It changes location |
| Downloads a file | `<a href download>` | It points to a resource |
| Submits a form | `<button type="submit">` | It acts on data |
| Opens a menu or dialog | `<button type="button">` | It changes the current page |
| Toggles a setting | `<button type="button">` | It performs an action |
A worked example
The markup below uses both elements. Each one has a different job.
<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 link sits inside a sentence and navigates to a guide. The button saves something without leaving the page. The last element is a link that carries the btn class. It looks like a button, but it still navigates to /signup, so it stays an <a>.
You can give a link button styling when its purpose is still navigation. The reverse is the mistake to avoid. Do not build a navigation control from a button element, and do not build an action from a link.
Why the element matters
The browser gives each element behavior you would otherwise have to rebuild.
- Keyboard: A link activates with Enter. A button activates with Enter and Space. Both are in the tab order by default.
- Screen readers: They announce "link" or "button". That announcement tells users what to expect before they activate it.
- Browser features: Links support right-click "Open in new tab", middle-click, copy link address, and a status-bar preview of the destination. Buttons do not.
- Forms: A
<button>inside a form submits it by default.
Because of this, the element is part of the signal that a control is clickable. A user who hears "link" expects to move to another page.
Common errors
- A link used as an action.
<a href="#">Delete</a>looks and announces as navigation. Use<button type="button">. - A button used as navigation.
<button onclick="location.href='/pricing'">breaks middle-click and open-in-new-tab. Use<a href="/pricing">. - A missing
typeon a button. Inside a form, a button with notypeis a submit button. Settype="button"for anything that should not submit. - A link with no
href. An<a>withouthrefis not focusable and is not exposed as a link. - A clickable
<div>. It needs a role, a tabindex, and key handlers to match a native button. Use<button>instead.
Styling without changing the meaning
Appearance and meaning are separate. Both of these are valid:
- A link styled as a filled button, for a call to action that goes to a sign-up page.
- A button styled as plain text, for a low-priority action such as "Cancel".
Keep the visual cues consistent with the behavior where you can. A prominent filled shape tells users "this is the main thing to do". The element underneath should still match the real behavior.
Exercise
A "Show more comments" control reveals extra comments on the same page without reloading. Which element should you use, and what attribute should you set if it sits inside a form?
Show answer
Use a <button>, because it performs an action on the current page and does not navigate. Set type="button" so it does not submit the surrounding form.
Exercise
A designer wants "View pricing" to look like a large filled button. It leads to the /pricing page. Which element do you use?
Show answer
Use <a href="/pricing" class="btn">. The control navigates, so it stays a link. The button look comes from CSS.