标签:cti 滚动 doc 留空 block nsf 定义 页面布局 相同
①元素的位置是在文档正常布局流中的位置。
②设置top right bottom left与z-index无效。
③在未指定position时,static是默认值
以下例子进行说明:
.default{ width: 100px; height: 100px; background-color: salmon; top: 20px; left: 20px; } .static{ width: 100px; height: 100px; background-color: yellowgreen; top: 20px; left: 20px; }
<body> <div class="default">default</div> <div class="static">static-1</div> <div class="static">static-2</div> </body>
运行结果:
结果说明:
① 元素先放置在未添加定位时的位置(即文档正常布局流中的位置),然后在不改变页面布局的情况下,依据元素的top right bottom left重新调整元素位置。
② 它的偏移量对其它元素定位均不会产生影响,也就是是说,元素在页面布局中所占的空间是不变的,这一点与postion值是static的情况是相同的
下面的例子说明以上两点
.static{ width: 100px; height: 100px; background-color: salmon; top: 20px; left: 20px; } .relative{ position: relative; width: 100px; height: 100px; background-color: yellowgreen; top: 20px; left: 20px; }
<body> <div class="static">static-1</div> <div class="relative">relative-1</div> <div class="static">static-2</div> </body>
运行结果
结果说明:
PS:当z-index的值不为auto时,这个值会创建一个层叠上下文。position:relative对table-*-group
, table-row
, table-column
, table-cell
, and table-caption元素
无效
以下例子进行说明
.relative { position: relative; width: 200px; height: 200px; background-color: yellowgreen; } .static { width: 150px; height: 150px; background-color: lightskyblue; } .absolute { width: 100px; height: 100px; background-color: salmon; top: 50px; left: 50px; position: absolute; }
<body> <div class="relative"> relative-ancestor <div class="static"> static-parent <div class="absolute">absolute</div> </div> </div> </body>
结果说明:如果按照文档布局流,元素absolute应该再图中元素default-2的位置,但是其实际结果位于default-1,default-2之上,且default-2位置不受absolute的影响,与①中定义相符合
以下例子进行说明:
.static { width: 200px; height: 200px; background-color: yellowgreen; } .relative { position: relative; width: 200px; height: 200px; background-color: yellowgreen; } .absolute { width: 100px; height: 100px; background-color: salmon; top: 50px; left: 50px; position: absolute; }
<body> <div class="relative"> relative-ancestor <div class="static"> static-parent <div class="absolute">absolute</div> </div> </div> </body>
运行结果一:
修改html代码
<body> <div class="absolute">absolute</div> </div> </body>
运行结果二
结果说明:
例子如下:
.relative { position: relative; width: 100px; height: 100px; background-color: yellowgreen; margin-bottom: 20px; margin-top: 20px; }
<body> <div class="relative">relative-1</div> <div class="relative">relative-2</div> </body>
运行结果一
修改代码
.relative { position: relative; width: 100px; height: 100px; background-color: yellowgreen; margin-bottom: 20px; margin-top: 20px; } .absolute { width: 100px; height: 100px; background-color: salmon; top: 100px; left: 100px; position: absolute; margin-bottom: 20px; margin-top: 20px; }
运行结果二
结果说明:
大多数情况下,height和width都设定为auto的绝对定位元素,按其内容大小调整尺寸。但是被绝对定位的元素可以通过指定top,bottom,保留height(即auto)来填充可以用的垂直空间。同样也可以通过指定left,right并将width指定为auto来填充可用的水平空间
例子如下
.relative { position: relative; width: 1000px; height: 1000px; background-color: yellowgreen; } .fixed { position: fixed; width: 150px; height: 150px; background-color: lightskyblue; top: 20px; left: 20px; }
<body> <div class="relative"> relative-parent <div class="fixed">fixed</div> </div> </body>
运行结果
例子如下:
.relative { position: relative; width: 200px; height: 200px; background-color: yellowgreen; transform: rotate(7deg); } .fixed { position: fixed; width: 50px; height: 50px; background-color: lightskyblue; top: 20px; left: 20px; }
<body> <div class="relative"> relative-parent <div class="fixed">fixed</div> </div> </body>
运行结果
结果说明 :与③中的说明符合
以下是例子:
.relative1 { position: relative; width: 200px; height: 200px; background-color: yellowgreen; } .sticky { position: sticky; width: 100px; height: 100px; background-color: lightskyblue; /*left: 50px;*/ /*top: 50px;*/ } .relative2 { position: relative; width: 100px; height: 100px; background-color: salmon; top:50px; left: 50px; }
<body> <div class="relative1"> relative1 <div class="sticky">sticky</div> <div class="relative2">relative2</div> </div> </body>
运行结果一
修改css代码,注释取消。html保持不变
.relative1 { position: relative; width: 200px; height: 200px; background-color: yellowgreen; } .sticky { position: sticky; width: 100px; height: 100px; background-color: lightskyblue; left: 50px; top: 50px; } .relative2 { position: relative; width: 100px; height: 100px; background-color: salmon; top:50px; left: 50px; }
运行结果二
结果说明
CSS布局学习(三) - position属性定义及解释(官网直译)
标签:cti 滚动 doc 留空 block nsf 定义 页面布局 相同
原文地址:https://www.cnblogs.com/xiaochengzi/p/9958672.html