博主信息
博文 20
粉丝 1
评论 0
访问量 22625
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
box-sizing属性和常用元素的居中方式
xosing的博客
原创
1007人浏览过

1. box-sizing属性;

属性值 说明
box-sizing: content-box w3c标准盒子模型,width/height 不含padding/border
box-sizing: border-box 盒模型以元素边框区为边界,包含padding、border值.称之为IE盒子模型

1.content-box 示例如下

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4. <title>Document</title>
  5. <style>
  6. :root {
  7. font-size: 10px;
  8. }
  9. .box {
  10. width: 10em;
  11. height: 10em;
  12. background-color: bisque;
  13. border: 3px solid navajowhite;
  14. background-clip: content-box;
  15. padding: .5em;
  16. box-sizing: content-box;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="box"></div>
  22. </body>

1.border-box 示例如下

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4. <title>border-box</title>
  5. <style>
  6. :root {
  7. font-size: 10px;
  8. }
  9. .box {
  10. width: 10em;
  11. height: 10em;
  12. background-color: bisque;
  13. border: 3px solid navajowhite;
  14. background-clip: content-box;
  15. padding: .5em;
  16. box-sizing: border-box;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="box"></div>
  22. </body>

2. 常用的元素居中方式

行内元素水平垂直居中 text-algin:center; 和 line-height: ;

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4. <title>行内元素水平垂直居中</title>
  5. <style>
  6. :root {
  7. font-size: 10px;
  8. }
  9. .box {
  10. width: 20em;
  11. height: 20em;
  12. background-color: bisque;
  13. border: 3px solid navajowhite;
  14. background-clip: content-box;
  15. padding: .5em;
  16. box-sizing: content-box;
  17. }
  18. .box>div{
  19. text-align: center;
  20. line-height: 20em;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div class="box"><div>行内元素水平垂直居中</div></div>
  26. </body>

块元素水平垂直居中 margin position

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4. <title>块元素水平垂直居中</title>
  5. <style>
  6. :root {
  7. font-size: 10px;
  8. }
  9. .box {
  10. width: 20em;
  11. height: 20em;
  12. background-color: bisque;
  13. border: 3px solid navajowhite;
  14. position: relative;
  15. }
  16. .box>div{
  17. width: 10em;
  18. height: 10em;
  19. background-color: olive;
  20. position: absolute;
  21. top: 0;
  22. left: 0;
  23. right: 0;
  24. bottom: 0;
  25. margin: auto;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div class="box"><div>块元素水平垂直居中</div></div>
  31. </body>

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学