CSS3 box-sizing 属性

语法:

box-sizing:content-box | border-box

默认值:content-box

适用于:所有接受widthheight的元素

继承性:无

取值:

content-box:
padding和border不被包含在定义的width和height之内。对象的实际宽度等于设置的width值和border、padding之和,即 ( Element width = width + border + padding )
此属性表现为标准模式下的盒模型。
border-box:
padding和border被包含在定义的width和height之内。对象的实际宽度就等于设置的width值,即使定义有border和padding也不会改变对象的实际宽度,即 ( Element width = width )
此属性表现为怪异模式下的盒模型。
CSS3 box-sizing 属性

示例:

  • content-box:

    .test1{ box-sizing:content-box; width:200px; padding:10px; border:15px solid #eee; }

    CSS3 box-sizing 属性

  • border-box:

    .test2{ box-sizing:border-box; width:200px; padding:10px; border:15px solid #eee; }

    CSS3 box-sizing 属性

说明:

设置或检索对象的盒模型组成模式。
  • 对应的脚本特性为boxSizing

兼容性:

CSS3 box-sizing 属性

示例:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<title>box-sizing_CSS参考手册_web前端开发参考手册系列</title>
<style>
.test{width:200px;height:70px;padding:10px;border:15px solid #999;-moz-box-sizing:content-box;-ms-box-sizing:content-box;box-sizing:content-box;background:#eee;}
.test2{width:200px;height:70px;padding:10px;border:15px solid #999;-moz-box-sizing:border-box;-ms-box-sizing:border-box;box-sizing:border-box;background:#eee;margin-top:20px;}
</style>
</head>
<body>
<div class="test">content-box</div>
<div class="test2">border-box</div>
</body>
</html>
   

运行结果:

CSS3 box-sizing 属性