练习17_垂直居中.html 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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,.box2{
  9. width: 400px;
  10. height: 400px;
  11. border:3px solid black;
  12. float: left;
  13. }
  14. .box1{
  15. text-align: center;
  16. line-height: 400px;
  17. }
  18. .box3{
  19. width: 100px;
  20. height: 100px;
  21. background-color: red;
  22. }
  23. .box4{
  24. margin-top: 150px;
  25. margin-left: 150px;
  26. }
  27. .box6{
  28. position: relative;
  29. }
  30. .box5{
  31. position: absolute;
  32. left: 50%;
  33. top: 50%;
  34. margin-top: -50px;
  35. margin-left: -50px;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div class="box1">
  41. <span>hello</span>
  42. </div>
  43. <div class="box2">
  44. <div class="box3 box4"></div>
  45. </div>
  46. <div class="box2 box6">
  47. <div class="box3 box5"></div>
  48. </div>
  49. </body>
  50. </html>