position 属性指定了元素的定位类型。css
position 属性的五个值:html
HTML元素的默认值,即没有定位,元素出如今正常的流中。web
静态定位的元素不会受到 top, bottom, left, right影响。浏览器
div.static { position: static; border: 3px solid #73AD21; }
h2.pos_left { position:relative; left:-20px; } h2.pos_right { position:relative; left:20px; }
移动相对定位元素,但它本来所占的空间不会改变spa
h2.pos_top { position:relative; top:-50px; }
元素的位置相对于浏览器窗口是固定位置。code
即便窗口是滚动的它也不会移动:htm
p.pos_fixed { position:fixed; top:30px; right:5px; }
h2 { position:absolute; left:100px; top:150px; }
position: sticky; 基于用户的滚动位置来定位。blog
粘性定位的元素是依赖于用户的滚动,在 position:relative 与 position:fixed 定位之间切换。get
它的行为就像 position:relative; 而当页面滚动超出目标区域时,它的表现就像 position:fixed;,它会固定在目标位置。it
元素定位表现为在跨越特定阈值前为相对定位,以后为固定定位。
这个特定阈值指的是 top, right, bottom 或 left 之一,换言之,指定 top, right, bottom 或 left 四个阈值其中之一,才可以使粘性定位生效。不然其行为与相对定位相同。
注意: Internet Explorer, Edge 15 及更早 IE 版本不支持 sticky 定位。 Safari 须要使用 -webkit- prefix (查看如下实例)。
div.sticky { position: -webkit-sticky; /* Safari */ position: sticky; top: 0; background-color: green; border: 2px solid #4CAF50; }