box-sizing属性值的区别

box-sizing有2种属性值:content-boxborder-box,其中默认值为content-box
content-box:指定高度和宽度时,此时 包括padding和border。
border-box:指定高度和宽度时,此时包括padding和border。
html代码:

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width">
 <title>JS Bin</title>
</head>
<body>
 <div class="box">
   <div class="box1">
   </div>
   <div class="box2">
   </div>
 </div>
</body>
</html>

CSS 代码:

.box {
  margin:0;
  font-size:0;
}

.box1{
  vertical-align:middle;
  display:inline-block;
  background-color:red;
  width:100px;
  height:200px;
  padding:30px;
  border:5px solid black;
}

.box2{
  vertical-align:middle;
  display:inline-block;
  background-color:blue;
  width:100px;
  height:200px;
  box-sizing:border-box;
  padding:30px;
  border:5px solid black;
}

output:

border-box&content-box
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,681评论 1 92
  • H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解dis...
    Mx勇阅读 10,114评论 0 26
  • 移动开发基本知识点 一.使用rem作为单位 html { font-size: 100px; } @media(m...
    横冲直撞666阅读 8,881评论 0 6
  • 阅读目录移动开发基本知识点 calc基本用法box-sizing的理解及使用理解display:box的布局理解f...
    张宪宇阅读 5,468评论 0 1
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 6,835评论 0 11