Sub topic 13
Feature detection, fallbacks, and accessibility
This chapter assumes you can place an element with position-area and know how anchor-name and position-anchor connect two elements. It covers three things: shipping anchor positioning to browsers that lack it, keeping the result in view, and keeping it usable for people who don't see the layout.
Detect support with @supports
Browsers that don't understand anchor positioning ignore the unknown properties. The tooltip then falls back to ordinary position: absolute rules, which are usually wrong. Write a plain layout first, then override it inside @supports.
<span class="wrap">
<button class="trigger" aria-describedby="tip">Save</button>
<span class="tooltip" id="tip" role="tooltip">Saves your draft</span>
</span>.wrap {
position: relative;
display: inline-block;
}
.tooltip {
position: absolute;
top: 100%;
left: 0;
margin-top: 8px;
}
@supports (anchor-name: --a) and (position-area: top) {
.trigger {
anchor-name: --tip;
}
.tooltip {
position-anchor: --tip;
position-area: top;
inset: auto;
margin: 0 0 8px;
position-try-fallbacks: --below, flip-inline;
}
}
@position-try --below {
position-area: bottom;
margin: 8px 0 0;
}Browsers without support use the first rule and show the tooltip below the button. Browsers with support get the anchored version. The second @supports condition matters because some versions shipped anchor-name before position-area. Testing both avoids a half-working state.
From JavaScript, use CSS.supports("anchor-name: --a").
Keep the element in view
Fallbacks protect users with small viewports, large text, or zoom, where an anchored element can run off screen. List the placements in position-try-fallbacks from most to least preferred. The browser uses the first one that fits. If none fit, the last-resort behavior is the original placement, so choose a primary placement that degrades acceptably.
Don't hide the element with position-visibility unless the content is also reachable another way. Hiding a tooltip only affects the visual rendering, so test that nothing essential disappears.
Accessibility
Anchor positioning is visual only. It does not tell assistive technology that two elements are related. You must express the relationship in markup.
- Tooltips: link them with
aria-describedby, as in the example. Show them on focus as well as hover, and let Escape dismiss them. - Menus and popovers: use a real
<button>withpopovertarget. The browser then supplies toggling, light dismiss, Escape handling, and top-layer rendering. - DOM order: place the anchored element right after its anchor. Keyboard users follow DOM order, not visual position, so a menu that appears below a button but sits at the end of the page is confusing.
- Don't encode meaning in position alone. After a fallback flips the element, "the tooltip above" may no longer be true.
- Check zoom and text resizing. Test at 200% zoom to confirm the fallbacks work.
Common errors
| Symptom | Likely cause |
|---|---|
| Tooltip appears in the wrong place in older browsers | The anchored rules sit outside `@supports` |
| Anchored element ignores `position-area` | Leftover `top` or `left` values; set `inset: auto` |
| Popover sits at the page center | The popover's default `inset` and `margin` weren't reset |
| Screen reader doesn't announce the tooltip | Missing `aria-describedby` |
Exercise
Why should the @supports condition test position-area as well as anchor-name, and where should the non-anchored layout live?
Show answer
Some browser versions supported anchor-name before position-area, so testing only anchor-name could enable a half-working layout. The non-anchored layout belongs outside the @supports block as the default rules, and the @supports block overrides it for browsers that pass both checks.
Exercise
A menu opens below its button visually, but its markup is at the end of the body. What accessibility problem does this cause, and how do you fix it?
Show answer
Keyboard and screen reader users follow DOM order, so after the button they won't reach the menu in a logical sequence. Place the menu right after the button in the DOM, and use popovertarget on the button.