# position_fixed什么时候会失效|博客系列

> 本文详细讲解了position fixed在父元素或祖先元素设置transform属性时会失效的原因和表现，结合实际案例和浏览器兼容性，帮助前端开发者理解和避免fixed定位失效的问题。

- 原文：https://feinterview.poetries.top/docs/fe-blog-docs/blog-docs/css/-position_fixed%E4%BB%80%E4%B9%88%E6%97%B6%E5%80%99%E4%BC%9A%E5%A4%B1%E6%95%88
- 来源：前端进阶之旅（feinterview.poetries.top）
- 更新：2025.10.25

---

## position: fixed什么时候会失效？
我们知道，设置了`position: fixed`固定定位属性的元素会脱离文档流，达到“超然脱俗”的境界。
也就是说此时给这种元素设置`top, left, right, bottom`等属性是根据**浏览器窗口**定位的，与其上级元素的位置无关。

但是有一种情况例外：

若是设置了`position: fixed`属性的元素，它的上级元素设置了`transform`属性则会导致固定定位属性失效。
无论你的`transform`设置的是什么属性都会影响到`position: fixed`。

注意，这个特性表现，目前只在Chrome浏览器/FireFox浏览器下有，IE浏览器，包括IE11, `fixed`还是`fixed`的表现 😊。

看下面的案例1：

```html
<style>
    .father {
        width: 300px;
        height: 300px;
        background: yellow;
        transform: translate(100px);
        /* transform: scale(0.5); */
        /* transform: rotate(-45deg); */
    }
    .son {
        width: 100px;
        height: 100px;
        background: red;
        position: fixed;
        top: 400px;
    }
</style>
<body>
<div class="father">
   <div class="son"></div>
</div>
</body>
```
给父级加上了`transform`属性之后就会影响子级的固定定位了。如下图：

![](https://s.poetries.top/uploads/2022/07/cbbbb8b4f22dd1cf.png)

![](https://s.poetries.top/uploads/2022/07/686868e92aacfe49.png)

**其实不仅仅是给父级加`transform`属性会失效，只要上级存在`transform`属性都会导致`position: fixed`失效。**

案例2：
```html
<style>
    .content{
        transform: translate(100px);
    }
    .father {
        width: 300px;
        height: 300px;
        background: yellow;
    }
    .son {
        width: 100px;
        height: 100px;
        background: red;
        position: fixed;
        top: 400px;
    }
</style>
<body>
    <div class="content">
        <div class="father">
            <div class="son"></div>
        </div>
    </div>
</body>
```
上面的案例也会影响`position: fixed`属性。

具体原理可以看一下张大大的这篇文章：https://www.zhangxinxu.com/wordpress/2015/05/css3-transform-affect/
