Blogger Information
Blog 6
fans 0
comment 0
visits 3144
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
传统定位和flex定位
深海
Original
606 people have browsed it
  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. .box {
  10. width: 15em;
  11. height: 15em;
  12. background-color: crimson;
  13. position: relative;
  14. }
  15. .box .sun {
  16. width: 5em;
  17. height: 5em;
  18. background-color: darkblue;
  19. position: absolute; /**绝对定位*/
  20. top: 0;
  21. left: 0;
  22. right: 0;
  23. bottom: 0; /**top left right bottom 等于0让盒子居中*/
  24. margin: auto; /*这一行才做了sun 元素再box中的居中效果,没有的话无效果*/
  25. }
  26. .box1 {
  27. width: 15em;
  28. height: 15em;
  29. background-color: darkorange;
  30. display: flex;
  31. justify-content: center;
  32. align-items: center;
  33. /** display: flex;justify-content: center; align-items: center; 完成了利用flex居中效果*/
  34. }
  35. .box1 .sun1 {
  36. width: 5em;
  37. height: 5em;
  38. background-color: darkslateblue;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <h2>传统居中</h2>
  44. <div class="box">
  45. <div class="sun"></div>
  46. </div>
  47. <h2>flex居中</h2>
  48. <div class="box1">
  49. <div class="sun1"></div>
  50. </div>
  51. </body>
  52. </html>

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post