leiyanting

导航

 

box-sizing 属性用于更改用于计算元素宽度和高度的默认的 CSS 盒子模型。可以使用此属性来模拟不正确支持CSS盒子模型规范的浏览器的行/列为。
 
默认值:content-box  不可继承
 

    content-box
       默认值,标准盒子模型。 width 与 height 只包括内容的宽和高, 不包括边框(border),内边距(padding),外边距(margin)。注意: 内边距, 边框 & 外边距 都在这个盒子的外部。 比如. 如果 .box {width: 350px}; 而且 {border: 10px solid black;} 那么在浏览器中的渲染的实际宽度将是370px;
       尺寸计算公式:
           width = 内容的宽度,
           height = 内容的高度。
           宽度和高度都不包含内容的边框(border)和内边距(padding)。
 
    border-box
       width 和 height 属性包括内容,内边距和边框,但不包括外边距。这是当文档处于 Quirks模式 时Internet Explorer使用的盒模型。
 
       这里的维度计算为:
           width = border + padding + 内容的  width,
           height = border + padding + 内容的 height。

 

开发时常用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 200px;
            height: 45px;
            box-sizing: border-box;
            padding: 0 50px;
        }
    </style>
</head>
<body>
    <input type="text" class="box">
</body>
</html>

相当于是padding向内收缩挤压内容区,当ui给的标注是box内容区加padding值得使用很有必要使用box-sizing

 

posted on 2021-07-06 10:48  leiyanting  阅读(161)  评论(0)    收藏  举报