25_定位2.html 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. .box1{
  9. width: 400px;
  10. height: 4000px;
  11. border:3px dashed red;
  12. position: relative;
  13. }
  14. .box2{
  15. width: 200px;
  16. height: 200px;
  17. background-color: blue;
  18. /* absolute 绝对定位 会脱离文档流 会释放掉之前的空间 */
  19. position: absolute;
  20. top:50px;
  21. z-index: 1;
  22. }
  23. /* 定位元素 默认情况下可以覆盖正常文档流中的元素 */
  24. /* 如果两个定位元素重叠 后面的定位元素会覆盖前面的定位元素 */
  25. /* 如果想调整两个元素的覆盖层级 可以使用 z-index 属性 数值越大 覆盖层级越高 */
  26. .box3{
  27. width: 100px;
  28. height: 100px;
  29. background-color: green;
  30. position: absolute;
  31. top:30px;
  32. z-index: 2;
  33. }
  34. /* z-index 属性仅能应用于定位元素 */
  35. span{
  36. background-color: yellow;
  37. font-size: 100px;
  38. z-index: 3;
  39. }
  40. .box4{
  41. font-size: 50px;
  42. /* 固定定位 相较于浏览器 窗口进行定位 */
  43. /* 固定定位 会脱离文档流 会释放掉之前的空间 */
  44. position: fixed;
  45. right: 0;
  46. bottom: 0;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div class="box1">
  52. <div class="box2"></div>
  53. <div class="box3"></div>
  54. <span>这是一个span标签</span>
  55. </div>
  56. <div class="box4">
  57. 返回顶部
  58. </div>
  59. </body>
  60. </html>