completefrontend
Open lab My progress
THE WEB COMPONENTS FIELD GUIDE

ARIA references across shadow roots

Learn why aria-labelledby fails across shadow DOM, then fix it: point out with ariaLabelledByElements, and point in with Reference Target.

By the end, you can
  • 01
    Spot a broken referenceTell when a for or aria-labelledby id crosses a shadow root, and see the missing name in Chrome's accessibility tree.
  • 02
    Point out of a shadow rootSet element references like ariaLabelledByElements, and know they reach out to the page but never in.
  • 03
    Point in, or work around itUse Reference Target where the browser has it, and fall back to fixes that work in every browser today.

Why doesn't aria-labelledby work across shadow DOM?

Leaf and Kettle's order page gets a box for how many cups. Ravi builds it as a component, <tea-cups>, with a number box in its shadow root. In front of it he writes a label, <label for="cups">Cups</label>, as he would for any input.

Leela orders with a screen reader. She presses Tab and lands in the box. Her screen reader tells her it's a box for a number, but never what it's for. It never says Cups. A click on the word Cups does nothing either. The label names the right id. So why can't it reach the box?

An id reference, like for or aria-labelledby, only looks inside its own tree. The page can't point at an id inside a shadow root, and a shadow root can't point at an id on the page. Two newer tools cross the line. Element references, like ariaLabelledByElements, point out of a shadow root. Reference Target lets a host pass references on to an element inside.

Here is Ravi's page:

A page label for a number box inside a componentPop 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 4 attaches a shadow root. Line 5 puts a number box in it, with the id input.
  • On the page, line 1 is the label. Its for names cups.
  • Line 2 is the component, with the id cups.

Lesson 13.1 met Chrome's accessibility tree: the list of what each thing on the page is, which screen readers read. Here it is for Ravi's page, shortened the same way:

Chrome's accessibility tree for the label and the boxText
StaticText "Cups"
spinbutton (no name)

Line 1 is the label's text, on its own. Line 2 is the number box. Chrome calls a number box a spinbutton, because its value goes up and down. It has no name. In the result, click Cups: the cursor doesn't move.

What went wrong? The label's for found the element with the id cups. That's the host, <tea-cups>. But a label can only name a few kinds of elements, like an <input>, a <button>, or an <output>. The HTML Standard calls them labelable. A plain custom element isn't one. So label.control, the element a label names, is null. And the label never looks inside the host.

Two windows. Left, titled page: a card <label for="cups">, with an arrow down to a card <tea-cups id="cups">, labelled stops at the host. Right, titled shadow root: a card <input id="input">, labelled out of reach.page<label for="cups"><tea-cups id="cups">stops at the hostshadow root<input id="input">out of reach
The label's for looks for cups in the page's tree. It finds the tea-cups host and stops. The number box lives in the shadow root, a tree of its own, so no id on the page reaches it.
Real-life analogyCalling a name in the front room

Meena calls out "Asha, your tea!" in Leaf and Kettle's front room. Everyone in the front room hears her. Asha is in the back room, behind a closed door. She never hears it. Meena called the right name, in the wrong room.

In real life: Meena calling out "Asha"
In HTML: An id reference, like for="cups"
In real life: The front room
In HTML: The page's tree
In real life: The back room, behind a closed door
In HTML: The shadow root
In real life: Asha, who never hears
In HTML: The number box inside
The main idea

Ids only work inside one tree. for and aria-labelledby can't cross a shadow root, in either direction. Element references can point out of one. Reference Target lets the page point at a host and reach one element inside.

Why it matters: nothing throws, and nothing warns. The page looks fine. Only the accessibility tree shows the missing name. This lesson is about references that cross a shadow root. The next lesson, Labels and descriptions, covers naming a component in general. A form-associated custom element is labelable too. Stage 6, Forms, covers those, from lesson 16.1.

Which attributes use id references?

An id reference is an attribute whose value is another element's id. Many HTML and ARIA attributes work this way:

  • for on a <label>: the control the label names.
  • aria-labelledby: the elements whose text names this one.
  • aria-describedby: the elements whose text describes it, like a hint.
  • aria-controls: what this control changes, like a tab's panel.
  • aria-activedescendant: the option that's active in a list.
  • list on an <input>: its list of suggestions.
  • popovertarget and commandfor on a <button>: the popover or dialog it opens.

They all follow one rule. MDN says they "can only refer to other elements that are in the same DOM or Shadow DOM". Lesson 6.3 showed why: each shadow root keeps its own ids, and its getElementById() stops at the root. Lesson 10.1 kept the tabs and their panels in the page for this reason, so each aria-controls id stayed in one tree. Inside one tree, ids work well. In lesson 13.1, the kit card's Add button got its name from aria-labelledby="add name", with both ids in the card's own shadow root.

The rule works both ways. Here the number box points out at the page instead:

An aria-labelledby id inside a shadow root, pointing at the pagePop out in the code editor (opens in a new tab)HTML
<p id="cups-label">Cups</p><tea-cups></tea-cups><script src="tea-cups.js"></script><script>  const input = document.querySelector("tea-cups").shadowRoot.querySelector("input");  console.log("aria-labelledby:", input.getAttribute("aria-labelledby"));  console.log("Found:", input.ariaLabelledByElements.length);</script>
ResultLoading…
  • In the tea-cups.js tab, line 5 gives the number box aria-labelledby="cups-label".
  • On the page, line 1 is the text with that id.
  • Line 6 prints the attribute. Line 7 prints how many elements it found.

It prints aria-labelledby: cups-label, then Found: 0. Line 7 uses ariaLabelledByElements, a property every element has. It gives back the elements that aria-labelledby found. The id is looked up in the box's own tree, the shadow root. The paragraph with that id is on the page, so nothing is found.

Chrome's accessibility tree for the id that points outText
paragraph
  StaticText "Cups"
spinbutton (no name)

The box still has no name. Why it matters: a component can't borrow the page's words with an id, and the page can't reach the component's parts with one.

What is ariaLabelledByElements?

An id is a name written down as text, and the browser has to look it up. Element reflection skips the lookup. Each ARIA attribute that holds ids has a matching property that holds elements. You hand over the element itself, like pointing at a person instead of calling out their name.

Here it is on a page with no shadow root at all:

Naming a number box with an element, not an idPop out in the code editor (opens in a new tab)HTML
<p>Cups</p><input type="number" value="1" min="1"><script>  const label = document.querySelector("p");  const input = document.querySelector("input");  input.ariaLabelledByElements = [label];  console.log("Elements:", input.ariaLabelledByElements.length);  console.log("Attribute:", JSON.stringify(input.getAttribute("aria-labelledby")));</script>
ResultLoading…
  • Line 1 is the text. It has no id, and it doesn't need one.
  • Line 6 sets ariaLabelledByElements to an array that holds the paragraph. It's always an array, even for one element.
  • Line 7 prints Elements: 1.
  • Line 8 prints the attribute: Attribute: "", an empty string.
Chrome's accessibility tree for the element referenceText
paragraph
  StaticText "Cups"
spinbutton "Cups"

The box is named Cups. Why is the attribute empty? MDN explains: setting the property clears the attribute. The elements may have no ids, so the browser can't write them down as text. From then on, the property and the attribute no longer match. Set the attribute again, and they match again.

There's one such property for each ARIA attribute that holds ids:

  • ariaLabelledByElements, for aria-labelledby.
  • ariaDescribedByElements, for aria-describedby.
  • ariaControlsElements, for aria-controls.
  • ariaActiveDescendantElement, for aria-activedescendant. It holds one element, not a list.
  • ariaDetailsElements, for aria-details.
  • ariaErrorMessageElements, for aria-errormessage.
  • ariaFlowToElements, for aria-flowto.
  • ariaOwnsElements, for aria-owns. Firefox and Safari have it. Chrome doesn't.

The other seven work in Chrome 135, Firefox 136, and Safari 16.4 and later.

ElementInternals has the same properties. Lesson 13.2 set role and ariaChecked on a component's internals, as defaults for its tag. A component can set element references there in the same way.

Why it matters: an element reference needs no id. That's what lets it cross a shadow root, in one direction.

How do you point out of a shadow root?

An element inside a shadow root can point out at the page. The page can't point in. Try both, and a third case inside one root:

Playground: which way does the reference point?
A number box and a hint inside tea-cups, and a heading and a button on the pagePop out in the code editor (opens in a new tab)HTML
<h2>Cups</h2><tea-cups></tea-cups><button>Order</button><script src="tea-cups.js"></script><script>  const [heading, button] = document.querySelectorAll("h2, button");  const [input, hint] = document.querySelector("tea-cups").shadowRoot.querySelectorAll("input, p");  input.ariaLabelledByElements = [heading];  console.log("Found:", input.ariaLabelledByElements.length);</script>
ResultLoading…
Chrome's accessibility tree for this page (shortened)Text
heading "Cups"
spinbutton "Cups"
paragraph
  StaticText "Up to 5 cups"
button "Order"
Try it yourself
Pointing out works

The number box sits inside the shadow root and points out at the page's heading. The page is a tree around it, so the reference counts: Found: 1, and the box is named Cups.

Only lines 8 and 9 of the page change. The tea-cups.js tab is the same each time.
  • In the tea-cups.js tab, line 5 puts a number box and a hint, Up to 5 cups, in the shadow root.
  • On the page, line 1 is a heading and line 3 is a button. Both are in the page's tree.
  • Line 6 finds the heading and the button. Line 7 reaches into the open shadow root for the box and the hint, as lesson 6.2 showed.
  • Line 8 sets the reference. Line 9 prints how many elements it found.

Out, to the page: Found: 1, and the box is named Cups. In, from the page: Found: 0, and the button gets no description. Inside one root: Found: 1, and the box gets the description Up to 5 cups.

The rule comes from the HTML Standard. Its steps read like a recipe, and "continue" means skip this one and go on to the next. Here is the check it runs on each element you set:

The scope check for element references, shortened from the HTML StandardText
For each attrElement in the explicitly set attr-elements:
  If attrElement is not a descendant of any of element's shadow-including ancestors, then continue.
  Append attrElement to elements.

In plain words, line by line:

  1. Look at each element you set, one at a time.
  2. The element's shadow-including ancestors sit in the trees around it: its own tree, its host's tree, and so on out to the page. If the target isn't in one of those trees, skip it.
  3. Otherwise, keep it.

So a target counts when it's in the same tree, or in a tree around it. MDN puts it as "the same DOM as the referencing element, or a parent DOM". A tree nested deeper, like a component's shadow root seen from the page, is out of scope.

A window titled page holds a card for the heading Cups, a card for the Order button, and a smaller window titled shadow root with cards <input> and <p id="hint">. An arrow goes from <input> out to the heading, labelled out: Found: 1. An arrow goes from the button in to <p id="hint">, labelled in: Found: 0.page<h2>Cups</h2><button>Order</button>shadow root<input><p id="hint">out: Found: 1in: Found: 0
An element reference counts when it points at its own tree or a tree around it. The number box points out at the page's heading: Found: 1. The page's button points in at the hint: Found: 0.
Real-life analogyA badge that points at the board out front

Leaf and Kettle's kitchen staff wear badges. Meena's says "Today's teas: see the board out front". Anyone can read the board, so her badge works. A customer can't write "see the recipe card in the kitchen" on an order slip. Nobody out front can reach that card.

In real life: Meena, in the kitchen
In HTML: The number box inside the shadow root
In real life: The board out front
In HTML: The heading on the page
In real life: "See the board out front" on her badge
In HTML: input.ariaLabelledByElements = [heading]
In real life: A slip that says "see the card in the kitchen"
In HTML: The page's button pointing at the hint: Found: 0

The browser checks the rule each time you read the property. Move the heading into another shadow root, and it drops out of the list. Move it back, and it returns, as MDN says.

Why it matters: a component can borrow the page's words for its own controls, in every current browser. The page can't do the reverse. That's why pointing in needs something else.

What is Reference Target in shadow DOM?

Pointing in is the hard direction. Reference Target handles it. The component names one element in its shadow root as its target. Then every id reference that points at the host goes on to that element instead. MDN calls it the "effective target" of references to the host from outside.

For the number box, it's one more option for attachShadow(): referenceTarget: "input". Try it, then try it with a wrong id:

Playground: what does the reference target name?
tea-cups with a reference target, and the page's labelPop 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><script>  console.log("Reference Target:", "referenceTarget" in ShadowRoot.prototype);  const label = document.querySelector("label");  console.log("Label's control:", label.control?.localName ?? "none");</script>
ResultLoading…
Chrome's accessibility tree for this page (shortened)Text
StaticText "Cups"
spinbutton "Cups"
Try it yourself
The label reaches the number box

The shadow root passes every reference to its host on to the element with id input. So the label names the box Cups, and a click on Cups puts the cursor in it. label.control still gives back only the host, tea-cups.

The page stays the same. Only line 4 of the tea-cups.js tab changes. Click Cups in the result to see where the cursor goes.
  • In the tea-cups.js tab, line 4 attaches the shadow root with referenceTarget: "input". The value is the id of the number box on line 5.
  • On the page, line 1 is the overview's label. Its for still names the host's id, cups.
  • Line 5 checks whether the browser has Reference Target. Line 7 prints the label's control.

With "input", the page prints Reference Target: true and Label's control: tea-cups. The box is named Cups. Click Cups, and the cursor jumps into the box. Look at the control, though. It's the host, not the input. The label reaches the box, but the page's scripts still can't get hold of it. The shadow root stays private.

With the wrong id, "cups", nothing inside has that id. The reference doesn't fall back to the host: it reaches nothing, as Alice Boxhall's explainer says. With no reference target, you're back to the overview.

A card <label for="cups">, labelled points at the host, with an arrow to a card <tea-cups id="cups">. From the host, an arrow labelled referenceTarget: "input" goes down into a window titled shadow root, to a card <input id="input">, labelled spinbutton "Cups".<label for="cups">points at the host<tea-cups id="cups">shadow root<input id="input">spinbutton "Cups"referenceTarget: "input"
The label points at the host, the only element it can see. The shadow root's reference target passes that reference on to the number box, so Chrome names the box Cups.
Real-life analogyA parcel for the tea shop

A parcel arrives addressed to Leaf and Kettle, with no person's name on it. The front desk has a note: orders about cups go to Meena. So the desk passes the parcel to her. The sender never learns who opened it. If the note named someone who doesn't work there, the parcel would go nowhere.

In real life: The address: Leaf and Kettle
In HTML: for="cups", the host's id
In real life: The front desk's note: cups orders go to Meena
In HTML: referenceTarget: "input"
In real life: Meena
In HTML: The number box inside
In real life: The sender never learns who opened it
In HTML: label.control gives back the host
In real life: A note that names someone who doesn't work there
In HTML: A reference target with a wrong id

You can set a reference target in three ways:

  • the referenceTarget option of attachShadow(), as above;
  • the shadow root's referenceTarget property, as in this.shadowRoot.referenceTarget = "input";
  • the shadowrootreferencetarget attribute on a shadow root written in HTML.

Lesson 7.1 showed a <template> with shadowrootmode that becomes a shadow root. Add one attribute, and no script is needed:

A reference target on a shadow root written in HTMLPop out in the code editor (opens in a new tab)HTML
<label for="cups">Cups</label><tea-cups id="cups">  <template shadowrootmode="open" shadowrootreferencetarget="input">    <input id="input" type="number" value="1" min="1">  </template></tea-cups>
ResultLoading…

Line 3 makes the shadow root and names input as its target. Line 4 is the number box. Chrome's tree is the same as before: spinbutton "Cups".

Which references go through? Chrome 152's release notes name <label for>, aria-labelledby, popovertarget, and commandfor. They say all id references that point at the host are passed on. Element references that point at the host go through too. Exercise 6 opens a popover inside a component from a button on the page.

Reference Target only passes on references that point at the host. It doesn't move the host's own attributes inside. An aria-describedby written on <tea-cups> still describes the host, not the box. Alice Boxhall lists that idea, forwarding attributes, as a separate problem.

Which browsers support Reference Target?

Chrome and Edge have it on by default, from version 152. Firefox and Safari have it only behind a setting. Here is what each browser needs:

  • Chrome or Edge: version 152 or later. Nothing to turn on.
  • Firefox: version 144 or later, with dom.shadowdom.referenceTarget.enabled set to true in about:config.
  • Safari: version 26 or later, with its referenceTarget feature flag turned on.

Your own browser answers on line 5 of the playground's page. The standard text is still in open pull requests, for HTML and for DOM. The tracking issue still lists open questions. Mozilla's position is positive, and WebKit hasn't given one. So don't count on it alone. The next section shows fixes that work everywhere.

What works in every browser today?

Three kinds of fix work in Chrome, Firefox, and Safari right now. Here they are, best first:

  1. Point out of the shadow root with an element reference, as two sections ago.
  2. Keep both ends of the reference in one tree.
  3. As a last resort, copy the words into aria-label.

Chrome's guide to accessible web components gives the same advice. Its fallback, when nothing else fits, is aria-label text.

Keeping both ends in one tree

One way lets the page write both ends. The component only arranges them, with slots. Lesson 8.1 showed that slotted elements stay the page's children. So their ids stay in the page's tree:

A label and a number box the page writes, shown through slotsPop out in the code editor (opens in a new tab)HTML
<tea-field>  <label slot="label" for="cups">Cups</label>  <input id="cups" type="number" value="1" min="1"></tea-field><script src="tea-field.js"></script>
ResultLoading…
  • On the page, line 2 is the label, sent to the slot named label. Line 3 is the number box, with the id cups.
  • In the tea-field.js tab, line 5 lays them out: the label in a paragraph, then the box.
Chrome's accessibility tree for the slotted label and boxText
paragraph
  StaticText "Cups"
spinbutton "Cups"

Chrome names the box Cups, and a click on Cups puts the cursor in the box. The for and the id are both in the page's tree. The cost is that the page writes the input itself.

The other way keeps both ends inside. The page passes the words in a plain attribute, and the component writes its own label:

tea-cups with its own label insidePop out in the code editor (opens in a new tab)HTML
<tea-cups label="Cups"></tea-cups><script src="tea-cups.js"></script>
ResultLoading…

On the page, line 1 passes label="Cups". In the tea-cups.js tab, line 5 has a <label for="input"> beside the box, in the same shadow root. Line 8 copies the words into it. Chrome names the box Cups. In 2017, Rob Dodson's A11ycasts episode showed this fix and an aria-label copy, years before element references or Reference Target.

Copying text into aria-label

The last resort copies the page's words into the inner control's aria-label. It works, but the copy doesn't follow the label. Rename the label and compare:

Playground: copy the words, or point at the label?
tea-cups names its number box from the page's labelPop out in the code editor (opens in a new tab)HTML
<label for="cups">Cups</label><tea-cups id="cups"></tea-cups><button>Rename the label</button><script src="tea-cups.js"></script><script>  document.querySelector("button").addEventListener("click", () => {    document.querySelector("label").textContent = "Cups of tea";  });</script>
ResultLoading…
The number box's row in Chrome's tree, before and after a clickText
Before the click: spinbutton "Cups"
After the click:  spinbutton "Cups"
Try it yourself
The copy goes stale

Line 10 copies the label's text into aria-label once. Press Rename the label: the page shows Cups of tea, but the box is still named Cups. Nothing tells the copy to change.

The page stays the same. Only line 10 of the tea-cups.js tab changes. Press Rename the label in the result.
  • In the tea-cups.js tab, line 8 finds the page's label for the host's id. Line 9 finds the number box.
  • Line 10 copies the label's text, or points at the label.
  • On the page, lines 6 to 8 rename the label to Cups of tea when you press the button.

The copy keeps the old name, Cups. The element reference follows the label to Cups of tea. A copy has other costs too, as Nolan Lawson lists. You have to watch the label for changes. And aria-controls or aria-activedescendant have no text version to copy into. So copy only when nothing else fits.

Real-life analogyA photocopy of the price list

Ravi photocopies Leaf and Kettle's price list and tapes it in the window. Next week, chai costs more on the list inside. The photocopy still shows the old price, until someone makes a new copy.

In real life: The price list inside
In HTML: The page's <label>
In real life: The photocopy in the window
In HTML: The words copied into aria-label
In real life: A new price on the list inside
In HTML: The label renamed to Cups of tea
In real life: A sign saying "see the list inside"
In HTML: ariaLabelledByElements = [label]

Why it matters: pick the fix by direction. To point out, use an element reference. To point in, keep both ends in one tree, and add Reference Target for the browsers that have it.

Labelling the kit's quantity from the page

The kit's tea-quantity has a - button, a number, and a + button. The number is an <output>. In lesson 13.1's tree, it had no name. A page should be able to name it with a plain label: <label for="cups">Cups</label> and <tea-quantity id="cups">. An <output> is labelable, like an input.

So the quantity uses Reference Target where the browser has it, and points out where it doesn't. These are the new lines in tea-kit/tea-quantity.js:

The quantity's new linesJavaScript
const hasReferenceTarget = "referenceTarget" in ShadowRoot.prototype;

  <output id="number" part="number">1</output>

    if (hasReferenceTarget) this.shadowRoot.referenceTarget = "number";

  connectedCallback() {
    if (hasReferenceTarget || !this.id) return;
    const label = this.getRootNode().querySelector(`label[for="${CSS.escape(this.id)}"]`);
    if (label) this.shadowRoot.getElementById("number").ariaLabelledByElements = [label];
  }
  • Line 1 checks once whether shadow roots have referenceTarget. It's line 3 of the file.
  • Line 3 gives the number the id number.
  • Line 5 makes the number the reference target, where the browser has it. It's line 44 of the file.
  • Lines 7 to 11 run when the quantity joins the page. Line 8 stops early if Reference Target does the job, or if the quantity has no id.
  • Line 9 looks for a <label for> with the quantity's id. getRootNode() gives the quantity's own tree: the page, or a shadow root around it. CSS.escape() makes any id safe inside a selector.
  • Line 10 points the number out at that label.
Playground: today's Chrome, or a browser without Reference Target?
A page label for the kit's quantityPop out in the code editor (opens in a new tab)HTML
<link rel="stylesheet" href="tea-kit/tokens.css"><script src="browser.js"></script><label for="cups">Cups</label><tea-quantity id="cups"></tea-quantity><script type="module" src="tea-kit/index.js"></script><script type="module">  const root = document.querySelector("tea-quantity").shadowRoot;  console.log("Path:", root.referenceTarget ? "Reference Target" : "ariaLabelledByElements");</script>
ResultLoading…
Chrome's accessibility tree for this page (shortened)Text
StaticText "Cups"
button "One cup less"
status "Cups"
button "One cup more"
Try it yourself
Reference Target forwards the label

Chrome has Reference Target, so line 44 of tea-kit/tea-quantity.js makes the number the root's reference target. The page's label reaches it, and the page prints Path: Reference Target.

The page and the kit stay the same. Only the browser.js tab changes. The second choice is Chrome with Reference Target removed.
  • On the page, line 2 loads browser.js before the kit. Line 3 is the label, and line 4 is the quantity.
  • Line 8 prints which way the label reached the number.

Both choices give the same tree. Line 3 is status "Cups", between the two buttons. A status is the role Chrome gives an <output>. With lesson 13.2's kit, that row had no name.

In a browser without Reference Target, the label has to be in the tree when the quantity joins it. So the JSDoc says to write the label first. The tea card's own quantity has no id, so nothing changes there: the card's whole tree is the same. The JSDoc and custom-elements.json say how to label the quantity. This version of the kit is kitAfter133.

One quick check before the mix-ups. Does each reference reach its target?

Does the reference reach its target?
  • An input inside a shadow root sets ariaLabelledByElements to a heading on the page
  • A <label for> and its <input>, both written by the page and slotted into a component
  • A page <label for="cups"> and <tea-cups id="cups">, whose root has referenceTarget: "input"
  • A page <button popovertarget="menu"> and <tea-menu id="menu">, whose root has referenceTarget: "list"
  • An input inside a shadow root with aria-labelledby="cups-label", an id on the page
  • A page button sets ariaDescribedByElements to a hint inside a component
  • A page <label for="input"> that names the id of an input inside a shadow root
Try it yourself
0 of 7 correct

Sort each one: does it work in every current browser, only with Reference Target, or never?

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

Common mix-ups

  • "Cross-root ARIA is solved." No. Pointing out works in every current browser. Pointing in needs Reference Target, which only Chrome and Edge turn on by default. Its standard text is still in open pull requests.
  • "ARIA can never cross shadow roots." No. Element references cross outward in Chrome 135, Firefox 136, and Safari 16.4. Reference Target crosses inward in Chrome and Edge 152.
  • "An element reference can point at any element." No. Only at its own tree or a tree around it. Pointed into a nested shadow root, the list comes back empty.
  • "Setting ariaLabelledByElements writes ids into aria-labelledby." No. It empties the attribute. The elements don't need ids.
  • "Reference Target hands the inner element to the page." No. label.control gives back the host. The label reaches the box, but scripts still can't.
  • "A wrong reference target falls back to the host." No. It reaches nothing.
  • "Reference Target moves the host's own ARIA attributes inside." No. It only passes on references that point at the host.
  • "Copying the label into aria-label is just as good." No. The copy goes stale when the label changes.

Read each row across: the same question for ids, element references, and Reference Target.

Which way can each one point?
QuestionAn id, like for or aria-labelledbyAn element reference, like ariaLabelledByElementsReference Target
Can something inside a shadow root point out to the page?No. The id is looked up in the shadow root only.Yes. The page is a tree around it.It doesn't point out. It passes on references that point at the host.
Can the page point at something inside?No. The page's id finds the host at most.No. The list comes back empty.Yes. The page points at the host, and the host passes it on to one element.
Does the target need an id?Yes. That's how it's found.No. You hand over the element itself.The inner target does. The host needs one too, for an id like for.
Which browsers have it?Every browser, inside one tree.Chrome 135, Firefox 136, and Safari 16.4 and later.Chrome and Edge 152 and later. Firefox and Safari only behind a setting.

Practice exercises

Answer first, then use the hints. The pages run live. Exercises 4 to 6 need Reference Target, so try them in Chrome or Edge 152 or later. To check a name, open DevTools on the result and look at the Accessibility tab.

Exercise 1 · Warm-upPredict the label's control

The number box inside has the id cups, the id the label names. The host has no id. What does line 6 print?

<label for="cups">Cups</label><tea-cups></tea-cups><script src="tea-cups.js"></script><script>  const label = document.querySelector("label");  console.log("Label's control:", label.control?.localName ?? "none");</script>
ResultLoading…

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

    Exercise 2 · Warm-upPredict a reference that points in

    The page's input points at text inside tea-name's shadow root. What does line 8 print?

    <tea-name></tea-name><input type="text"><script src="tea-name.js"></script><script>  const span = document.querySelector("tea-name").shadowRoot.querySelector("span");  const input = document.querySelector("input");  input.ariaLabelledByElements = [span];  console.log("Found:", input.ariaLabelledByElements.length);</script>
    ResultLoading…

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

      Exercise 3 · PracticeDescribe the box from the page

      Line 7 gives the number box an aria-describedby id, which can't cross the shadow root. Change line 7 so the hint describes the box. What description does Chrome's tree show?

      <p id="hint">Up to 5 cups</p><tea-cups></tea-cups><script src="tea-cups.js"></script><script>  const hint = document.querySelector("#hint");  const input = document.querySelector("tea-cups").shadowRoot.querySelector("input");  input.setAttribute("aria-describedby", "hint");</script>
      ResultLoading…

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

        Exercise 4 · PracticeFix the label's for

        The component has a reference target, but the box still has no name. Fix line 1 of the page. What should for say?

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

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

          Exercise 5 · ChallengeA reference target written in HTML

          This shadow root is written in HTML. Add its reference target on line 3, so the label reaches the box. Then click Cups in the result. What does the page print?

          <label for="cups">Cups</label><tea-cups id="cups">  <template shadowrootmode="open">    <input id="input" type="number" value="1" min="1">  </template></tea-cups><script>  const cups = document.querySelector("tea-cups");  cups.addEventListener("focusin", () => console.log("The box has focus"));</script>
          ResultLoading…

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

            Exercise 6 · ChallengeOpen a popover inside a component

            The page's Teas button should open the list inside tea-menu. Fix line 4 of the tea-menu.js tab. Then click Teas. What does the page print?

            <button popovertarget="menu">Teas</button><tea-menu id="menu"></tea-menu><script src="tea-menu.js"></script>
            ResultLoading…

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

              Check your understanding

              For each question, ask: which tree is the reference in, and which way does it point?

              The ARIA references across shadow roots quiz · 8 questionsScore: first tries count
              1. Question 1 of 8What does this page print?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <p id="tea">Green tea</p>
                <tea-name></tea-name>
                <script>
                  customElements.define("tea-name", class extends HTMLElement {
                    constructor() {
                      super();
                      this.attachShadow({ mode: "open" }).innerHTML = '<input aria-labelledby="tea">';
                    }
                  });
                  const input = document.querySelector("tea-name").shadowRoot.querySelector("input");
                  console.log(input.ariaLabelledByElements.length);
                </script>

                Choose an answer to see the explanation.

              2. Question 2 of 8You set input.ariaLabelledByElements = [label]. What does input.getAttribute("aria-labelledby") give back now?

                Choose an answer to see the explanation.

              3. Question 3 of 8A page button sets ariaDescribedByElements to a hint inside a component's shadow root. What does reading the property back give?

                Choose an answer to see the explanation.

              4. Question 4 of 8Which of these element references does Chrome not have?

                Choose an answer to see the explanation.

              5. Question 5 of 8In this page, what does label.control.localName print?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <label for="cups">Cups</label>
                <tea-cups id="cups"></tea-cups>
                <script>
                  customElements.define("tea-cups", class extends HTMLElement {
                    constructor() {
                      super();
                      this.attachShadow({ mode: "open", referenceTarget: "input" }).innerHTML = '<input id="input">';
                    }
                  });
                  console.log(document.querySelector("label").control.localName);
                </script>

                Choose an answer to see the explanation.

              6. Question 6 of 8A shadow root's referenceTarget names an id that no element inside has. What does a page label that points at the host name?

                Choose an answer to see the explanation.

              7. Question 7 of 8Which fix names an input inside a shadow root from the page's text in Chrome, Firefox, and Safari, with no settings?

                Choose an answer to see the explanation.

              8. Question 8 of 8Is cross-root ARIA solved?

                Choose an answer to see the explanation.

              Key takeaways

              • An id reference, like for or aria-labelledby, only looks in its own tree. It can't cross a shadow root either way, and nothing warns you.
              • Element references, like ariaLabelledByElements, take elements instead of ids. Setting one empties the attribute.
              • An element reference counts in its own tree and every tree around it. Out of a shadow root works. Into one, the list is empty.
              • Chrome has every element reference except ariaOwnsElements.
              • Reference Target lets a host pass references on to one element inside, with referenceTarget or shadowrootreferencetarget. label.control still gives back the host.
              • Reference Target is on by default only in Chrome and Edge 152 and later. Firefox and Safari keep it behind a setting.
              • In every browser today: point out, keep both ends in one tree, or, last, copy the words into aria-label.

              Remember the one-liner.
              Ids stay in their own tree. Element references point out, and Reference Target lets a host pass references in.

              Sources

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

              Coming next: Labels and descriptions. It gives a component a name and a description that reach the right element.

              CompleteFrontend Clear concepts. Working examples.