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

box-sizing属性**

content-box :border、padding 的设置会破坏元素宽高,必须得重新计算;
实例演示

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>box-sizing属性</title>
  7. <style type="text/css">
  8. .box {
  9. width: 200px;
  10. height: 200px;
  11. text-align: center;
  12. border: 10px solid black;
  13. padding: 15px;
  14. }
  15. </style>
  16. </style>
  17. </head>
  18. <body>
  19. <div class="box"></div>
  20. </body>
  21. </html>

border-box :border、padding 的设置不会影响元素的宽高;
实例演示

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>box-sizing属性</title>
  7. <style type="text/css">
  8. .box {
  9. width: 200px;
  10. height: 200px;
  11. text-align: center;
  12. border: 10px solid black;
  13. padding: 15px;
  14. box-sizing: border-box;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="box"></div>
  20. </body>
  21. </html>

常用元素居中方式:

1.行级元素:水平居中使用:text-align:center,垂直居中:line-height:父元素的高度
2.块级元素:水平居中:margin:0 auto;

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>常用元素居中方法</title>
  6. <style>
  7. *{
  8. font-size: 16px;
  9. box-sizing: border-box;
  10. }
  11. .box {
  12. margin: 0 auto;
  13. height: 10em;
  14. width: 10em;
  15. background-color: violet;
  16. }
  17. .title {
  18. line-height: 10em;
  19. text-align: center;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="box">
  25. <p class="title">我在中间</p>
  26. </div>
  27. </body>
  28. </html>
批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系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+教程免费学