CSS-定位 | <position>

简介:

CSSposition属性用于指定一个元素在文档中的定位方式。toprightbottom和 left属性则决定了该元素的最终位置。

vqso7lxnhk.png

位置坐标由一个或两个关键字指定,并带有可选的偏移量。每个关键字(leftrighttopbottom,或center)表示任一元件的盒子的边缘或两个边缘之间的中心线上。根据上下文,center表示左边缘和右边缘之间的中心,或者表示顶边和底边之间的中心。

如果指定,偏移量可以是相对<percentage>值或绝对值<length>。正值向右或向下偏移,以适当值为准。负值在相反的方向上偏移。

如果只指定一个关键字或偏移量,则它定义x坐标; 另一个轴的值​​被假定为center

内插值

横坐标和纵坐标都是独立插值的。由于速度由<timing-function>两者定义,点将沿直线移动。

/* 1-value syntax */
keyword                  /* Either the horizontal or vertical position; the other axis defaults to center */
<length> or <percentage> /* The position on the x-axis; the y-axis defaults to 50% */

/* 2-value syntax */
keyword keyword          /* A keyword for each direction (the order is irrelevant) */
keyword value            /* The value is the offset for the edge defined by the keyword */

正式语法

[ [ left | center | right | top | bottom | <percentage> | <length> ] |
                  [ left | center | right | <percentage> | <length> ] [ top | center | bottom | <percentage> | <length> ] |
                  [ center | [ left | right ] [ <percentage> | <length> ]? ] && 
                  [ center | [ top | bottom ] [ <percentage> | <length> ]? ]
                ]

规范

Specification

Status

Comment

CSS Values and Units Module Level 3The definition of '<position>' in that specification.

Candidate Recommendation

Relists links to both definitions with the requirement to be coherent: if CSS Backgrounds and Borders Module Level 3 is supported, its definition of <position> must be used too.

CSS Backgrounds and Borders Module Level 3The definition of '<position>' in that specification.

Candidate Recommendation

Defines <position> explicitly and extends it to support offsets from any edge.

CSS Level 2 (Revision 1)The definition of '<position>' in that specification.

Recommendation

Allows combination of a keyword with a <length>, or <percentage> value.

CSS Level 1The definition of '<position>' in that specification.

Recommendation

Defines <position> anonymously as the value of background-position.

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

1.0

12

1.0 (1.7 or earlier)

4.0

3.5

1.0 (85)

Combination of a keyword and a <length> or <percentage>

1.0

12

1.0 (1.7 or earlier)

4.0

3.5

1.0 (85)

Four-value syntax (support for offset from any edge)

25.0

12

13.0 (13.0)

9.0

10.5

7.0

Feature

Android

Chrome for Android

Edge mobile

Firefox Mobile (Gecko)

IE mobile

Opera Android

iOS Safari

Basic support

(Yes)

(Yes)

(Yes)

1.0 (1)

(Yes)

(Yes)

(Yes)

Combination of a keyword and a <length> or <percentage>

(Yes)

(Yes)

(Yes)

1.0 (1)

(Yes)

(Yes)

(Yes)

Four-value syntax (support for offset from any edge)

(Yes)

(Yes)

(Yes)

13.0 (13.0)

(Yes)

(Yes)

7.1

相关文章
|
7天前
|
前端开发
|
23天前
|
前端开发 容器
最新CSS3定位元素
【8月更文挑战第28天】
16 5
|
1月前
|
前端开发
HTML+CSS基础知识(5)相对定位、绝对定位、固定定位
这篇文章介绍了HTML和CSS中的三种定位方式:相对定位、绝对定位和固定定位,并通过代码示例展示了它们如何影响元素在页面上的布局和位置。
HTML+CSS基础知识(5)相对定位、绝对定位、固定定位
|
1月前
|
前端开发 容器
css中使用定位实现吸顶效果
css中使用定位实现吸顶效果
31 2
|
28天前
|
前端开发 容器
解锁CSS定位奥秘,从迷茫到精通,让你的网页布局“稳如老狗”,引领前端新潮流!
【8月更文挑战第23天】本文深入探讨了Web前端开发中CSS定位的三大基石:相对定位、绝对定位与固定定位。通过具体示例,展示了各种定位方式的特点及应用场景。相对定位可在不影响布局的情况下微调元素位置;绝对定位使元素完全脱离文档流,适用于精确控制位置的需求;固定定位则确保元素始终固定于浏览器窗口特定位置,不受页面滚动影响。这些技巧对于实现复杂网页布局至关重要。
34 0
|
1月前
|
前端开发 UED 开发者
神秘的 CSS 属性 “position: sticky” 究竟有何魔力?带你彻底理解粘性定位的奇妙世界!
【8月更文挑战第20天】在前端开发中,CSS的粘性定位(`position: sticky`)是一种结合了相对与固定定位优点的强大工具。它使元素能在特定条件下相对定位,达到指定阈值时转为固定定位,非常适合制作“吸顶”导航栏等。例如,设置`position: sticky; top: 0;`能让导航栏滚动至顶部时固定显示。此特性不仅限于导航栏,还可应用于侧边栏等,增强布局灵活性与用户体验。尽管如此,仍需注意不同浏览器间的兼容性和可能的布局冲突。
40 0
|
2月前
|
前端开发
css实用技巧——给锚点定位添加偏移
css实用技巧——给锚点定位添加偏移
81 3
|
3月前
|
前端开发 开发者
CSS属性选择器:精确定位元素的利器
CSS属性选择器:精确定位元素的利器
|
3月前
|
前端开发 编译器
CSS定位(如果想知道CSS有关定位的知识点,那么只看这一篇就足够了!)
CSS定位(如果想知道CSS有关定位的知识点,那么只看这一篇就足够了!)
|
2月前
|
前端开发 索引 Python
技术心得:xpath、CSS定位方法
技术心得:xpath、CSS定位方法
20 0