Blogger Information
Blog 19
fans 0
comment 6
visits 19095
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CSS中box-sizing用法实例,常用元素垂直左右居中方式演示
葵花宝典
Original
677 people have browsed it

box-sizing 用法实例,常用元素居中方式演示

  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>CSS中box-sizing属性演示,CSS中元素距中方式</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. }
  12. h2 {
  13. /* 行内元素和文本距中用text-align:center */
  14. text-align: center;
  15. border: 1px solid red;
  16. margin: 1em;
  17. }
  18. p {
  19. /* P元素设置了行高 */
  20. height: 2.5em;
  21. text-align: center;
  22. font-size: 1.5em;
  23. border: 1px solid red;
  24. margin: 1em;
  25. }
  26. span {
  27. /* P元素下的子元素,要垂直距中,行高要设置和父元素P一样的高度 */
  28. line-height: 2.5em;
  29. }
  30. div {
  31. width: 200px;
  32. height: 200px;
  33. border: 5px solid red;
  34. background-color: sandybrown;
  35. }
  36. body div:first-of-type {
  37. /* 块元素距中用 margin: 0 auto,意为将左或设置为自动,两边自动挤压后距中 */
  38. margin: 1em auto;
  39. box-sizing: content-box;
  40. }
  41. body div:first-of-type + div {
  42. margin: 1em auto;
  43. box-sizing: border-box;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <h2>box-sizing属性说明</h2>
  49. <p><span>box-sizing属性</span></p>
  50. <div>
  51. 此box宽200,高200,box-sizing=content-box时,盒子大小为:boder+padding+width
  52. </div>
  53. <div>box-sizing=border-box时,盒子大小为:width(盒子自身大小)</div>
  54. </body>
  55. </html>

效果图示

盒子大小带边框和内距
盒子大小不带边框和内距
行内元素垂直距中

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
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