05.元素的层级.html 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. 对于开启了定位的元素,可以通过z-index属性来指定元素的层级
  11. z-index需要一个整数作为参数,值越大元素的层级越高
  12. 元素的层级越高越优先显示
  13. 如果元素的层级一样,则优先显示靠下的元素
  14. 祖先元素的层级再高,也不会盖住后代元素
  15. */
  16. body{
  17. font-size: 50px;
  18. }
  19. .box1{
  20. width: 200px;
  21. height: 200px;
  22. background-color: #bfa;
  23. position: absolute;
  24. /* z-index: 1; */
  25. }
  26. .box2{
  27. width: 200px;
  28. height: 200px;
  29. background-color:rgba(255,0,0,.3);
  30. position: absolute;
  31. top: 50px;
  32. left: 50px;
  33. /* z-index: 2; */
  34. }
  35. .box3{
  36. width: 200px;
  37. height: 200px;
  38. background-color: yellow;
  39. position: absolute;
  40. top: 100px;
  41. left: 100px;
  42. z-index: 3;
  43. }
  44. .box4{
  45. width: 100px;
  46. height: 100px;
  47. background-color: orange;
  48. position: absolute;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <div class="box1">1</div>
  54. <div class="box2">2</div>
  55. <div class="box3">3
  56. <div class="box4">4</div>
  57. </div>
  58. </body>
  59. </html>