completefrontend
Open lab My progress
THE WEB COMPONENTS FIELD GUIDE

Labels and descriptions

Learn how to label a web component so screen readers name the right element: inner labels, form-associated elements, aria-describedby, and DevTools.

By the end, you can
  • 01
    Name the right elementFind the element that has the role and gets focus, and give that one its name, from inside the shadow root or from a page label.
  • 02
    Label a form-associated elementUse static formAssociated = true so a page's <label> names the host, and read the labels back with internals.labels.
  • 03
    Describe it and check itAdd a description that reaches the same element, then check the name and description in DevTools and in a test.

How do you label a web component?

Ravi's <tea-cups> box is back on Leaf and Kettle's order page. In lesson 13.3, a page <label for> couldn't reach the number box inside it. So this time Ravi skips the label. He writes the name right on the tag: aria-label="Cups". No id, nothing to cross.

Leela orders with a screen reader. She presses Tab and lands in the box. Her screen reader says it's a spin button. It doesn't say Cups. The name is right there on the tag. Where did it go?

A name only helps on the element that has the role and gets focus. aria-label on a host names the host, not the input inside it. So name the input from inside its own shadow root: with a <label> there, or with text the page slots into one. Or make the host itself the control. Then a form-associated custom element takes the page's own <label>.

Here is Ravi's page:

A name written on the hostPop out in the code editor (opens in a new tab)HTML
<tea-cups aria-label="Cups"></tea-cups><script src="tea-cups.js"></script>
ResultLoading…
  • In the tea-cups.js tab, line 4 attaches a shadow root with delegatesFocus: true. Line 5 puts a number box in it.
  • On the page, line 1 is the host, with aria-label="Cups".

Lesson 9.3 showed what delegatesFocus does. Focus on the host, or a click on it, is handed to the first element inside that can take focus. Here, that's the box.

Click the box in the result. Lesson 13.1 met Chrome's accessibility tree: the list of what each thing on a page is, which screen readers read. Each row has a role and, often, a name. Here it is after the click, shortened as before:

Chrome's accessibility tree after a click on the boxText
generic "Cups"
  spinbutton (no name), focused: true

Line 1 is the host. A tag with no role is generic: a plain box, like a <div>. Chrome gave it a row because of its aria-label, and named it Cups. Line 2 is the number box. Chrome calls it a spinbutton, because its value goes up and down. It has no name. And focused: true says it's the element with focus.

So the name is on one element, and focus is on another. A screen reader reads out the element with focus. That element has no name, so Leela never hears Cups.

A card <tea-cups aria-label="Cups">, labelled generic "Cups" and the host gets the name. An arrow labelled focus lands here goes from it into a window titled shadow root, to a card <input type="number">, labelled spinbutton (no name).<tea-cups aria-label="Cups">generic "Cups"the host gets the namefocus lands hereshadow root<input type="number">spinbutton (no name)
aria-label names the element it's written on, so the host gets a row called Cups. A click or a Tab lands on the box inside, and the box has no name.
Real-life analogyA name badge goes on the person who serves you

Leaf and Kettle hands out name badges. Ravi pins one to the front of the counter, not to Meena. A customer walks up and talks to Meena. She has no badge, so the customer never learns her name. The badge was real. It was just on the wrong thing.

In real life: The badge that says Cups
In HTML: aria-label="Cups"
In real life: The counter
In HTML: The host, <tea-cups>
In real life: Meena, behind the counter
In HTML: The number box inside the shadow root
In real life: A customer walking up to Meena
In HTML: Focus landing on the box
The main idea

Give the name to the element that gets focus. An accessible name is the short text a screen reader says for an element, like Cups. An accessible description is extra text that adds to the name, like Max 6 per order. Both belong on the same element: the one with the role, where focus lands.

Why it matters: nothing on the page looks wrong. The ARIA Authoring Practices Guide notes that WAI-ARIA and WCAG require a name for every focusable control. It says a name is short, often one to three words. This lesson puts the name and the description in the right place, then checks both in DevTools.

Which element should carry the name?

Lesson 13.1 gave each control one owner. The owner is the element with the role. That decides where the name goes:

  • The host wraps a native control, like an <input>. The control inside owns the role, and gets focus. So the name goes on the control inside.
  • The host is the control itself, with a role from its internals and a tab stop. The tea toggle from lesson 13.2 is one. So the name goes on the host.

tea-cups is the first kind. Its box needs the name. But the page only sees the host. How does the page's word reach the box? Try three ways:

Playground: where does the name go?
tea-cups, named three waysPop out in the code editor (opens in a new tab)HTML
<tea-cups aria-label="Cups"></tea-cups><script src="tea-cups.js"></script>
ResultLoading…
Chrome's accessibility tree after a click on the boxText
generic "Cups"
  spinbutton (no name), focused: true
Try it yourself
The host gets the name

aria-label names the element it's written on: the host. Chrome gives the host a generic row named Cups. A click lands on the box inside, and the box has no name.

Line 1 of the page and the tea-cups.js tab change. Click the box in the result, then try clicking the word Cups.
  • aria-label on the host is the overview again. The host is named. The box, where focus lands, is not.
  • A label inside: on the page, line 1 says label="Cups". It's a plain attribute the component reads, not ARIA. In the tab, line 5 has a <label for="box"> and the box, in the same shadow root. Line 8 copies the words into the label.
  • Slotted text in a label: on the page, line 1 writes Cups as the host's text. In the tab, line 5 puts a <label> around a <slot> and the box.

The last two name the box Cups, and the row with focus has the name. Click the word Cups in either one. The cursor jumps into the box, because a real label is tied to it.

Why does the slotted text count? The text stays the page's child. But the slot shows it inside the label. Chrome builds its tree from the flat tree, where slotted content sits in its slot, as lesson 8.2 showed. So the label holds the text and the box at once. The page keeps its words visible, and the component keeps the ids private.

Two more ways reach the box. Both come from lesson 13.3, so here they are in one line each:

  • The box points out at a page label with ariaLabelledByElements. That works in every current browser.
  • The host passes the page's <label for> on to the box with Reference Target. That works in Chrome and Edge 152 and later.

Lesson 13.3 showed both, and the kit's quantity uses both. And when nothing fits, Chrome's guide falls back to aria-label text, set on the box itself.

Why it matters: pick the way by who writes the words. The page writes them? Use a label attribute or a slot. The page already has a <label>? Point at it.

Can a label element name a custom element?

In lesson 13.3, a page <label for> found <tea-cups> and stopped. A label can only name a labelable element, like an <input> or a <button>. A plain custom element isn't one. Google's HowTo: Components project even built a <howto-label> tag in 2017, to name custom elements another way.

Today there's a real answer. The HTML Standard lists form-associated custom elements as labelable. A custom element becomes form-associated with one line in its class: static formAssociated = true. Then a page <label> names the host itself.

This only fits the second kind of control: the host is the control. So here is a switch, like lesson 13.2's tea toggle, made form-associated:

A form-associated switch, labelled two waysPop out in the code editor (opens in a new tab)HTML
<label for="milk">Add milk</label><tea-toggle id="milk"></tea-toggle><label>Add sugar <tea-toggle></tea-toggle></label><script src="tea-toggle.js"></script><script>  for (const toggle of document.querySelectorAll("tea-toggle")) {    console.log("Labels:", toggle.labels.length, toggle.labels[0].textContent.trim());  }</script>
ResultLoading…
  • In the tea-toggle.js tab, line 2 makes the class form-associated. Line 3 asks for its internals, as in lesson 13.2.
  • Lines 6 and 7 give it the role switch and the state not checked. Line 15 puts it in the Tab order.
  • Line 8 prints Focus when it gets focus. Lines 9 to 12 flip it on a click, and print the new state.
  • Lines 17 to 19 share its labels, like a native input's labels. internals.labels is a list of every <label> that names this element.
  • On the page, line 1 is a label with for="milk", and line 2 is a switch with that id. Line 3 wraps a second switch in its label.
  • Lines 6 to 8 print each switch's labels.

It prints Labels: 1 Add milk and Labels: 1 Add sugar. Both ways work: a label with for, and a label wrapped around the tag. Here is Chrome's tree:

Chrome's accessibility tree for the two labelled switchesText
StaticText "Add milk"
switch "Add milk", checked: false
StaticText "Add sugar"
switch "Add sugar", checked: false

Lines 2 and 4 are the two switches, named Add milk and Add sugar by their labels. Now click the words Add milk in the result. The page prints Focus, then Click, checked: true. The switch's row becomes switch "Add milk", checked: true, focused: true.

So in Chrome, a click on the label moves focus to the switch. Then it fires a click event at the switch, which flips it. That's what a checkbox's label does. The HTML Standard leaves a label's click to the platform. It adds that, where a label's click acts on its control, it acts on a form-associated custom element too.

Two halves. Left: a card <label for="milk"> with an arrow down to a card <tea-toggle id="milk">, labelled switch "Add milk" and formAssociated = true. Right: a card <label for="cups"> with an arrow down to a card <tea-cups id="cups">, labelled label.control: null and a plain custom element.<label for="milk"><tea-toggle id="milk">switch "Add milk"formAssociated = true<label for="cups"><tea-cups id="cups">label.control: nulla plain custom element
Both labels find the tag with their id. The form-associated switch is labelable, so it gets the name Add milk. The plain tag isn't, so label.control is null and nothing is named.
Real-life analogyJoining the staff list

Leaf and Kettle's intercom can only call people on the staff list. A visitor isn't on it, so a call for him does nothing. Ravi signs the list on his first day. Now "Ravi to the counter, please" reaches him. He walks over and takes the order.

In real life: The staff list by the intercom
In HTML: The labelable elements
In real life: Ravi signing the list
In HTML: static formAssociated = true
In real life: "Ravi to the counter, please"
In HTML: A <label> pointing at the tag
In real life: Ravi walking over and taking the order
In HTML: Focus, then a click event
In real life: A visitor who isn't on the list
In HTML: A plain custom element: label.control is null

A few more facts about these labels:

  • static formAssociated = true is what makes the tag labelable. attachInternals() gives the class its internals: internals.labels to read the labels, and internals.role for the role.
  • Reading internals.labels on a class without formAssociated throws a NotSupportedError, as MDN says.
  • The host needs a role too. ARIA in HTML allows form roles, like switch, checkbox, or spinbutton, set through the internals. Without one, the host is generic, and a generic element must not be named. HTML-AAM maps it to generic.
  • It needs to take focus, too, as MDN says. Exercise 2 shows a switch with no tab stop.

Safari doesn't use the label as the name of a form-associated custom element, so VoiceOver doesn't read it (WebKit bug 259124). Chrome and Firefox do. So also give the element a name of its own, for example with internals.ariaLabel, as MDN shows, and check it in Safari.

What if the form-associated host wraps an input?

Here is the trap. A component that wraps an input might turn on formAssociated too. Then the page's label names the host:

A form-associated host that wraps a number boxPop out in the code editor (opens in a new tab)HTML
<label for="cups">Cups</label><tea-cups id="cups"></tea-cups><script src="tea-cups.js"></script>
ResultLoading…

In the tea-cups.js tab, line 2 makes it form-associated. Line 5 delegates focus, and line 6 adds the box. On the page, line 1 is the label. Click Cups, and the cursor lands in the box. Here is the tree after that click:

Chrome's accessibility tree after a click on CupsText
StaticText "Cups"
generic "Cups"
  spinbutton (no name), focused: true

Line 2 is the host, named Cups. Chrome names it, though ARIA in HTML says a generic element must not be named. Line 3 is the box. It has focus, and no name. delegatesFocus moves focus, but never the name. It's the overview's problem again. A label names the host, so use formAssociated for naming only when the host is the control.

Matuzović tried labels for shadow DOM inputs and found the same: a label around the host, or pointing into it, doesn't name the input. Form association does much more than labels: a value the form sends, validity, and the form it belongs to. Stage 6, Forms, covers those, from lesson 16.1, Joining a form. If forms in plain JavaScript are new to you, Form properties & elements comes first.

Form-associated custom elements are Baseline widely available, web-features says. They work in Chrome 77, Firefox 98, and Safari 16.4 and later.

How does aria-describedby work with shadow DOM?

A name says what something is. A description adds a little more, like a hint under a box. aria-describedby gives an element a description: it holds the ids of the elements whose text describes it. Like any id reference, it only looks in its own tree.

The simple case keeps everything in one shadow root:

A label, a box, and a hint in one shadow rootPop out in the code editor (opens in a new tab)HTML
<tea-cups></tea-cups><script src="tea-cups.js"></script>
ResultLoading…
  • In the tea-cups.js tab, line 6 is the label. Line 7 is the box, with aria-describedby="hint".
  • Line 8 is the hint: a <small> with the id hint. It's in the same shadow root, so the id is found.
Chrome's accessibility tree for the box and its hintText
StaticText "Cups"
spinbutton "Cups", description: "Max 6 per order"
StaticText "Max 6 per order"

Line 2 is the box: named Cups, described as Max 6 per order. Line 3 is the hint's text, still on the page for everyone to see. The APG asks for that: visible text first.

Real-life analogyThe small print under the name badge

Meena's badge says Meena in big letters. Under it, in small print: Ask me about tea strength. You read the name first, then the small print. Both help only because they're on her badge. The same words on a poster across the room tell you nothing about her.

In real life: The name on the badge
In HTML: The accessible name, Cups
In real life: The small print under it
In HTML: The description, Max 6 per order
In real life: Both on Meena's badge
In HTML: Both on the box, the element with focus
In real life: Small print on a poster across the room
In HTML: A page hint the box can't reach by id

What if the hint lives on the page? Try four ways to give it to the box. The page and the tab stay the same. Only line 8 changes:

Playground: how does the box get the page's hint?
A hint on the page, and tea-cups with a label and a boxPop out in the code editor (opens in a new tab)HTML
<p id="hint">Max 6 per order</p><tea-cups></tea-cups><script src="tea-cups.js"></script><script>  const hint = document.querySelector("#hint");  const cups = document.querySelector("tea-cups");  const box = cups.shadowRoot.querySelector("input");  box.setAttribute("aria-describedby", "hint");</script>
ResultLoading…
Chrome's accessibility tree for this page (shortened)Text
paragraph
  StaticText "Max 6 per order"
StaticText "Cups"
spinbutton "Cups"
Try it yourself
The id stays in the shadow root

The box looks for the id hint in its own tree, the shadow root. The paragraph is on the page, so nothing is found. The box gets no description, and nothing warns you: no error, no console line.

Only line 8 of the page changes. The tea-cups.js tab is the same each time.
  • On the page, line 1 is the hint, a paragraph with the id hint.
  • Lines 5 to 7 find the hint, the host, and the box inside its open shadow root.
  • Line 8 gives the hint to the box, or to the host.

Here is what each choice does:

  • An id on the box: no description. The id is looked up in the shadow root. No error, no warning.
  • An element on the box: described. ariaDescribedByElements points out at the page, as lesson 13.3 showed. MDN notes the elements need no ids.
  • An id on the host: the host is described, as a generic row. The box gets nothing. It's the overview's mistake, with a description.
  • Words on the box: described. ariaDescription sets the aria-description attribute: the text itself, with no element to point at.

aria-description is handy when no visible text fits. But MDN notes it's still in the ARIA 1.3 draft, and says to keep using aria-describedby for now. If an element has both, aria-describedby wins. Chrome 83, Firefox 119, and Safari 17.4 all have the ariaDescription property.

What about error messages?

An error, like Too many cups, has its own attribute: aria-errormessage. It points at the error's text, and it counts only while aria-invalid="true" is set, MDN says. Like aria-describedby, it holds ids, so the error text must be in the same shadow root, or reached with ariaErrorMessageElements. Lesson 16.3, in Forms, builds validation messages for real.

Why it matters: the hint belongs where the name is. Put both on the element with focus, in one shadow root if you can.

How do you check an accessible name in DevTools?

Every tree in this lesson came from Chrome. You can read the same thing for any element, with the steps in the DevTools docs:

  1. Right-click the box in a result, and pick Inspect.
  2. In the Elements panel, find the box inside its #shadow-root, and click it.
  3. Open the Accessibility tab, in the same row of tabs as Styles. It may hide behind the » button.
  4. Read Computed properties. Name, Description, and Role are there.

Here is the top of it for the hint example's box:

A window titled Accessibility, under the heading Computed properties. Row: Name: "Cups". Under it, four sources: aria-labelledby: Not specified, aria-label: Not specified, a card From label (for= attribute): "Cups", and title: Not specified. Last row: Description: "Max 6 per order".AccessibilityComputed propertiesName: "Cups"aria-labelledby: Not specifiedaria-label: Not specifiedFrom label (for= attribute): "Cups"title: Not specifiedDescription: "Max 6 per order"
The top of the Accessibility tab for the hint example's box, drawn. Chrome lists every place it looked for a name. Only the label had text, so the name is Cups. The description comes from the hint.

Under Name, Chrome lists each place it looked, in order. A row with nothing says Not specified. Here only the label had text, so it wins. In DevTools' own code, a row that had text but lost is crossed out.

The order comes from the accessible name computation, a W3C standard. For an <input>, it and HTML-AAM give this order:

  1. aria-labelledby
  2. aria-label
  3. the <label>
  4. the title
  5. the placeholder
Real-life analogyWho signs for a parcel

At Leaf and Kettle, Asha signs for parcels. If she's out, Ravi signs. If he's out too, Meena does. The courier's log lists each person asked, and who signed. Nobody after the signer is asked at all.

In real life: Asha, then Ravi, then Meena
In HTML: aria-labelledby, then aria-label, then the <label>
In real life: The first one in the shop
In HTML: The first source with text
In real life: The courier's log
In HTML: The sources under Name in DevTools
In real life: "Not in" in the log
In HTML: Not specified

Try it. This box has four sources at once. Remove them one by one, and watch the name change:

Playground: which name wins?
A number box with four ways to get a namePop out in the code editor (opens in a new tab)HTML
<p id="words">Cups of tea</p><label for="cups">Cups</label><input id="cups" type="number" value="1" aria-labelledby="words" aria-label="How many cups" title="Number of cups">
ResultLoading…
What DevTools' Computed properties show for the box (placeholder rows left out)Text
Name: "Cups of tea"
  aria-labelledby: "Cups of tea"
  aria-label: "How many cups" (crossed out)
  From label (for= attribute): "Cups" (crossed out)
  title: "Number of cups" (crossed out)
Description: "Number of cups"
Try it yourself
aria-labelledby wins

Chrome tries aria-labelledby first, and it has text: Cups of tea. Every source after it is crossed out. The title, unused as a name, becomes the description.

Only line 3 changes. Open DevTools on the result to see the same rows.
  • Line 1 is a paragraph with the id words. Line 2 is a label for the box.
  • Line 3 is the box. With all four, it has aria-labelledby, aria-label, the label, and a title.

Each time, the first source with text wins. Look at the description, too. A title that isn't the name becomes the description. HTML-AAM says so. And with aria-label, the name is How many cups, not the visible Cups. Prefer the visible label, so what people see matches what they hear.

The order is the same inside a shadow root. It also explains the overview. Chrome looked on the box for each source, found none, and left it with no name.

Reading names in a test

DevTools reads the tree through the DevTools protocol. A test can do the same. This lesson's own test does. Here is the idea, in a Playwright test, after the page has loaded:

Reading a name and a description with the DevTools protocolJavaScript
const cdp = await page.context().newCDPSession(page);
const { nodes } = await cdp.send("Accessibility.getFullAXTree");
const box = nodes.find((node) => node.role?.value === "spinbutton");
console.log("Name:", box.name.value);
console.log("Description:", box.description?.value ?? "none");
  • Line 1 opens a CDP session, a direct line to Chrome's DevTools protocol. Playwright's docs say it works only in Chromium browsers.
  • Line 2 asks for the whole accessibility tree. Accessibility.getFullAXTree gives back a list of nodes, one per row. Accessibility.getPartialAXTree does the same for one element.
  • Line 3 finds the spinbutton.
  • Lines 4 and 5 print its name and description.

On the hint example, it prints Name: Cups and Description: Max 6 per order. Each node's name.sources holds the same list DevTools shows. Why it matters: a test like this catches a lost name before Leela does.

Describing the kit's quantity

The kit's tea-quantity has a - button, a number, and a + button. Since lesson 13.3, a page <label for> names its number. Now Leaf and Kettle wants a hint under it, like Each cup is 250 ml. The number has the name, so it gets the description too.

The quantity takes a new hint attribute. It shows the hint, and points the number at it, all inside its own shadow root. These are the new lines in tea-kit/tea-quantity.js:

The quantity's new linesJavaScript
    #hint { color: var(--tea-color-stone, dimgray); }

  <output id="number" part="number" aria-describedby="hint">1</output>
  <button id="more" part="button more" aria-label="One cup more">+</button>
  <small id="hint" part="hint" hidden></small>`;

  static observedAttributes = ["hint"];

  attributeChangedCallback(name, oldValue, newValue) {
    const hint = this.shadowRoot.getElementById("hint");
    hint.textContent = newValue ?? "";
    hint.hidden = !newValue;
  }
  • Line 1 colors the hint gray. It's line 10 of the file.
  • Line 3 gives the number aria-describedby="hint". Line 5 is the hint, a <small>, hidden until it has text. They're lines 20 and 22 of the file, in the same shadow root.
  • Line 7 watches the hint attribute. It's line 42.
  • Lines 9 to 13 run when the attribute changes. Line 11 writes its text into the hint, and line 12 shows the hint only when there is text. They're lines 61 to 65.

Here it is on a page, with two buttons that change the words:

The kit's quantity, labelled and describedPop out in the code editor (opens in a new tab)HTML
<link rel="stylesheet" href="tea-kit/tokens.css"><label for="cups">Cups</label><tea-quantity id="cups" hint="Each cup is 250 ml"></tea-quantity><p><button id="rename">Rename the label</button> <button id="change">Change the hint</button></p><script type="module" src="tea-kit/index.js"></script><script>  document.querySelector("#rename").addEventListener("click", () => {    document.querySelector("label").textContent = "Cups of tea";  });  document.querySelector("#change").addEventListener("click", () => {    document.querySelector("tea-quantity").setAttribute("hint", "Each cup is 300 ml");  });</script>
ResultLoading…
  • Line 2 is the label, and line 3 is the quantity, with hint="Each cup is 250 ml".
  • Lines 7 to 9 rename the label to Cups of tea. Lines 10 to 12 change the hint to Each cup is 300 ml.

Press the two buttons in turn, and check the number's row after each change:

The number's row in Chrome's tree, after each changeText
At the start:            status "Cups", description: "Each cup is 250 ml"
After Rename the label:  status "Cups of tea", description: "Each cup is 250 ml"
After Change the hint:   status "Cups of tea", description: "Each cup is 300 ml"

A status is the role Chrome gives an <output>. The name follows the label, and the description follows the attribute. Chrome reads the label each time, and the callback keeps the hint in step with its attribute. So neither goes stale.

The tea card's own quantity has no hint, so its hint stays empty and hidden. The card's tree is the same as with lesson 13.3's kit. The JSDoc and custom-elements.json list the new attribute and the hint part. This version of the kit is kitAfter134.

One quick check before the mix-ups. Which element gets the name?

Which element gets the name?
  • <tea-cups aria-label="Cups">, whose shadow root holds a number box
  • <tea-cups label="Cups">, which writes the words into a <label for="box"> beside its box
  • <tea-cups>Cups</tea-cups>, whose <label> wraps a <slot> and the box
  • A page <label for="cups"> and a form-associated <tea-cups id="cups"> that wraps a number box
  • A page <label for="cups"> and a plain <tea-cups id="cups">
  • A page <label>Cups <tea-cups></tea-cups></label> around a plain host
Try it yourself
0 of 6 correct

Sort each page: does the box inside get the name, only the host, or nothing?

Choose a category for every card. You can change an answer at any time; Reset clears them all.

Common mix-ups

  • "aria-label on the host names the input inside." No. It names the host. The input gets focus with no name.
  • "delegatesFocus passes the name on with focus." No. It moves focus only.
  • "A <label> can't label a custom element." No. A form-associated custom element is labelable. That old advice is from before form association.
  • "Form association names the input inside." No. The label names the host. Use it when the host is the control.
  • "A slotted label can't name anything in the shadow root." No. A <label> inside that wraps a slot and the box names the box with the slotted text.
  • "A broken aria-describedby shows an error." No. It fails without a word. Check the tree.
  • "The visible label always wins." No. aria-labelledby and aria-label come first.

Read each row across: what you write, which element gets it, and whether the box inside gets it.

Where does a name or description land?
What you writeWhich element gets itDoes the box inside get it?
aria-label on the host.The host, as a generic row.No. The box gets focus with no name.
A label attribute the component writes into a <label for> inside.The box inside.Yes. Both ids are in one shadow root.
Text the page slots into a <label> inside.The box inside.Yes. The label holds the slot and the box.
A page <label for> that points at a form-associated host.The host.No. The host has to be the control.
aria-describedby on the box, with a page id.Nothing.No. The id stays in the shadow root.
ariaDescribedByElements on the box, with a page element.The box inside.Yes. An element reference points out.

Practice exercises

Answer first, then use the hints. The pages run live. To check a name, open DevTools on the result and look at the Accessibility tab.

Exercise 1 · Warm-upCount the labels

This page has two labels with for="milk". The switch is the form-associated one from this lesson. What does line 6 print?

<label for="milk">Add milk</label><tea-toggle id="milk"></tea-toggle><label for="milk">(free)</label><script src="tea-toggle.js"></script><script>  console.log("Labels:", document.querySelector("tea-toggle").labels.length);</script>
ResultLoading…

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · Warm-upA label click on a switch with no tab stop

    This switch has no connectedCallback(), so nothing gives it a tab stop. Click Add milk in the result. What does the page print?

    <label for="milk">Add milk</label><tea-toggle id="milk"></tea-toggle><script src="tea-toggle.js"></script>
    ResultLoading…

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeTie the label inside to the box

      The component writes Cups into a label, but the box has no name. Fix line 5 of the tea-cups.js tab. What name does Chrome give the box now?

      <tea-cups label="Cups"></tea-cups><script src="tea-cups.js"></script>
      ResultLoading…

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · PracticeDescribe the box from inside

        The hint shows under the box, but a screen reader doesn't tie it to the box. Fix line 7 of the tea-cups.js tab. What description does the box get?

        <tea-cups></tea-cups><script src="tea-cups.js"></script>
        ResultLoading…

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeMake the switch labelable

          The page prints Control: none, and the switch has no name. Fix the tea-toggle.js tab with one line. What does line 6 print now?

          <label for="milk">Add milk</label><tea-toggle id="milk"></tea-toggle><script src="tea-toggle.js"></script><script>  const label = document.querySelector("label");  console.log("Control:", label.control?.localName ?? "none");</script>
          ResultLoading…

          Answer, then press Check. Spacing and letter case don’t matter.

            Exercise 6 · ChallengeDescribe the kit's quantity

            This page describes the quantity's host, so its number gets no description. Use the kit instead. What description does the number get?

            <link rel="stylesheet" href="tea-kit/tokens.css"><label for="cups">Cups</label><tea-quantity id="cups" aria-describedby="cups-hint"></tea-quantity><p id="cups-hint">Each cup is 250 ml</p><script type="module" src="tea-kit/index.js"></script>
            ResultLoading…

            Answer, then press Check. Spacing and letter case don’t matter.

              Check your understanding

              For each question, ask: which element has the role and focus, and does the name reach it?

              The labels and descriptions quiz · 8 questionsScore: first tries count
              1. Question 1 of 8A reader clicks the box in this page. Which row of Chrome's tree has the name Cups?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <tea-cups aria-label="Cups"></tea-cups>
                <script>
                  customElements.define("tea-cups", class extends HTMLElement {
                    constructor() {
                      super();
                      this.attachShadow({ mode: "open", delegatesFocus: true }).innerHTML = '<input type="number">';
                    }
                  });
                </script>

                Choose an answer to see the explanation.

              2. Question 2 of 8What does this page print?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <label for="milk">Add milk</label>
                <tea-toggle id="milk"></tea-toggle>
                <script>
                  let internals;
                  customElements.define("tea-toggle", class extends HTMLElement {
                    constructor() {
                      super();
                      internals = this.attachInternals();
                    }
                  });
                  try {
                    console.log(internals.labels.length);
                  } catch (error) {
                    console.log(error.name);
                  }
                </script>

                Choose an answer to see the explanation.

              3. Question 3 of 8In Chrome, you click the <label> of a form-associated switch that is in the Tab order. What happens?

                Choose an answer to see the explanation.

              4. Question 4 of 8A form-associated host wraps a number box and uses delegatesFocus. A page <label for> points at the host. Which element gets the name?

                Choose an answer to see the explanation.

              5. Question 5 of 8A box inside a shadow root has aria-describedby="hint". The element with that id is on the page. What description does the box get?

                Choose an answer to see the explanation.

              6. Question 6 of 8A box has both aria-describedby="hint" and aria-description="Ask about refills". Which text is its description?

                Choose an answer to see the explanation.

              7. Question 7 of 8A number box has aria-labelledby, aria-label, a <label for>, and a title. Which one names it?

                Choose an answer to see the explanation.

              8. Question 8 of 8Where does Chrome DevTools show where a name came from?

                Choose an answer to see the explanation.

              Key takeaways

              • Name the element that has the role and gets focus. Give it the description too.
              • aria-label or aria-describedby on a host lands on the host, not on the input inside. delegatesFocus moves focus, not names.
              • To name an input inside, use a <label> in the same shadow root, slotted text inside that label, or lesson 13.3's element references and Reference Target.
              • static formAssociated = true makes a custom element labelable. A page <label> then names the host, and internals.labels lists it.
              • In Chrome, a click on that label focuses the host, if it can take focus, and fires a click at it. Safari doesn't use the label as the name.
              • A description needs the same care: an id in one tree, an element reference that points out, or aria-description text.
              • Check the name and its sources in DevTools' Computed properties, or in a test with Accessibility.getFullAXTree.

              Remember the one-liner.
              The name and the description go where focus lands.

              Sources

              The facts on this page come from these standards, docs, and posts. Browser versions come from MDN's browser-compat-data:

              Coming next: Announcing changes. It tells screen reader users when something on the page changes, with live regions and ariaNotify().

              CompleteFrontend Clear concepts. Working examples.