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.
- 01Spot a broken referenceTell when a
fororaria-labelledbyid crosses a shadow root, and see the missing name in Chrome's accessibility tree. - 02Point out of a shadow rootSet element references like
ariaLabelledByElements, and know they reach out to the page but never in. - 03Point 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:
<label for="cups">Cups</label><tea-cups id="cups"></tea-cups><script src="tea-cups.js"></script>- In the
tea-cups.jstab, line 4 attaches a shadow root. Line 5 puts a number box in it, with the idinput. - On the page, line 1 is the label. Its
fornamescups. - 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:
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.
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.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
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:
foron 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.liston an<input>: its list of suggestions.popovertargetandcommandforon 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:
<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>- In the
tea-cups.jstab, line 5 gives the number boxaria-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.
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:
<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>- Line 1 is the text. It has no id, and it doesn't need one.
- Line 6 sets
ariaLabelledByElementsto 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.
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, foraria-labelledby.ariaDescribedByElements, foraria-describedby.ariaControlsElements, foraria-controls.ariaActiveDescendantElement, foraria-activedescendant. It holds one element, not a list.ariaDetailsElements, foraria-details.ariaErrorMessageElements, foraria-errormessage.ariaFlowToElements, foraria-flowto.ariaOwnsElements, foraria-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:
<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>heading "Cups"
spinbutton "Cups"
paragraph
StaticText "Up to 5 cups"
button "Order"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.
- In the
tea-cups.jstab, 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:
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:
- Look at each element you set, one at a time.
- 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.
- 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.
Found: 1. The page's button points in at the hint: Found: 0.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:
<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>StaticText "Cups"
spinbutton "Cups"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.
- In the
tea-cups.jstab, line 4 attaches the shadow root withreferenceTarget: "input". The value is the id of the number box on line 5. - On the page, line 1 is the overview's label. Its
forstill 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 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.controlgives 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
referenceTargetoption ofattachShadow(), as above; - the shadow root's
referenceTargetproperty, as inthis.shadowRoot.referenceTarget = "input"; - the
shadowrootreferencetargetattribute 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:
<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>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.enabledset to true inabout:config. - Safari: version 26 or later, with its
referenceTargetfeature 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:
- Point out of the shadow root with an element reference, as two sections ago.
- Keep both ends of the reference in one tree.
- 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:
<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>- On the page, line 2 is the label, sent to the slot named
label. Line 3 is the number box, with the idcups. - In the
tea-field.jstab, line 5 lays them out: the label in a paragraph, then the box.
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 label="Cups"></tea-cups><script src="tea-cups.js"></script>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:
<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>Before the click: spinbutton "Cups"
After the click: spinbutton "Cups"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.
- In the
tea-cups.jstab, 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.
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:
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.
<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>StaticText "Cups"
button "One cup less"
status "Cups"
button "One cup more"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.
- On the page, line 2 loads
browser.jsbefore 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?
- An input inside a shadow root sets
ariaLabelledByElementsto 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 hasreferenceTarget: "input" - A page
<button popovertarget="menu">and<tea-menu id="menu">, whose root hasreferenceTarget: "list" - An input inside a shadow root with
aria-labelledby="cups-label", an id on the page - A page button sets
ariaDescribedByElementsto a hint inside a component - A page
<label for="input">that names the id of an input inside a shadow root
Sort each one: does it work in every current browser, only with Reference Target, or never?
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
ariaLabelledByElementswrites ids intoaria-labelledby." No. It empties the attribute. The elements don't need ids. - "Reference Target hands the inner element to the page." No.
label.controlgives 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-labelis 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.
| Question | An id, like for or aria-labelledby | An element reference, like ariaLabelledByElements | Reference 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.
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>Label's control: none. The label looks for cups in the page's tree. The box with that id lives in the shadow root, a tree of its own, so the label names nothing. The same id in another tree doesn't help.
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>Found: 0. The page's input points in at a span inside tea-name's shadow root. That tree is nested inside the input's tree, so the list comes back empty, and the input gets no name.
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><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.ariaDescribedByElements = [hint];</script>Line 7 of the page now hands over the hint element itself. The box points out at a tree around it, so the reference counts, and Chrome's row is spinbutton (no name), description: "Up to 5 cups". The tea-cups.js tab stays the same.
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><label for="cups">Cups</label><tea-cups id="cups"></tea-cups><script src="tea-cups.js"></script>cups. The label looks in the page's tree, where only the host's id is. The host's reference target then passes it on to input. Line 1 of the page now says for="cups", and the box is named Cups. The tea-cups.js tab stays the same.
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><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><script> const cups = document.querySelector("tea-cups"); cups.addEventListener("focusin", () => console.log("The box has focus"));</script>The box has focus. Line 3 now names input as the reference target, so the label reaches the box. A click on Cups moves focus into it, and the focusin event reaches the listener on line 9.
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><button popovertarget="menu">Teas</button><tea-menu id="menu"></tea-menu><script src="tea-menu.js"></script>Menu: open. The page stays the same. In the tea-menu.js tab, line 4 now names list as the reference target. The button's popovertarget points at the host, the host passes it on to the popover, and line 7 of the tab prints Menu: open.
Check your understanding
For each question, ask: which tree is the reference in, and which way does it point?
Question 1 of 8What does this page print?
Read the code, then predict<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.
Question 2 of 8You set
input.ariaLabelledByElements = [label]. What doesinput.getAttribute("aria-labelledby")give back now?Choose an answer to see the explanation.
Question 3 of 8A page button sets
ariaDescribedByElementsto a hint inside a component's shadow root. What does reading the property back give?Choose an answer to see the explanation.
Question 4 of 8Which of these element references does Chrome not have?
Choose an answer to see the explanation.
Question 5 of 8In this page, what does
label.control.localNameprint?Read the code, then predict<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.
Question 6 of 8A shadow root's
referenceTargetnames 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.
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.
Question 8 of 8Is cross-root ARIA solved?
Choose an answer to see the explanation.
Key takeaways
- An id reference, like
fororaria-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
referenceTargetorshadowrootreferencetarget.label.controlstill 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:
- Attribute reflection: element id reference scope, MDN.
- HTML Standard: The label element, WHATWG.
- Element: ariaLabelledByElements property, MDN.
- Attribute reflection: reflected element reference scope, MDN.
- HTML Standard: Reflecting content attributes in IDL attributes, WHATWG.
- Element: attachShadow(), the referenceTarget option, MDN.
- Chrome 152 release notes: Reference Target for cross-root ARIA, Chrome for Developers.
- Reference Target: having your encapsulation and eating it too, Alice Boxhall, Igalia.
- Add reference target, whatwg/html pull request 10995, WHATWG.
- Add reference target to shadow root, whatwg/dom pull request 1353, WHATWG.
- Reference Target tracking issue, WICG/webcomponents issue 1086, WICG.
- Reference Target for Cross-Root ARIA, standards position, Mozilla.
- Reference Target for Cross-Root ARIA, standards position, WebKit.
- Accessible web components: ARIA relationships across shadow roots, Chrome for Developers, Modern Web Guidance.
- Shadow DOM and accessibility: the trouble with ARIA, Nolan Lawson.
- How to Label Shadow DOM, A11ycasts #20, Rob Dodson, Chrome for Developers.
- browser-compat-data, MDN.
Coming next: Labels and descriptions. It gives a component a name and a description that reach the right element.