Blogger Information
Blog 14
fans 2
comment 1
visits 7886
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
css定位:定位元素介绍及绝对定位和固定定位演示及差异分析
梦想
Original
530 people have browsed it

定位元素演示

1.相对定位
以文档流的方式显示,在相对定位中right和bottom两个值无效
演示:

  1. <div class="box">
  2. <div class="box1">相对定位</div>
  3. <div class="box2">绝对定位</div>
  4. <div class="box3">固定定位</div>
  5. </div>
  6. .box > .box1 {
  7. background-color: aquamarine;
  8. position: relative;
  9. left: 3.125rem;
  10. top: 30px;
  11. /* right bottom 这两个值在相对定位下无效
  12. right: 30px;
  13. bottom: 0px; */
  14. }


2.绝对定位
脱离文档流,页面布局会塌陷在没设置其他属性时box3会顶上来,参照物:定位父级,若定位父级无定位时会一层一层的往上找,若没有的话会以初始包含块为定位。初始包含块:视觉上来看时视口,但是不等于视口
演示:

  1. .box .box2 {
  2. background-color: yellow;
  3. position: absolute;
  4. left: 18.75rem;
  5. }


若父级.box不是定位父级时会上找如body head html
演示:
找不到定位父级

body定位

  1. body {
  2. width: 62.5rem;
  3. height: 900px;
  4. border: 1px solid red;
  5. position: absolute;
  6. }


html定位

  1. html{
  2. width: 1200px;
  3. height: 1000px;
  4. border:2px solid blue;
  5. position: absolute;
  6. }


父级div:

  1. div {
  2. position: absolute;
  3. }


3.固定定位,同样脱离文档流,但参照物是初始包含块。

  1. .box .box3{
  2. position: fixed;
  3. right: 0;
  4. bottom: 0;
  5. }

2.差异分析

绝对定位和固定定位差异分析:

两者都会脱离文档流,视觉上不同地方,绝对定位会根据滚动条来滚动,固定定位则不会,他会展现在浏览器视口的顶层。不会被其他定位覆盖。
代码上:绝对定位会找自己的父级定位,找不到则会一层一层的往上找,还找不到的话会以初始包含块为定位参考物,固定定位则会直接以初始包含块为参照物。

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!