HTML role Attribute

Beginner
6 min read
Updated: Nov 2026
3 examples
Live preview

Definition and Usage

The role global attribute declares an ARIA role — how assistive technologies should interpret an element. Use it for custom widgets when no native HTML element provides the right semantics.

Prefer native elements first: <button>, <nav>, <dialog>, headings, lists, and form controls already expose roles correctly. Adding the wrong role can hurt accessibility.

First rule of ARIA.

If a native element exists for the job, use it. role fills gaps; it does not replace good HTML.

Role alone is not enough.

Custom widgets also need keyboard support, focus management, and matching aria-* states.

HTML Syntax

syntax.html
<element role ="rolename">

Value is an ARIA role token such as button, dialog, navigation, or status.

Attribute Values

ValueDescriptionTry it
buttonExpose a non-button as a button (prefer <button>)Try it
status / live regionsAnnounce updates politely to assistive techTry it
navigationLandmark role (prefer <nav>)Try it

Live Preview

Compare a native button with a div role="button" that implements Enter/Space.

role=button

Both should update the note. Prefer the native button in real apps.

Result

Click either control

HTML Examples

Example 1 — Custom Button Role

Only when you cannot use <button> — add keyboard support yourself.

HTML
<div role="button" tabindex="0" id="go">Go</div>
<script>
  go.addEventListener("click", () => alert("go"));
  go.addEventListener("keydown", (e) => {
    if (e.key === "Enter" || e.key === " ") { e.preventDefault(); go.click(); }
  });
</script>
Try It Yourself

How It Works

  1. role="button" exposes button semantics.
  2. tabindex="0" puts it in the tab order.
  3. Handle Enter and Space like a real button.
  4. Prefer <button> to avoid this boilerplate.

Example 2 — Live Status Region

Announce non-urgent updates with role="status".

HTML
<p role="status" id="msg">Ready</p>
<button type="button" onclick="msg.textContent='Saved'">Save</button>
Try It Yourself

How It Works

  1. status is a polite live region.
  2. Text changes can be announced by screen readers.
  3. Use for confirmations, not critical alerts (alert).
  4. Keep messages short and meaningful.

Example 3 — Prefer Native Landmarks

<nav> already has an implicit navigation role.

HTML
<!-- Good -->
<nav aria-label="Main">...</nav>

<!-- Avoid unless you cannot use nav -->
<div role="navigation" aria-label="Main">...</div>
Try It Yourself

How It Works

  1. Native landmarks already map to ARIA roles.
  2. aria-label distinguishes multiple navs.
  3. role="navigation" is a fallback, not a first choice.
  4. Audit with a screen reader or accessibility tree tools.

Edge Cases & Pitfalls

Wrong roles are worse than no roles.

native

Do not override good semantics

Avoid role on elements that already expose the correct role unless fixing a real bug.

keyboard

Interactive roles need keyboards

role=button without Enter/Space support fails accessibility.

name

Accessible name still required

Use text content, aria-label, or aria-labelledby.

presentation

none/presentation strip meaning

Never use them on focusable or meaningful controls.

Browser Support

role is an HTML attribute for browsers. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

role

ARIA role is supported across modern assistive technologies and browsers.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer Partial / legacy
Partial
role Widely supported

Bottom line: Safe to use. Prefer semantic HTML first; validate custom widgets with a screen reader.

Key Takeaways

  • Role: declare accessibility semantics for assistive tech.
  • Prefer native HTML whenever a built-in element exists.
  • Pair with ARIA and real keyboard support for custom widgets.
  • Do not override correct native roles without a strong reason.

One line: role exposes ARIA semantics — use it to fill gaps, not to replace good HTML.

Frequently Asked Questions

It tells assistive technologies what an element is supposed to be in the accessibility tree — for example button, dialog, or navigation — when native HTML semantics are not enough.
No. Prefer native elements (<button>, <nav>, <dialog>, …). Add role only when you must build custom widgets that lack a native equivalent.
Yes. role can appear on any element, though many roles only make sense in specific patterns with supporting ARIA attributes and keyboard behavior.
They remove implicit semantics (useful for decorative tables/images). Prefer modern none; do not strip semantics from interactive controls.
It exposes button semantics, but you must still add tabindex, keyboard handlers, and disabled/pressed states yourself. A real <button> is safer.
role sets the widget type; aria-* properties and states fill in name, expanded, selected, and other details.

Next: rows

Control how many visible text lines a textarea shows.

rows tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful