A11Y 101: Buttons vs links

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Yesterday evening, I had a dilemma about what to use for the image carousel for a website I built. I decided to use buttons instead of links. I see I'm not the only one who thinks so.
In this series I'll explore the basics of accessibility and you can join me on this learning trip.
Regarding accessibility, you'll always hear the following sentence: Semantic HTML always wins. Accordions are sections of content grouped by headers. The user can then collapse or close these sections to show/hide more information. Regarding these a...
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

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.
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.
I see people frown and wonder, ok, I get it, but when do I use which one?
And the answer to that is straightforward.
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.
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.
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>
When using keyboard navigation, we can tab onto both elements. However, the interactions are different.

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.
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 my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter