# position

CSS position 属性用于指定一个元素在文档中的定位方式。top,right,bottom 和 left 属性则决定了该元素的最终位置。

# uni-app x 兼容性

Android iOS web
3.9 4.11 4.0

# 语法

position: static | relative | absolute | sticky | fixed;

# position 的属性值

relative 该关键字下,元素先放置在未添加定位时的位置,再在不改变页面布局的前提下调整元素位置(因此会在此元素未添加定位时所在位置留下空白)。position:relative 对 table-*-group, table-row, table-column, table-cell, table-caption 元素无效。

absolute 元素会被移出正常文档流,并不为元素预留空间,通过指定元素相对于最近的非 static 定位祖先元素的偏移,来确定元素位置。绝对定位的元素可以设置外边距(margins),且不会与其他边距合并。

fixed 元素会被移出正常文档流,并不为元素预留空间,而是通过指定元素相对于屏幕视口(viewport)的位置来指定元素位置。元素的位置在屏幕滚动时不会改变。打印时,元素会出现在的每页的固定位置。fixed 属性会创建新的层叠上下文。当元素祖先的 transform、perspective、filter 或 backdrop-filter 属性非 none 时,容器由视口改为该祖先。

static 该关键字指定元素使用正常的布局行为,即元素在文档常规流中当前的布局位置。此时 top, right, bottom, left 和 z-index 属性无效。

sticky 元素根据正常文档流进行定位,然后相对它的最近滚动祖先(nearest scrolling ancestor)和 containing block(最近块级祖先 nearest block-level ancestor),包括 table-related 元素,基于 top、right、bottom 和 left 的值进行偏移。偏移值不会影响任何其他元素的位置。 该值总是创建一个新的层叠上下文(stacking context)。注意,一个 sticky 元素会“固定”在离它最近的一个拥有“滚动机制”的祖先上(当该祖先的 overflow 是 hidden、scroll、auto 或 overlay 时),即便这个祖先不是最近的真实可滚动祖先。这有效地抑制了任何“sticky”行为(详情见 Github issue on W3C CSSWG)。

# 默认值

平台 默认值
uvue relative

注意:W3C 默认值为:static

# 兼容性

# position 的属性值兼容性

Android iOS web
relative 3.9 4.11 4.0
absolute 3.9 4.11 4.0
fixed 3.9 4.11 4.0
static x x 4.0
sticky x x 4.0

# 浏览器兼容性

Chrome Edge Firefox Opera Safari IE
position √( 1 ) √( 12 ) √( 1 ) √( 4 ) √( 1 ) √( 4 )
sticky √( 1 ) √( 12 ) √( 1 ) √( 4 ) √( 1 ) √( 4 )

# App平台差异

absolute元素相对父组件确定位置,fixed元素位于页面顶层。

虽然App不支持sticky属性值,但uni-app x提供了全端可用的吸顶组件,另见sticky组件

# fixed定位

position: fixed定位时,web端为相对于整个浏览器页面进行定位,app端为相对于页面(除导航栏、tabbar)定位。可以使用css变量使两端表现一致

.fixed {
  position: fixed;
  width: 100px;
  height: 100px;
  background-color: #FF0000;
  left: 10px;
  /* #ifdef WEB */
  top: calc(--window-top + 10px);
  /* #endif */
  /* #ifdef APP */
  top: 10px;  /* App端暂不支持calc */
  /* #endif */
}

# 参见