Blogger Information
Blog 18
fans 0
comment 0
visits 15943
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
box-sizing和常用的居中方式
沉寂的博客
Original
862 people have browsed it

box-sizing和常用的居中方式

box-sizing是为了解决padding和border改变盒子本身大小而出现的属性,它有两个值
1.content-box 与之前的标准盒子一样;
box-sizing:content-box;
2.border-box 俗称IE盒子,加入padding和border不会影响本身盒子的大小。
box-sizing:border-box;
常用的居中方式有以下几种:
1.行内元素垂直和水平居中
text-align:center;
line-height:20em(行高);
2.块元素水平和垂直居中
margin:0 auto;
padding-top:5em(父级行高-自己行高/2)
具体代码如下所示:

  1. :root {
  2. font-size: 10px;
  3. margin: 0;
  4. padding: 0;
  5. }
  6. .box {
  7. box-sizing: border-box;
  8. width: 20em;
  9. height: 20em;
  10. border: solid 1px;
  11. background-color: chocolate;
  12. color: white;
  13. text-align: center;
  14. line-height: 20em;
  15. /* 利用padding和text-align实现水平和垂直居中 */
  16. /* padding-top: 10em; */
  17. }
  18. .box1 {
  19. /* box-sizing: border-box; */
  20. width: 20em;
  21. height: 20em;
  22. border: solid 1px;
  23. background-color: chocolate;
  24. color: white;
  25. /* 利用padding和text-align实现水平和垂直居中 */
  26. /* padding-top: 20em; */
  27. padding-top: 5em;
  28. box-sizing: border-box;
  29. }
  30. .box2 {
  31. margin: 0 auto;
  32. line-height: 20em;
  33. /* box-sizing: border-box; */
  34. width: 10em;
  35. height: 10em;
  36. border: solid 1px;
  37. background-color: chocolate;
  38. /* color: white; */
  39. /* 利用padding和text-align实现水平和垂直居中 */
  40. }
  1. <div class="box1">
  2. <div class="box2"></div>
  3. </div>
  4. <div class="box">
  5. <span>php.cn</span>
  6. </div>

执行结果:
box-sizing和居中

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!