Skip to content

    Frontend System Design

    Semantic HTML, ARIA and Accessible Names

    See how a browser picks the accessible name of a control, why native HTML beats ARIA, and where aria-label, aria-labelledby and aria-hidden help or hurt.

    By Published 13 min read

    • frontend-system-design
    • accessibility
    • aria
    • accessible-name
    • semantic-html
    Share

    Standard lessonintermediateReact labFrontend System Design

    Before this: Accessibility Fundamentals and WCAG

    What you'll learn: By the end of this lesson you will be able to explain what an accessible name is and why a control without one is unusable, list the sources a name can come from and the order in which they win, choose between a native label, aria-label and aria-labelledby, say when aria-describedby is the right tool instead, avoid aria-hidden on anything focusable, keep the visible label inside the accessible name, and read a small TypeScript model of the name computation, noting where it is simpler than the specification.

    Think of a name tag at a conference: when someone points at you, others can say who you are. A button with no accessible name is a person with a blank badge. A screen reader user hears "button" and nothing else; a speech-input user has nothing to say to activate it; a tester cannot find it by role and name. The accessible name is the badge, and the browser has a fixed procedure for deciding what is printed on it.

    The running example is the buy box of a product page on a grocery marketplace (the ShopStream front end): a quantity field, an icon-only increase button, an "Add to basket" button, a "Save" button and a basket link that shows only an image. Accessibility Fundamentals and WCAG covered the criteria that require names, and semantic HTML the native elements; this lesson goes inside the mechanism.

    The core idea

    1. Every control has a role and a name. The role says what it is, the name says which one it is. Together they are what assistive technology announces and what speech input and tests look for.
    2. The name is computed, not set. The browser picks it from several possible sources in a fixed order, so adding one attribute can silently override another.
    3. ARIA adds information; it does not add behaviour. MDN says ARIA only exposes extra information to the accessibility API. A native element gives name, role and keyboard handling together; ARIA on a generic element gives only what you write.

    Roles, names and descriptions

    The name is a short label ("Quantity"); the description is extra information, and MDN says a label should be concise while a description is more verbose (aria-describedby points at it). WCAG 4.1.2 (Name, Role, Value, Level A) requires the name and role of every component to be programmatically determined; standard HTML controls already meet it when used according to specification, and custom controls need additional measures. Related criteria: 1.1.1 (a control needs a name that describes its purpose), 2.5.3 (Label in Name) and 2.4.4 (Link Purpose in Context), both Level A.

    Where a name comes from

    The Accessible Name and Description Computation specification (version 1.2 as read for this lesson) lists the steps in order. Read as a list of "first match wins" rules, the relevant ones are:

    1. A hidden element that is not part of a reference traversal has no name.
    2. aria-labelledby with at least one valid ID reference: the referenced elements' text, in order, joined with spaces. An element's aria-labelledby is not followed again while another one is being followed.
    3. An embedded control inside a label (a text field's value, a menu's chosen option). This lesson's model leaves it out.
    4. aria-label, if it is not empty and not just whitespace.
    5. A native text alternative from the host language: HTML alt, a label, and so on.
    6. Name from content, if the role allows it (a button, a link, a heading) or the node is referenced by aria-labelledby.
    7. A tooltip attribute (title), only if nothing else gave a result.

    Name from content applies only to roles that support it (the WAI-ARIA specification lists them): a button is named by its text, a plain div is not.

    Figure 1. The order in which a name is chosen

    flowchart TD accTitle: The order of sources for an accessible name accDescr: A top-to-bottom decision list. If the element is hidden it has no name. Otherwise use aria-labelledby if it gives text, then aria-label if not blank, then a native label such as label, alt or value, then the element's own content if its role allows name from content, then the title attribute. If none gives text the name is empty. A["Hidden element?"] -->|"yes"| Z["No name"] A -->|"no"| B["aria-labelledby
    gives text?"] B -->|"yes"| N1["Use it"] B -->|"no"| C["aria-label
    not blank?"] C -->|"yes"| N2["Use it"] C -->|"no"| D["Native label:
    label, alt, value"] D -->|"found"| N3["Use it"] D -->|"none"| E["Role allows
    name from content?"] E -->|"yes, text found"| N4["Use content"] E -->|"no"| F["title attribute"] F --> N5["Use it, or empty"]

    How to read this: go down until a step produces text. The first one that does wins, which is why an aria-label on a button hides its visible text from the computation and aria-labelledby beats both.

    Illustration 1: the minimal example

    MDN's aria-labelledby example puts three sources on one button:

    <button aria-label="Blue" aria-labelledby="color">Red</button>
    <span id="color">Yellow</span>

    MDN states the accessible name is "Yellow": aria-labelledby takes precedence over aria-label and over the element's inner text. The lab below reproduces this as its first case.

    The sources, one at a time

    A native label. MDN describes two ways to tie a label to a control: for matching the control's id, or nesting the control inside the label. It recommends explicit association (and says you can nest and also provide id and for). A screen reader reads the label on focus, and clicking it passes focus to the control. MDN warns against putting other interactive elements, or headings, inside a label.

    alt on images. MDN calls alt mandatory for accessibility. An empty alt="" marks an image that is not a key part of the content (implicit role presentation); with no alt, some screen readers may announce the file name. title is not an acceptable substitute.

    Content. A button or link is named by its text. An image inside a link contributes its alt, so <a href="/basket"><img alt="Basket"></a> is a link named "Basket". Content that is aria-hidden contributes nothing.

    value and title. In the model, a submit or button input is named by its value; title is the last resort.

    ARIA attributes for names

    aria-label. MDN says it names an element as long as the role does not prohibit naming; use it where there is no visible text, such as an icon-only button. In practice it is supported on interactive elements, widgets, landmarks, images and iframes, and not on generic, which is what a plain div or span is. If visible text labels an element, use aria-labelledby instead.

    aria-labelledby. MDN says the referenced content is combined in the order referenced, an aria-labelledby on a referenced element is ignored, and hidden referenced elements still contribute. It defines only the name: it does not make clicking the label activate the control, so a native label is preferred.

    aria-describedby. The description, not the name: MDN says use it for plain text, and aria-details for extensive or structured content.

    aria-hidden. MDN says aria-hidden="true" removes an element and its children from the accessibility tree without hiding anything visually. Do not use it on focusable elements or on their ancestors: the keyboard can reach the control but assistive technology cannot name it.

    Figure 2. The same button, as the accessibility tree sees it

    Markup, name computation and what a screen reader or speech tool receivesMarkup to Name computation; Name computation to Accessibility tree (role + name); Accessibility tree to Screen reader; Accessibility tree to Speech input; Accessibility tree to Role queriesMarkup: button, attributesMarkupbutton,attributesName computation: first match winsNamecomputationfirst match winsAccessibility tree: role, name, stateAccessibilitytreerole, name, stateScreen reader: announcesScreen readerannouncesSpeech input: says the nameSpeech inputsays the nameRole queries: find by nameRole queriesfind by namerole + name

    How to read this: the name is computed once and consumed by three clients. A screen reader announces it, speech input listens for it, and a role-and-name test looks it up. If the name is wrong, all three go wrong together.

    Native first, ARIA second

    The W3C "Using ARIA" note states the first rule: if a native HTML element or attribute has the semantics and behaviour you need already built in, use it instead of re-purposing an element and adding an ARIA role, state or property. Its second rule says not to change native semantics unless you really have to, its third that all interactive ARIA controls must be usable with the keyboard, and its fourth that you must not use role="presentation" or aria-hidden="true" on a focusable element. At the time of writing that document is marked a discontinued draft kept for reference, pointing to the ARIA Authoring Practices Guide for current guidance.

    MDN's page on role="button" shows what the shortcut costs. The role on a div "does not provide other typical button functionality such as click events and keyboard handling": the element needs tabindex, and you must handle click and keydown yourself, including Enter and Space. MDN recommends a native button, which provides keyboard and focus behaviour by default.

    So prefer, in order: a native element with visible text or a label; the same with aria-label when there is no visible text; a custom element with a role, name, tabindex and key handling only when no native element fits.

    Keep the visible label in the name

    WCAG 2.5.3 Label in Name says the name contains the text presented visually, because people who use voice interaction use the visible labels in their commands. Capitalisation and most punctuation are generally ignored, and the visible text is best placed at the start of the name. A button that shows "Save" and is named "Save to wish list" can be activated by saying "click Save"; one named "Favourite" cannot.

    WCAG 2.4.4 adds the link case: a link's purpose should be determinable from its text, or from the text with its programmatically determined context (the same paragraph, list item or table cell). A column of links that all read "Read more" fails when nothing around them says more.

    Figure 3. Choosing how to name a control

    flowchart TD accTitle: Choosing how to give a control an accessible name accDescr: A decision tree. If the control has visible text, use native content or a label element. If it has visible text somewhere else, tie it with a label or aria-labelledby. If it has no visible text, such as an icon button, use aria-label and keep any visible words in the name. If the name must be longer than the visible text, start the name with the visible text. A{"Is there visible
    text for it?"} A -->|"yes, inside it"| B["Native content
    (button, link)"] A -->|"yes, elsewhere"| C["label for, or
    aria-labelledby"] A -->|"no, icon only"| D["aria-label,
    hide the icon"] B --> E{"Need more words
    for context?"} C --> E E -->|"yes"| F["Start the name with
    the visible text"] E -->|"no"| G["Done"]

    How to read this: let visible text name the control wherever you can, so name and label cannot drift apart; use aria-label when there is no text, and begin an extended name with what the eye sees.

    Build it

    The lab is examples/frontend/labs/accessible-name-model. It is a simplified model, not the accessible name specification. It follows the order of the steps above for a few HTML elements and roles. It has no CSS (no display, generated text or block spacing), no embedded controls, no SVG title, no default names such as "Submit", and it does not follow aria-labelledby on descendants. A browser or screen reader can give a different answer. The code is a pure TypeScript core plus thin React:

    • src/model.ts has roleOf and accessibleName, returning the name and which step produced it. src/audit.ts has eight checks (missing alt, no name, aria-hidden on something focusable, visible text missing from the name, a name on a generic element, a click handler on a generic element, a dangling aria-labelledby, a duplicate id).
    • src/components/BuyBox.tsx renders the buy box twice, built from native elements and as "div soup". The tests check that the rendered DOM equals the fixtures the model reads, and use Testing Library's role queries, which filter by accessible name, as a second opinion on all 14 cases. That shows where the model and that library agree; it does not show that a browser or screen reader agrees.

    Verified. The 54 tests passed under Node 26.13.1 and Vitest 5.1.0 in jsdom (a JavaScript model of the DOM, not a browser), and the typecheck passed. The demo's first part shows which step names each of 14 cases.

    Expected output, captured from a real run (the first seven cases of the demo):

    === 1. which step names the element (simplified model)
    labelledby-wins          role=button     source=aria-labelledby name="Yellow"
    aria-label-over-content  role=button     source=aria-label      name="Close"
    label-for                role=searchbox  source=label           name="Search products"
    label-wrapping           role=spinbutton source=label           name="Quantity"
    img-alt                  role=img        source=alt             name="Fresh basil leaves"
    img-empty-alt            role=none       source=none            name=""
    button-content           role=button     source=content         name="Add Fresh Basil to basket"

    The first line reproduces MDN's "Yellow" example, and the sixth shows alt="" taking the image out of the tree. The other cases cover a blank aria-label, a hidden label and unchained aria-labelledby.

    Illustration 2: the realistic example, the buy box

    The same product page buy box built from generic elements: the model finds four things by role, three with an empty name, and the audit reports eight findings.

    Expected output, captured from a real run (the start of part 2 of the demo):

    === 2. buy box built from generic elements
    controls the model finds (role, name, source):
      input#qty        spinbutton ""                     none
      a#basket         link       ""                     none
      img              img        ""                     none
      button#save      button     "Favourite"            aria-label
    audit: 8 finding(s)

    The eight findings are no-name (the quantity input and the basket link), click-on-generic (the "Add to basket" div and the wish list icon), missing-alt, label-not-in-name (the Save button), hidden-focusable (a Close button with aria-hidden) and name-on-generic. The div buttons are missing from the controls list: with no role there is nothing for the model, or a role query, to list, and the tests confirm that getByRole("button", { name: "Add to basket" }) finds nothing. The model cannot see event listeners, so the lab marks them with a data-click attribute, a device of the lab that real pages do not have.

    The same box built from native elements has no findings.

    Expected output, captured from a real run (the end of part 3 of the demo):

      a#basket         link       "Basket"               content
      img              img        "Basket"               alt
    audit: 0 finding(s)

    Native button and label supply role, name and keyboard behaviour; the icon-only button needs one aria-label and a hidden icon; and "Save" is named "Save to wish list", which starts with the visible word.

    Illustration 3: a failure

    In the soup box a designer gives the button showing "Save to wish list" aria-label="Favourite". The audit reports label-not-in-name: a speech-input user who says "click Save to wish list", the words on the screen, does not match the name "Favourite". Sighted users see nothing wrong and a screen reader user hears a plausible name, so only a 2.5.3 check or a speech test finds it. Likewise the Close button with aria-hidden="true" can be reached with Tab but is removed from the tree.

    Not shown: how any real browser maps these names, what a screen reader says, or how speech software matches names.

    Trade-offs

    Choice Gain Cost
    Visible text names the control Name and label cannot drift apart Needs a place for the text in the design
    aria-label for icon buttons Names a control with no text Not shown to sighted users; must be kept in sync and translated
    role plus script on a div Design freedom You own focus, keys and states

    In practice: the icon button in the shared component library

    NOTE

    This is a realistic scenario, not a case history. It is a composite built from common production patterns. It describes no specific company or product, and every number in it is an illustrative assumption.

    The situation

    The shared component library has an IconButton that takes an icon and a click handler, used for the basket stepper, the wish list and the close buttons. An audit of the product page finds six icon buttons with no name and two whose names do not match their visible text. Fixes so far have been page by page.

    Starting architecture

    Figure 4. A component that cannot say what it is

    Starting architecture: an icon button with no required nameProduct page to IconButton; IconButton to Accessibility tree (no label); Accessibility tree to Screen reader user (unnamed)Product page: uses IconButtonProduct pageuses IconButtonIconButton: icon, onClickIconButtonicon, onClickAccessibility tree: button, no nameAccessibilitytreebutton, no nameScreen reader user: hears: buttonScreen readeruserhears: buttonno labelunnamed

    How to read this: the warning boxes are where the name goes missing.

    Options considered

    Option What it fixes What it costs
    Fix each page Each page's names Repeated work; regressions on new pages
    Required label prop that sets aria-label and hides the icon; text variant uses its text as the name Every use is named, names match visible words A small API change; callers must supply text

    The decision

    Field Decision record
    Context Unnamed and mismatched icon buttons come from one component.
    Decision Make the label required in the type. With visible text, the text is the name; with only an icon, set aria-label and hide the icon. Test each variant by role and name, and audit for names that omit the visible text.
    Consequences The fault is fixed where it is produced; callers must supply text, which needs copy and translation.

    Rollout and verification

    1. Change the type so a missing label fails compilation, and list the callers it breaks.
    2. Write component tests that query by role and name, as the lab does, and add the audit check for visible text missing from the name.
    3. Run the manual checks: keyboard, screen reader and speech input on the buy box.

    The 2 a.m. failure

    A developer shortening a card replaces the visible text "Add Fresh Basil to basket" with an icon and sets aria-label="Buy". A customer who uses speech input reports that the spoken command "click Add Fresh Basil to basket" matches nothing. No unit test failed, because the tests found the button by its new name.

    First three checks: (1) compare visible text with the name for every button in the card; (2) find the commit that changed the label and its test; (3) check whether a 2.5.3 audit exists. Mitigation: put a visible word back in the name, for instance "Buy Fresh Basil". Follow-up: the audit check, and a review rule that a name begins with any visible text.

    Architect's notes

    • Let visible text be the name, so it cannot drift and speech users can see what to say.
    • Make the name a required input of a component, not a convention.
    • Treat aria-hidden as dangerous, and check that nothing inside can take focus.

    Common mistakes

    • A div or span with a click handler and no role.
    • A name that does not contain the visible text.
    • aria-hidden="true" on or around something focusable.
    • An image with a missing alt, or links that all read "Read more".

    Key takeaways

    • A control's name and role are what assistive technology, speech input and tests all use.
    • The name comes from the first matching source: aria-labelledby, aria-label, a native label, content, then title.
    • ARIA adds information to the tree, not behaviour: prefer native elements.
    • Never put aria-hidden="true" on focusable elements or their ancestors, and keep the visible label at the start of the name (WCAG 2.5.3).
    • The lab model is simplified: it agrees with Testing Library on its cases, not necessarily with a browser or screen reader.

    Practice exercises

    Exercise 1: Starter (30 min). Change one case so a different step wins, and predict the new source before you run the demo.

    Exercise 2: Intermediate (2 hours). Add cases for aria-labelledby with two ids and with a missing id, and check them against the role query in the React test.

    Exercise 3: Advanced (half day). Add an audit check for two nav elements with the same name, and list what the model would still miss.

    In this course:

    Official documentation

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments