定位.html 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. .div1{
  15. width: 100px;
  16. height: 100px;
  17. background-color: red;
  18. /* margin-top: -20px; 向上移动20px 负数向相反方向移动*/
  19. /* 相对定位 相对于原来的位置 */
  20. /*position: relative; 设置为相对定位 */
  21. /* top: 300px;
  22. left:300px; */
  23. /* 相对于最近的已定位祖先元素 */
  24. /* absolute 定位 需要满足两个条件 */
  25. /* 第一个必须是他的祖先元素 */
  26. /* 第二个必须使用定位 */
  27. position: absolute; /* 设置为绝对定位 */
  28. right: 20px;
  29. bottom: 0;
  30. }
  31. .div2{
  32. width: 100px;
  33. height: 100px;
  34. background-color: blue;
  35. /* 相对于浏览器窗口 */
  36. position: fixed; /* 设置为固定定位 */
  37. right: 50px;
  38. bottom: 50px;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <div class="box">
  44. <div class="div1"></div>
  45. </div>
  46. <div class="box">
  47. <div class="div1"></div>
  48. </div>
  49. <div class="box">
  50. <div class="div1"></div>
  51. </div>
  52. <div class="box">
  53. <div class="div1"></div>
  54. </div>
  55. <div class="box">
  56. <div class="div1"></div>
  57. </div>
  58. <div class="box">
  59. <div class="div1"></div>
  60. </div>
  61. <div class="div2"></div>
  62. </body>
  63. </html>