定位2.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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: 400px;
  10. height: 400px;
  11. border: 5px solid black;
  12. position: relative;
  13. /* 设置为相对定位 */
  14. }
  15. .div1 {
  16. position: absolute;
  17. left: 0;
  18. top:0;
  19. background-color: red;
  20. z-index: 1; /* 设置层级 数字越大越在上面 */
  21. }
  22. .div2 {
  23. position: absolute;
  24. right: 100px;
  25. top: 0;
  26. background-color: blue;
  27. z-index: 2; /* 设置层级 数字越大越在上面 */
  28. }
  29. /* 分组选择器 用","间隔*/
  30. .div1,.div2 {
  31. width: 200px;
  32. height: 200px;
  33. /* float: left; */
  34. opacity: 0.5;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <!-- 文档流 -->
  40. <!-- absolute fixed 元素脱离了文档流 他们旁边的元素会忽略特们 他们之前所占用的空间会被释放掉-->
  41. <!-- relative 不会脱离文档流 -->
  42. <div class="box">
  43. <div class="div1"></div>
  44. <div class="div2"></div>
  45. <span>hello world</span>
  46. </div>
  47. </body>
  48. </html>