Sub topic 13
Feature detection, fallbacks, and accessibility
This chapter assumes you know how to declare an anchor with anchor-name, connect an element with position-anchor, place it with position-area, and use the Popover API at a basic level. It covers three things that decide whether anchor positioning is safe to ship: detecting support, giving other browsers a usable layout, and keeping the result accessible.
Why this needs its own chapter
Anchor positioning is newer than most of CSS. Chromium-based browsers have shipped it. Other engines have been adding it at different speeds. Check a current compatibility table such as MDN or Can I Use before you decide how much fallback work you need.
A browser that does not understand position-area or anchor() ignores those declarations. It does not report an error. The element then falls back to ordinary absolute positioning. Without a plan, a tooltip can land at the top left of its containing block, or a menu can cover the page. The plan has three parts:
- Write a simple layout that works everywhere.
- Layer anchor positioning on top, only where it is supported.
- Make sure the meaning of the page does not depend on the position.
Feature detection with @supports
@supports tests whether a browser understands a property and value. The test for anchor positioning uses anchor-name, because any browser that parses it has the feature family.
@supports (anchor-name: --a) {
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
}
}The name --a is arbitrary. The browser only checks that the value is valid for the property. Browsers that fail the test skip the whole block, so any rules you write outside it act as the fallback.
You can also test from JavaScript. This is useful when script needs to choose a different strategy, such as loading a positioning library.
const supportsAnchors = CSS.supports("anchor-name", "--a");
if (!supportsAnchors) {
document.documentElement.classList.add("no-anchor");
}Prefer @supports in CSS when you can. It needs no script and cannot run late.
Test the feature you use
anchor-name is a good general test. If you depend on a newer part, such as position-area or position-try-fallbacks, test that property as well. Support for the pieces can differ between versions of the same browser.
@supports (position-area: top) and (position-try-fallbacks: flip-block) {
.menu {
position-area: bottom span-right;
position-try-fallbacks: flip-block;
}
}The and means both conditions must pass. If either fails, none of the rules apply, and the fallback layout is used.
Designing the fallback layout
The fallback is not a degraded afterthought. For many interfaces, a simpler layout is the right default, and anchor positioning is an enhancement. Three approaches are common.
| Approach | How it works | Trade-off |
|---|---|---|
| Inline layout | The element sits in normal flow, below or next to its trigger | Always works, but pushes other content around |
| Relative wrapper | A `position: relative` wrapper holds the trigger and an absolutely positioned element with fixed offsets | Works everywhere, but cannot react to viewport edges |
| JavaScript library | A script measures and positions the element | Handles edges, but adds code and layout work |
The relative wrapper is the closest match to the anchored result. Here is a tooltip that works in every browser and improves where anchors are supported.
<span class="tip-wrap">
<button class="trigger" aria-describedby="tip-1">Save</button>
<span class="tooltip" id="tip-1" role="tooltip">Saves your changes</span>
</span>.tip-wrap {
position: relative;
display: inline-block;
}
.tooltip {
position: absolute;
bottom: 100%;
left: 50%;
translate: -50% 0;
margin-bottom: 8px;
white-space: nowrap;
}
@supports (anchor-name: --a) {
.trigger {
anchor-name: --tip;
}
.tooltip {
position-anchor: --tip;
position-area: top;
inset: auto;
translate: none;
margin-bottom: 8px;
position-try-fallbacks: flip-block;
}
}The base rules place the tooltip above its wrapper using percentages. Inside @supports, the enhanced rules replace them. Note the resets. inset: auto clears the bottom and left values, and translate: none clears the manual centering. If you skip these resets, the old offsets still apply and fight with position-area.
Pitfall: leftover offsets
This is the most common bug in progressive enhancement here. Inset properties set to a value other than auto take part in placement, and they combine with position-area in ways that are hard to predict. Whenever the enhanced block replaces a manual layout, reset every inset and every transform the manual layout used.
What happens with a popover
Popovers raise a second question. A popover opened with the popover attribute is rendered in the top layer, so it escapes clipping and stacking problems. Browsers that support the popover attribute but not anchor positioning will show the popover in the default centered position of the top layer. That is usable, though not attached to its button.
Here is the markup from the earlier chapter, with the enhancement guarded.
<button id="menu-btn" popovertarget="menu">Menu</button>
<div id="menu" popover>
<a href="/profile">Profile</a>
<a href="/settings">Settings</a>
</div>#menu {
padding: 0.5rem;
}
@supports (anchor-name: --a) {
#menu-btn {
anchor-name: --menu-btn;
}
#menu {
position-anchor: --menu-btn;
position-area: bottom span-right;
inset: auto;
margin: 0;
position-try-fallbacks: flip-block;
}
}Outside the support block, the menu opens in the browser's default popover placement. Inside it, the menu attaches to the button. The links work either way, so the feature is a pure enhancement.
Accessibility
Anchor positioning changes where an element is drawn. It does not change the document structure, the reading order, or the accessibility tree. This has two consequences.
First, the visual position can disagree with the source order. A tooltip placed above a button still sits after it in the DOM. Screen readers and keyboard users follow the DOM, not the picture. So the DOM order must make sense on its own.
Second, CSS cannot create the relationship between a trigger and its target. You must express that relationship in HTML.
Express the relationship in markup
| Pattern | Markup that carries the meaning |
|---|---|
| Tooltip | `aria-describedby` on the trigger pointing to the tooltip's `id` |
| Popover menu | `popovertarget` on the button, which also links the two for assistive technology |
| Disclosure of extra content | `aria-expanded` on the button, kept in sync with open state |
The popovertarget attribute does real work. It makes the button toggle the popover, and the browser exposes the connection to assistive technology. Anchor positioning adds only the visual attachment.
Keyboard and focus
Position tells a sighted mouse user which element a popover belongs to. Keyboard users need other signals.
- Make the trigger a real
buttonor link, so it is focusable and activates with Enter or Space. - Let the Popover API handle light dismiss and the Escape key. Do not rebuild them.
- Show tooltips on focus as well as hover. A tooltip that appears only on hover is unreachable by keyboard.
- Keep the popover's content in a sensible tab order. Because it follows the trigger in the DOM, tabbing into it works naturally when it is placed right after the button.
Do not hide meaning with position-visibility
position-visibility can hide an anchored element when its anchor scrolls out of view. That is a visual choice. If the element carries information that a user needs, hiding it removes access to it for sighted users while it may stay present for others. Use it only for supplementary elements such as decorative tooltips.
Fallback placements and readability
When position-try-fallbacks moves an element to another side, check that the new placement still does not cover the trigger or related content. Test at narrow widths and at high zoom, such as 200 percent, because zooming reduces the effective viewport and triggers fallbacks more often.
Respect reduced motion if you animate the transition between placements. Avoid animating position changes unless the user has no motion preference set.
@media (prefers-reduced-motion: no-preference) {
.tooltip {
transition: opacity 150ms ease;
}
}A decision flow
The diagram summarizes how to choose a strategy for a given component.
Common errors
- Testing only in one browser. Everything looks right in a supporting browser, and the fallback is never seen. Test with the
@supportsblock disabled, for example by temporarily changing the test to a property that does not exist. - Forgetting resets. Old
top,left, andtranslatevalues stay active inside the enhanced block. - Using position as the only link. Without
aria-describedbyorpopovertarget, assistive technology sees two unrelated elements. - Hover-only tooltips. Add focus handling, and make sure the tooltip remains visible while the pointer moves onto it if it contains interactive content.
- Assuming fallbacks always rescue overflow. If none of the listed fallbacks fits, the browser keeps the last tried or original placement and the element may still overflow. Add a
max-widthormax-height, and give wide content room to wrap.
Summary of the working model
Treat anchor positioning as a visual layer over a structure that already works. Detect support with @supports, write a fallback that is a real layout, reset anything the fallback sets that the enhanced layout should not inherit, and carry the trigger and target relationship in HTML attributes. Check the result with the keyboard, at high zoom, and in a browser that lacks the feature.
Exercise
A tooltip uses bottom: 100%; left: 50%; translate: -50% 0; as its base layout. Inside @supports (anchor-name: --a) you add position-anchor: --tip; position-area: top;. In a supporting browser the tooltip appears in the wrong place. What is the likely cause, and what lines fix it?
Show answer
The base bottom, left, and translate values still apply inside the supported branch and interfere with position-area. Reset them in the @supports block with inset: auto; and translate: none;.
Exercise
Name two things in HTML, not CSS, that connect a tooltip to its trigger and a popover menu to its button for assistive technology.
Show answer
Use aria-describedby on the trigger, pointing to the tooltip's id, for the tooltip. Use the popovertarget attribute on the button, pointing to the popover's id, for the menu.
Exercise
Why can a layout that works at 100 percent zoom start using fallback placements at 200 percent zoom?
Show answer
Zooming shrinks the effective viewport in CSS pixels. The anchored element has less room, so its preferred placement overflows more often and the browser tries the entries in position-try-fallbacks.