01.绝对定位.html 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <!DOCTYPE html>
  2. <html lang="en">
  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. <style>
  9. /*
  10. 绝对定位
  11. -当元素的position属性值设置为absolute时,则开启了元素的绝对定位
  12. -绝对定位的特点:
  13. 1、开启绝对定位后,如果不设置偏移量,元素的位置不会发生变化
  14. 2、开启绝对定位后,元素会从文档流中脱离
  15. 3、绝对定位会改变元素的性质,行内变成块,块的宽高被内容撑开(没有单独设置宽高的情况)
  16. 4、绝对定位会使元素提升一个层级
  17. 5、绝对定位元素是相对于其包含块进行定位的
  18. 包含块(containing block)
  19. -正常情况下:
  20. 包含块就是离当前元素最近的祖先块元素
  21. <div><div></div></div>
  22. 内层div最近的祖先块元素就是外层的div
  23. <div><span><em>hello</em></span></div>
  24. span的祖先块元素是外层的div,em的祖先块元素也是div
  25. -绝对定位的包含块
  26. 包含块就是离它最近的开启了定位的祖先元素
  27. 如果所有的祖先元素都没有开启定位,则根元素就是它的包含块
  28. -html(根元素,初始包含块)
  29. */
  30. body{
  31. font-size: 50px;
  32. }
  33. .box1{
  34. width: 200px;
  35. height: 200px;
  36. background-color: #bfa;
  37. }
  38. .box2{
  39. width: 200px;
  40. height: 200px;
  41. background-color:orange;
  42. position: absolute;
  43. left: 0;
  44. top: 0;
  45. }
  46. .box3{
  47. width: 200px;
  48. height: 200px;
  49. background-color: yellow;
  50. }
  51. .box4{
  52. width: 400px;
  53. height: 400px;
  54. background-color: tomato;
  55. position: relative;
  56. }
  57. .box5{
  58. width: 300px;
  59. height: 300px;
  60. background-color: aliceblue;
  61. /* position: relative; */
  62. }
  63. </style>
  64. </head>
  65. <body>
  66. <div class="box1">1</div>
  67. <div class="box4">
  68. 4
  69. <div class="box5">
  70. 5
  71. <div class="box2">2</div>
  72. </div>
  73. </div>
  74. <!-- <div class="box3">3</div> -->
  75. </body>
  76. </html>