博客列表 >【CSS】定位类型与原理:相对定位-绝对定位-固定定位-粘性定位

【CSS】定位类型与原理:相对定位-绝对定位-固定定位-粘性定位

可乐随笔
可乐随笔原创
2022年11月27日 20:22:36557浏览

定位类型

  • 相对定位:position:relative,相对定位,元素仍在文档流中,受文档布局限制(源码顺序),只是相对原始位置发生了偏移而已。
  • 绝对定位:position:absolute,总是相对于一个固定的定位参考父级,父级中找不到定位元素,就继续向上找,直到html。
  • 固定定位:position:fixed,不是相对父级,总是相对于html定位(看成绝对定位的一个特例)。
  • 粘性定位:position:sticky = static + fixed 粘性定位 = 静态定位 + 固定定位。常用来做浮动置顶。

参考代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>定位的类型与原理</title>
  8. </head>
  9. <body>
  10. <!--
  11. ! 定位布局
  12. * 1. 定位三个必知术语: 定位元素, 最初包含块(定位上下文/html), 参照物
  13. (1). 定位元素: position: relative / absolute / fixed
  14. (2). 最初包含块: 包含了<html>的不可见的,但可感知的区块,大多场景下与"视口"表现一致,但并不相同
  15. (3). 参照物: 自身, 祖先定位元素, 最初包含块
  16. * 2. 页面坐标系
  17. (1) 与数学坐标每系不同
  18. (2) 以左上角为起始点,坐标(0,0)
  19. (3) 向右, X 变大, 向下, Y 变大
  20. (4) 页面四个顶点用: top,left,right,bottom表示
  21. * 3. 定位类型: 相对, 绝对, 固定, 默认静态
  22. (1). 相对定位: position: relative
  23. (2). 绝对定位: position: absolute
  24. (3). 固定定位: position: fixed
  25. (4). 静态定位: position: static (浏览器默认,即文档流布局)
  26. * 4. 定位元素:
  27. (1): 定义: 也叫"可定位元素",即可以在文档流中自定义元素的排列方向
  28. (2): 属性: position: relative / absolute / fixed,即 非static即可
  29. * 5. 定位参照物:
  30. (1): 相对定位: 相对于"自身在文档流中的位置"
  31. (2). 绝对定位: 相对于"距离它最近的定位元素的位置",即常说的"定位父级",逐级向上直到最初包含块
  32. (3). 固定定位: 总是相对于"最初包含块"定位
  33. -->
  34. <!-- 1. 相对定位 -->
  35. <div class="box box1">
  36. <div class="item relative">相对定位</div>
  37. <div>Hello</div>
  38. </div>
  39. <style>
  40. .box {
  41. border: 1px solid #000;
  42. height: 200px;
  43. width: 200px;
  44. background-color: lightcyan;
  45. }
  46. .item {
  47. height: 50px;
  48. width: 100px;
  49. border: 1px solid #000;
  50. }
  51. /* 相对定位 */
  52. .box.box1 .item.relative {
  53. background-color: lightgreen;
  54. /* 默认静态定位 */
  55. position: static;
  56. /* 将文档流中的元素转为"定位元素" */
  57. /* position: 只要不是static; */
  58. position: relative;
  59. top: 20px;
  60. left: 20px;
  61. /* 相对定位,元素仍在文档流中,受文档布局限制(源码顺序) */
  62. /* 只是相对原始位置发生了偏移而已 */
  63. }
  64. </style>
  65. <hr />
  66. <!-- 2. 绝对定位 -->
  67. <div class="box box2">
  68. <div class="item absolute">绝对定位</div>
  69. </div>
  70. <style>
  71. .box.box2 .item.absolute {
  72. background-color: wheat;
  73. /* 绝对定位 */
  74. position: absolute;
  75. /* 定位偏移量 */
  76. top: 20px;
  77. left: 20px;
  78. /* 绝对定位元素,总是相对于它的祖先定位元素进行定位 */
  79. /* ! 因为父级没有定位元素,所以逐级向上查询,直到html */
  80. }
  81. /* 我们将它的父级转为定位元素 */
  82. .box.box2 {
  83. /* 实际工作中, relative用得极少, 除了当定位参考父级 */
  84. position: relative;
  85. /* position: absolute; */
  86. }
  87. /* 垂直水平居中 */
  88. .box.box2 .item.absolute {
  89. top: 0;
  90. left: 0;
  91. right: 0;
  92. bottom: 0;
  93. margin: auto;
  94. }
  95. /* .box.box2 .item.absolute {
  96. transform: translate(25%, 75%);
  97. } */
  98. </style>
  99. <hr />
  100. <!-- 3. 固定定位 -->
  101. <div class="box box3">
  102. <div class="item fixed">固定定位</div>
  103. </div>
  104. <style>
  105. .box.box3 .item.fixed {
  106. background-color: violet;
  107. width: 100vw;
  108. /* 固定定位,总是相对于一个固定的定位参考父级,html */
  109. position: fixed;
  110. /* top: 0;
  111. left: 0; */
  112. right: 0;
  113. bottom: 0;
  114. }
  115. .box.box3 {
  116. /* 固定定位不是相对于父级 */
  117. position: relative;
  118. }
  119. /* 适合于做固定位置的导航, 楼层, 客服 */
  120. body {
  121. height: 2000px;
  122. }
  123. /**
  124. *
  125. * 1. 相对定位: 静态定位的特例,还在文档流
  126. * 2. 绝对定位: 相对于具有定位属性的祖先元素定位
  127. * 3. 固定定位: 总是相对于html定位(看成绝对定位的一个特例)
  128. *
  129. */
  130. </style>
  131. </body>
  132. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议