日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

transform 遇上 position: fixed

發布時間:2025/3/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 transform 遇上 position: fixed 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近遇到一個有意思的現象,以下 demo 中 fixed 的元素沒有相對 viewport 定位,而是相對于它的父元素進行定位。

<html><head><style>.parent {width: 200px;height: 300px;background: yellow;transform: scale(1);}.fixed {position: fixed;left: 0;right: 0;bottom: 0;background: red;}</style></head><body><div class='parent'>parent<div class='fixed'>fixed</div></div></body> </html>

  

?

在 w3c 中對 position: fixed 的[定義](https://www.w3.org/TR/css-position/#fixed-pos)如下:

Fixed positioning is similar to absolute positioning. The only difference is that for a fixed positioned box, the containing block is established by the viewport.

大概意思就是,fixed 元素的塊級格式上下文 Block Formatting Context(BFC) 由 viewport 創建,也就是fixed 元素的 BFC 包含在根元素的 BFC 里。

那以上 demo 的表現就說不過去了。為什么呢?。谷歌一下,原來是父元素的 transform 在搗亂。

再看看 w3c 對 transform 的[定義]():

For elements whose layout is governed by the CSS box model, any value other than none for the transform property also causes the element to establish a containing block for all descendants. Its padding box will be used to layout for all of its absolute-position descendants, fixed-position descendants, and descendant fixed background attachments.

  

大概意思就是,transform 屬性使元素創建了**新的 BFC,所有**的子元素都被包含在這個新的 BFC 內。那么設置了 position: fixed 的子元素 BFC 被包含在了 transform 元素的 BFC 里。

BFC 和定位有什么關系呢,繼續翻 w3c,有段關于 BFC 的[定義](https://www.w3.org/TR/css-position/#def-cb):

?

The position and size of an element’s box(es) are sometimes computed relative to a certain rectangle, called the containing block of the element.

  

元素的位置和尺寸是相對于一個確定的 BFC 計算的。

所以 demo 展示的 fixed 元素位置是根據它所在的 BFC 計算的。

轉載于:https://www.cnblogs.com/xxhuan/p/11030244.html

總結

以上是生活随笔為你收集整理的transform 遇上 position: fixed的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。