Web Fundamentals · Lesson 6 of 10 · 5 min
Introduction to ARIA: Roles & Landmarks
This is how you answer "How would you make this custom component accessible?" without sounding like you just read the MDN docs.
When native HTML elements aren't enough for the complex UI you're building, ARIA is your tool for bridging the gap for assistive technologies. But using it correctly requires knowing the first rule of ARIA.
The First Rule of ARIA: Don't Use It
The most important accessibility principle is this: if a native HTML element or attribute can do the job, use it.
Don't use <div role="button"> when you can use <button>. Don't use <div role="list"> when you can use <ul>. Native elements come with keyboard accessibility, focus management, and semantic meaning built-in. When you use ARIA, you are signing up to implement all of that yourself.
This isn't just a best practice; it's a critical filter interviewers use. Violating it suggests you don't understand the trade-offs. "No ARIA is better than bad ARIA" is a common refrain for a reason.
Use this mental model:
Structuring Your Page with Landmarks
Landmarks are a type of ARIA role that defines the major regions of your page. Screen reader users can use them to jump directly to the main content, the navigation, or the footer, bypassing everything in between.
The good news is you get most of this for free with semantic HTML.
| HTML5 Element | Implicit ARIA Landmark Role |
| :------------ | :-------------------------- |
| <header> | role="banner" |
| <nav> | role="navigation" |
| <main> | role="main" |
| <aside> | role="complementary" |
| <footer> | role="contentinfo" |
The trap: Adding a redundant role, like <main role="main">. This just adds noise and signals you might not know that HTML5 elements have implicit roles. The only time you'd add one of these roles is if you're forced to use a div for a structural section.
A few landmarks don't have a dedicated HTML element. The most common is role="search", which you can apply to a div or <form> that contains your site's search functionality.
If you have multiple landmarks of the same type, like two <nav> elements for primary and secondary navigation, you must label them so users can tell them apart. Use aria-label for this.
<nav aria-label="Main">...</nav>
...
<nav aria-label="On this page">...</nav>
Building Custom Widgets with Roles
When you build a component that has no native HTML equivalent—like a tab interface, a custom slider, or a menu bar—you use ARIA widget roles. These roles tell assistive technology what the element is. A <div> with role="tab" is announced as a tab, not just a generic container.
This is where the trade-off hits. Adding role="button" to a <div> does not make it a button. It only changes how it's announced. You are now responsible for everything else a user expects from a button.
The interview question: "You've added
role="button"to adiv. What else do you need to do?"The answer must include these three things:
- Make it focusable. Add
tabindex="0".- Make it work with a keyboard. Add an event listener that triggers its action on
EnterandSpacekey presses.- Ensure it has an accessible name. If it only contains an icon, you need to provide a label via
aria-label.
This code demonstrates the minimum work required to make a non-semantic element accessible as a button. It's far more work than just using <button>.
// HTML: <div id="custom-btn" class="btn-style">Save</div>
const customButton = document.getElementById('custom-btn');
if (customButton) {
// 1. Declare its role.
customButton.setAttribute('role', 'button');
// 2. Make it focusable.
customButton.setAttribute('tabindex', '0');
// 3. Handle keyboard activation.
customButton.addEventListener('keydown', (event) => {
// Buttons are activated by Enter or Space.
if (event.key === 'Enter' || event.key === ' ') {
// Prevent the default action for the space bar (scrolling).
event.preventDefault();
// Trigger the button's action.
handleClick();
}
});
// 4. Handle click activation.
customButton.addEventListener('click', (event) => {
event.preventDefault();
handleClick();
});
}
function handleClick() {
console.log('Custom button activated!');
}
This pattern applies to any interactive ARIA role. If you assign a role, you are also signing a contract to implement the full keyboard interaction model that users expect for that role.
How to Answer "Design an Accessible Tab Component"
This is a classic question that combines your knowledge of roles, states, and properties. Here’s how to structure your answer.
-
Start with the roles. "I'd structure this with three key ARIA roles:
role="tablist"for the container of all the tab controls,role="tab"for each individual tab control, androle="tabpanel"for each content area that a tab reveals." -
Connect tabs to panels. "To link a tab to its panel, I'd use
aria-controlson the tab, pointing to the ID of the panel. On the panel, I'd usearia-labelledbypointing back to the ID of the tab." -
Describe the state. "The currently active tab needs
aria-selected="true", and all other tabs needaria-selected="false". The visible tab panel should be present in the DOM, while the others should be hidden, for example with thehiddenattribute." -
Explain keyboard navigation. This is the most critical part. "Keyboard interaction is essential. The user should be able to
Tabto the active tab in thetablist. Then, they should use the Left and Right arrow keys to switch focus between tabs.EnterorSpaceshouldn't be needed to select a tab; selection should follow focus as the user arrows through.Tabagain should move focus from the active tab into its associatedtabpanel."
This answer shows you understand the complete picture: semantics (role), relationships (aria-controls), state (aria-selected), and behavior (keyboard navigation).
Try it
Find a component on a live website that should be a button but is implemented as a div or a tag (a settings icon, a "like" button, etc.). Open your browser's dev tools. Add the role="button" and tabindex="0" attributes. See if you can navigate to it with your keyboard and activate it with Enter or Space. Note what breaks.
Next: ARIA States & Properties for Dynamic Content
Lock it in — apply this lesson under interview conditions.