锚点元素未被识别的主因是浏览器未将其视为锚点,需满足三条件:anchor-name带--前缀、目标元素position非static、且非display:none或visibility:hidden。
最常见的“--anchor is not defined”错误,根本原因不是语法写错,而是浏览器压根没把那个元素当锚点。关键条件有三个:anchor-name 必须带 -- 前缀、目标元素必须有非 static 的 position、且不能是 display: none 或 visibility: hidden。
anchor-name: --tooltip-trigger 合法;anchor-name: tooltip-trigger 或 anchor-name: #trigger 都无效anchor-name,但没设 position: relative(或 absolute/fixed),Chrome 125+ 也会静默忽略该锚点anchor-name 是否出现在样式列表里 —— 如果没出现,说明属性没被解析,大概率是父级 display: contents 或 Shadow DOM 隔离导致anchor() 不是万能函数,它只在少数几个 CSS 属性里被识别,写错地方就完全没反应,还不报错。比如 top: anchor(--trigger top) 有效,但 margin-top: anchor(--trigger top) 会被直接丢弃。
top、right、bottom、left、translate、inset-block-start、inset-inline-start 等定位/偏移类属性anchor-size() 只能在 width、height、min-width 等尺寸属性中使用,不能混用top: anchor(--trigger top) + 8px 会解析失败,得拆成 top: anchor(--trigger top); margin-top: 8px
position: absolute 或 position: fixed,relative 或 static 下调用 anchor() 会被忽略截至 2026 年 7 月,CSS Anchor Positioning 仍不是默认启用的功能。Chrome 125+ 和 Edge 125+ 支持,但必须手动开启实验性标志;Safari 和 Firefox 完全不支持 —— 这意味着你写的代码在多数用户浏览器里根本不会运行。
chrome://flags/#enable-css-anchor-positioning,设为 Enabled,然后重启浏览器@supports (anchor-name: --x) 包裹样式做特性检测,但注意:这个检测本身在未开启 flag 时也返回 false,不能替代运行时 JS fallbackpopover 显示事件后用 getBoundingClientRect() 手动计算位置即使锚点识别成功、anchor() 写法正确,最终位置也可能严重偏移。这不是 bug,而是定位参考系被意外改变的结果。
transform、filter 或 will-change,就会创建新的包含块(containing block),导致 anchor() 计算基于该新上下文,而非视口position: fixed 时,锚点位置会随滚动变化;设为 position: absolute 时,则受最近非 static 祖先影响 —— 两者行为完全不同,选错会导致滚动时弹层“飞走”overflow: scroll 容器内,且容器发生滚动,anchor() 的坐标会实时更新,但若容器用了 contain: layout,可能中断更新链anchor-name 拼写错误 + flag 没开 + 目标元素用了 relative —— 浏览器只报一个模糊提示,你得逐项排除。建议从 DevTools 的 “Computed” 面板开始,先确认 anchor-name 是否存在,再看目标元素的 position 和 top 是否被解析,最后验证 flag 是否生效。