在css3中,box-sizing提供了两种选择,一个是content-box,一个是border-box。
content-box的模式里,元素设定的宽和高为内容宽高;而border-box中,元素设定的宽高为整体元素呈现出来的宽高。
使用第二种模式就省去了计算,能够十分简单地,不需计算内外边距而设定好整个元素应该占多少宽高
<style>
div {
width: 100px;
height: 100px;
background-color: skyblue;
padding: 15px;
margin: 15px;
border: 1px solid deepskyblue;
}
div:first-child{
box-sizing: content-box;
}
div:last-child{
box-sizing: border-box;
}
</style>
<title>Document</title>
</head>
<body>
<div>content-box</div>
<div>border-box</div>
</body>