17.定位.html 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. border: 3px solid #0f0;
  11. margin: 100px auto;
  12. /* position: relative; */
  13. }
  14. /* 定位:top left bottom right */
  15. #box div {
  16. width: 200px;
  17. height: 200px;
  18. margin-top: 20px;
  19. }
  20. #box1 {
  21. background: red;
  22. /* fixed 固定定位 相对于浏览器窗口进行定位 脱离文档里 不占位 滚动时候 位置不动 */
  23. /* position: fixed;
  24. top: 100px;
  25. left: 100px; */
  26. }
  27. #box2 {
  28. background: rgb(72, 255, 0);
  29. /* 静态定位 static 默认定位 */
  30. /* position: static; */
  31. /* sticky 粘性定位 不脱离文档流 一般搭配top使用 相当于相对定位和固定定位 一般用于做吸顶效果 */
  32. /* position: sticky;
  33. top: 100px; */
  34. }
  35. #box3 {
  36. background: yellow;
  37. /* relative 相对定位 相对于自己原来位置进行定位 不脱离文档流 */
  38. /* position: relative;
  39. top: 300px;
  40. left: 500px; */
  41. /* absolute 绝对定位 脱离文档流
  42. 父级无定位 相对于整个页面进行定位
  43. 父级有定位 相对于父级进行定位 子绝父相
  44. */
  45. position: absolute;
  46. top: 320;
  47. /* z-index: -2; */
  48. /* left: 500px; */
  49. }
  50. #box4 {
  51. background: purple;
  52. }
  53. #box5 {
  54. background: orange;
  55. }
  56. </style>
  57. </head>
  58. <body>
  59. <!-- 定位:控制元素在页面的摆放位置
  60. position
  61. -->
  62. <div id="box">
  63. <div id="box1"></div>
  64. <div id="box2"></div>
  65. <div id="box3"></div>
  66. <div id="box4"></div>
  67. <div id="box5"></div>
  68. </div>
  69. </body>
  70. </html>