DEV Community

Cover image for A11Y 101: Buttons vs links
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

A11Y 101: Buttons vs links

Let's set one thing straight before we even dive into the article.

Divs are not buttons!

Yes, they are sometimes easier to style, but we should always opt for semantic HTML when introducing actionable items.

Alright, that's out of the way. Now, let's dive into the matter of links vs. buttons.

Is there a difference?

When we browse the modern-day web, you'll struggle to differentiate buttons from links (let aside divs).
Many style these elements the same, but this is not the case for screen readers.

They can still easily distantiate between the two.

When to use a link or a button?

I see people frown and wonder, ok, I get it, but when do I use which one?

And the answer to that is straightforward.

  • A link will trigger a URL change.
  • A button will trigger an action.

Let's look at some examples of valid links.

Our main navigation menu: <a href="mywebsite.com/about">About</a>.
A one-pager layout: <a href="#contact">Contact</a>

Key point: If it's changing the URL, use a link!

And around the button, they adhere to almost all other interactions on your website.

For example form submits: <button type="submit">Send contact request</button>.

But they can also include certain actions for instance liking a article: <button aria-label="upvote" onclick="upvoteArticle()"><svg /></button>.

There will be a ton more of the button examples, but keep the basic two rules in mind. It's all you ever need.

  1. URL change occurs? 👉 Link
  2. No URL change? 🫢 Button

What about styling?

Now, we know we can style buttons as links and links as buttons.

But what is valid?

If we looked at how the web is supposed to work, I would strongly urge you to keep elements as they are.

Links should be identifiable by users as such, and the same goes for buttons.
However, when this is impossible for whatever reason, still opt for the right semantic solution.

So, how do screen readers see them?

Let's set up a simple test by adding a button and a link and see what happens.

<p>
  Some cool text about a topic.
  <a href="https://daily-dev-tips.com">read more on daily-dev-tips.com</a>.
</p>

<p>
  Or we could opt this alert?
  <button onclick="alert('Hi there')">alert me</button>
</p>
Enter fullscreen mode Exit fullscreen mode

When using keyboard navigation, we can tab onto both elements.
However, the interactions are different.

  • Link: We can press enter, and it's collected in the Links window
  • Button: We can press enter or space, and it's collected in the Form controls window

VoiceOver links and button navigation demo

Within the demo, we see the Mac VoiceOver link and button behavior.
You can see the links are announced as such, and the button as well. For the button, we can either press enter or tab.

In the last section of the video, we see the voiceOver rotation menu. This is a way to go over all types of elements.
It's important to note that people use this to identify all links or buttons quickly.

If you style your links as buttons, people might not even find them.

And if you style buttons as links, they might get surprised by unexpected behaviors.

Conclusion

Picking the right semantic element is very important.
Use links for URL changes and buttons for any other actions.

And for those who forgot, don't use divs! 😈

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Discussion (4)

Collapse
preciouschicken profile image
Precious Chicken

Links = URL change, Buttons = Actions. Brilliant. That's one of those things that is completely obvious when you see it written down, but if someone had asked you previously to sketch out the difference you would have struggled.

Collapse
dailydevtips1 profile image
Chris Bongers Author

Yeah, I must admin there where times I did it wrong.
Mainly coming from the stone-age of development where it was needed to abuse buttons as links 🤦‍♂️.

However loved this simplification 💖

Collapse
jmau111 profile image
jmau111

Nice writeup, but what's wrong with styles?

I thought one of the benefits of using semantic is that you can make your links look like buttons if you want instead of using inappropriate HTML markup to get the desired apparence.

Collapse
dailydevtips1 profile image
Chris Bongers Author

Thanks!

Nothing wrong with styles, it's more about not fooling the users.
Users might expect different actions that you show.

The semantics should always win though
So link = url, button = action.
If you then want to style links as buttons that's a choice, my advice would be against it (if you can)