一键学会三种定位布局,相对定位、绝对定位、固定定位!

旧城等待, 2022-10-23 03:59 344阅读 0赞

定位布局

  1. 定位属性: position
  2. 作用: 指定元素的定位类型
  3. 取值:
  4. static 默认不定位
  5. 相对定位
  6. 绝对定位
  7. 固定定位

相对定位

  1. 关键字 positionrelative
  2. 特点:
  3. 相对不脱离文档流,并且是占有之前的位置,随着自身原来位置的左顶点进行位置偏移的。
  4. 偏移量:
  5. top
  6. left
  7. right
  8. bottom
  9. 堆叠顺序:
  10. 在元素出现堆叠效果时改变他们的顺序
  11. 属性: z-index
  12. 取值: 没有单位的数字 值越大越靠上,可以是负数
  13. <!DOCTYPE html>
  14. <html lang="en">
  15. <head>
  16. <meta charset="UTF-8">
  17. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <title>Document</title>
  19. <style> .father{ width: 300px; height: 300px; border: 1px solid red; margin: 0 auto; } .son1,.son2,.son3{ width: 100px; height: 100px; } .son1{ background-color: red; /* 相对定位 */ position: relative; left: 100px; /* z-index:1; */ } .son2{ background-color: green; /* position: relative; */ left: 100px; top: -100px; } .son3{ background-color: blue; /* position: relative; */ top: -200px; left: 100px; /* z-index: 1; */ } </style>
  20. </head>
  21. <body>
  22. <div class="father">
  23. <div class="son1"></div>
  24. <div class="son2"></div>
  25. <div class="son3"></div>
  26. </div>
  27. </body>
  28. </html>

图示如下:
在这里插入图片描述

绝对定位

  1. 关键字: positionabsolute
  2. 特点:
  3. 脱离文档流,并且不占有之前的位置,随着页面的左顶点进行位置偏移。

****加粗样式一个简单的例子给大家看看:

  1. 一个简单的3x3竖向排列的小块给大家演示一下
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style> .gfather{ width: 600px; height: 600px; border: 1px solid red; margin: 0 auto; /* position: relative; */ } .father{ width: 300px; height: 300px; border: 1px solid red; margin: 100px auto; /* position: relative; */ } .son1,.son2,.son3{ width: 100px; height: 100px; } .son1{ background-color: red; } .son2{ background-color: green; position: absolute; top: 50px; left: 50px; } .son3{ background-color: blue; /* position: absolute; top: 50px; left: 50px; */ /* right: 50px; */ } </style>
  9. </head>
  10. <body>
  11. <div class="gfather">
  12. <div class="father">
  13. <div class="son1"></div>
  14. <div class="son2"></div>
  15. <div class="son3"></div>
  16. </div>
  17. </div>
  18. </body>
  19. </html>

图示如下:
在这里插入图片描述
在这里插入图片描述

子绝父相布局

  1. 就近原则 离谁近就依靠谁的左顶点进行位置偏移,就近在使用父级元素里面使用相对定位,子元素里面就近使用

这里还是用图示的方法给大家展示:
由于代码是一样的,我这边就不一 一赘述了,就是在father父级里面使用相对定位,grandfather里面使用相对定位,子级小绿盒子跟随father进行偏移。
在这里插入图片描述

固定定位

1.随页面的左顶点进行偏移
2.脱离文档流,不占用之前位置
3.随页面的左顶点进行偏移(不受父元素影响)一直在一个位置

给大家展示代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style> /* .father{ width: 500px; height: 500px; border: 1px solid red; margin: 0 auto; } */ ul li{ list-style: none; width: 80px; height: 40px; background-color: teal; border-top: 1px solid white; text-align: center; line-height: 40px; color: white; /* 改变鼠标样式 pointer 小手的样式 */ cursor: wait; } ul{ /* position: absolute; top: 0px; right: 20px; */ position: fixed; top:100px; } a{ color: white; text-decoration: none; cursor: wait; } </style>
  8. </head>
  9. <body>
  10. <div class="father" id="top">
  11. </div>
  12. lorem*2000
  13. <ul>
  14. <li>一元抢购</li>
  15. <li>新品发布</li>
  16. <li>联系客服</li>
  17. <li>购物车</li>
  18. <li>售后服务</li>
  19. <li><a href="#top">回到顶部</a></li>
  20. </ul>
  21. </body>
  22. </html>

图示如下:
在这里插入图片描述
**总结:**想要学好这三种定位布局其实很简单,只需要仔细理解某种定位的特点,结合案例分析练习体会其中的功能和作用即可!

**注意:**三种定位各有优势和缺点,需要将绝对定位和相对定位一起结合使用方可起到很好的作用,固定定位一般用在不需要动的导航条之类的!

发表评论

表情:
评论列表 (有 0 条评论,344人围观)

还没有评论,来说两句吧...

相关阅读

    相关 相对定位绝对定位

    什么是文档流? 将窗体自上而下分成一行行, 并在每行中按从左至右的顺序排放元素,即为文档流。 只有三种情况会使得元素脱离文档流,分别是:浮动绝对定位和相对定位。 对pos