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);
}
相关推荐
LaughingZhu8 分钟前
Product Hunt 每日热榜 | 2026-05-21
前端·人工智能·经验分享·chatgpt·html
怕浪猫15 分钟前
Electron 开发实战(一):从零入门核心基础与环境搭建
前端·electron·ai编程
辰海Coding21 分钟前
MiniSpring框架学习笔记-解决循环依赖的简化IoC容器
笔记·学习
杜子不疼.27 分钟前
【C++ AI 大模型接入 SDK】 - DeepSeek 模型接入(上)
开发语言·c++·chatgpt
加号333 分钟前
【C#】 串口通信技术深度解析及实现
开发语言·c#
晓梦林39 分钟前
cp520靶场学习笔记
android·笔记·学习
小鹏linux1 小时前
Ubuntu 22.04 部署开源免费具有精美现代web页面的Casdoor账号管理系统
linux·前端·ubuntu·开源·堡垒机
sycmancia1 小时前
Qt——编辑交互功能的实现
开发语言·qt
石山代码2 小时前
C++ 内存分区 堆区
java·开发语言·c++
心中有国也有家2 小时前
cann-recipes-infer:昇腾 NPU 推理的“菜谱集合”
经验分享·笔记·学习·算法