定位.html 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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>
  8. #box {
  9. width: 500px;
  10. height: 2400px;
  11. margin: 0 auto;
  12. position: relative;
  13. border:1px solid #f00;
  14. }
  15. div {
  16. width: 200px;
  17. height: 200px;
  18. font-size: 30px;
  19. color: #000;
  20. text-align: center;
  21. line-height: 200px;
  22. margin-top: 10px;
  23. }
  24. #part1 {
  25. background: #00f;
  26. }
  27. #part2 {
  28. background: #0ff;
  29. position: absolute;
  30. /* position: relative; */
  31. /* position: fixed; */
  32. /* position: sticky; */
  33. /* position: static; */
  34. top: 100px;
  35. left: 100px;
  36. }
  37. #part3 {
  38. background: #f0f;
  39. position: static;
  40. }
  41. #part4 {
  42. background: #ff0;
  43. }
  44. #part5 {
  45. background: #f00;
  46. }
  47. #part6 {
  48. background: #0f0;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <!--
  54. 定位:position
  55. top bottom right left
  56. 1.sticky 粘性定位 一般用作于吸顶效果 top
  57. 2.static 静态定位
  58. 3.fixed 固定定位 脱离文档流 不占位 相对于body进行定位
  59. 4.relative 相对定位 不脱离文档流 占位 相对于自身进行定位
  60. 5.absolute 绝对定位
  61. a.父盒子无定位 相对于祖先元素进行定位 脱离文档流不占位
  62. b.父盒子有定位 子绝父相 相对于父盒子进行定位 脱离文档流不占位
  63. -->
  64. <div id="box">
  65. <div id="part1">1</div>
  66. <div id="part2">2</div>
  67. <div id="part3">3</div>
  68. <div id="part4">4</div>
  69. <div id="part5">5</div>
  70. <div id="part6">6</div>
  71. <div id="part3">7</div>
  72. <div id="part4">8</div>
  73. <div id="part5">9</div>
  74. <div id="part6">10</div>
  75. </div>
  76. </body>
  77. </html>