← 返回完整题库
CSS困难

position: fixed 一定是相对于浏览器窗口进行定位吗?

参考答案

不一定。

默认相对视口定位,但若祖先元素创建了新的包含块,则会相对该祖先:

  • transform 非 none
  • filter / backdrop-filter 非 none
  • perspective 非 none
  • will-change 指定上述属性
  • contain: paint / container-type

此时 fixed 元素被"捕获"在祖先内,滚动行为也随之改变(常见于弹窗被 transform 容器限制)。

本题来自 计算机知识面试题库,共 921 道题。