CSS 锚点定位 - 锚点定位引入(anchor-name、position-anchor)

html 复制代码
<button class="anchor">点击我</button>
<div class="tooltip">我是工具提示</div>
  1. 使用锚点定位
css 复制代码
/* 锚点元素 */
.anchor {
    anchor-name: --my-btn;
    position: relative;
}

/* 定位元素 */
.tooltip {
    background: #333;
    color: white;
    padding: 8px 12px;
    border-radius: 4px;
    position: fixed;

    /* tooltip 的上边缘对齐 anchor 的上边缘 */
    top: anchor(--my-btn top);

    /* tooltip 的左边缘对齐 anchor 的右边缘 */
    left: anchor(--my-btn right);
}
  1. 另一种写法
css 复制代码
/* 锚点元素 */
.anchor {
    anchor-name: --my-btn;
    position: relative;
}

/* 定位元素 */
.tooltip {
    background: #333;
    color: white;
    padding: 8px 12px;
    border-radius: 4px;
    position: fixed;

    /* tooltip 定位相对于 anchor */
    position-anchor: --my-btn;

    /* tooltip 的上边缘对齐 anchor 的上边缘 */
    top: anchor(top);

    /* tooltip 的左边缘对齐 anchor 的右边缘 */
    left: anchor(right);
}
相关推荐
远航_13 小时前
OpenSpec 完整详细介绍
前端·后端
召钱熏14 小时前
状态枚举正确≠渲染正确:一个语音按钮的状态机边界修复实录
android·前端
SkyWalking中文站14 小时前
认识 Horizon UI · 1/17:SkyWalking 新一代可观测性控制台
运维·前端·监控
cidy_9814 小时前
Dify 操作教程:工作流编排 & Chat 对话编排
前端·工作流引擎
tangdou36909865514 小时前
AI真好玩系列-2分钟快速了解DeepAgents | Quick Guide to DeepAgents in 2 Minutes
前端·javascript·后端
张元清14 小时前
React useIntersectionObserver Hook:懒加载与可见性检测(2026)
javascript·react.js
小四的小六14 小时前
AI Agent效果评测实战——搭完Agent才是噩梦的开始
前端
梨子同志14 小时前
JavaScript
前端
彭于晏爱编程14 小时前
纯 JS + Node,一个下午手搓了能读懂公司代码的 AI 助手,老板以为我转行了
前端·javascript
Delicate15 小时前
前端路由扫盲篇:Hash 模式和 History 模式到底怎么选?
前端