Blogger Information
Blog 64
fans 2
comment 1
visits 46825
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
box-sizing属性、 实例演示常用的元素居中方式
Y的博客
Original
677 people have browsed it

实例演示box-sizing属性

  • box-sizing: border-box; 可以把内边距和边框都控制在盒子内
    1. html {
    2. font-size: 10px;
    3. }
    4. /* 盒子的宽度是100px,高度是100px */
    5. .box {
    6. width:10em;
    7. height: 10em;
    8. background-color: aqua;
    9. }
    10. /* 盒子的宽度和高度是100px+10px*2+1*2=122px */
    11. .box {
    12. border: 1px solid #9c1d1d;
    13. padding: 1em;
    14. background-clip: content-box;
    15. }

  1. /* 用box-sizing: border-box; 可以把内边距和边框都控制在盒子内,宽度还是为100px */
  2. .box {
  3. box-sizing: border-box;
  4. }

实例演示常用的元素居中方式

  1. 行内元素
  • 水平居中,使用text-align:center
  • 垂直居中,使用line-height:200px(子元素设置行高与父元素高度相同)
    1. .box {
    2. width: 200px;
    3. height: 200px;
    4. background-color: lightgreen;
    5. }
    6. /* 1.行内元素*/
    7. /* 水平居中,使用text-align:center */
    8. /* 垂直居中,使用line-height:200px(200px为盒子高度必须和盒子的高度一致才能居中) */
    9. .box {
    10. text-align: center;
    11. line-height: 200px;
    12. }

2.块元素

  • 水平居中,使用margin: auto;
  • 垂直居中,使用padding:50px;上下挤压50px
    1. .box {
    2. width: 200px;
    3. /* 不要给高度高度由padding挤出来 */
    4. /* height: 200px; */
    5. background-color: lightgreen;
    6. box-sizing: border-box;
    7. }
    8. /* 2.块元素*/
    9. /* 水平居中,使用margin: auto; */
    10. /* 垂直居中,使用padding:50px;上下挤压50px */
    11. .box>div {
    12. width: 100px;
    13. height: 100px;
    14. background-color: rgb(14, 46, 223)
    15. }
    16. .box {
    17. padding: 50px;
    18. }
    19. .box>div{
    20. margin: auto;
    21. }

3.用padding水平垂直居中

  1. .box {
  2. width: 200px;
  3. /* height: 200px; */
  4. background-color: lightgreen;
  5. box-sizing: border-box;
  6. }
  7. .box>div {
  8. width: 100px;
  9. height: 100px;
  10. background-color: rgb(14, 46, 223)
  11. }
  12. .box {
  13. padding: 50px;
  14. }

4.用margin水平垂直居中

  1. .box {
  2. width: 200px;
  3. height: 200px;
  4. background-color: lightgreen;
  5. box-sizing: border-box;
  6. }
  7. .box>div {
  8. width: 100px;
  9. height: 100px;
  10. background-color: rgb(14, 46, 223)
  11. }
  12. .box {
  13. position: relative;
  14. }
  15. .box>div {
  16. position: absolute;
  17. top: 0;
  18. left: 0;
  19. right: 0;
  20. bottom: 0;
  21. margin: auto;
  22. }
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!