盒子模型为border-box的时候,特定情况下内容宽度为0,宽度不是预想中的设置的width

我好像在面试中只被问过一两次,有这样一道题:介绍一下CSS的盒子模型

CSS的盒子模型有哪些:

标准盒子模型、IE盒子模型

CSS的盒子模型区别:

        标准盒子模型:margin、border、padding、content

        IE盒子模型:margin、content(border+padding+content)

通过CSS如何转换盒子模型:

CSS3中的box-sizing 属性允许以特定的方式来指定盒模型,现代浏览器和IE9+默认值是content-box。
box-sizing:content-box;/*标准盒子模型*/

box-sizing:border-box;/*IE盒子模型*/

border-box的时候,特定情况下内容宽度为0,宽度不是预想中的设置的width:

复现步骤:

创建一个html文件,查看下面的li元素大小。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            overflow: hidden;
            list-style: none;
        }

        .u1 li{
            float:left;
            margin:10px;
            padding: 50px;
            width: 100px;
            height: 100px;
            border: 5px solid #000;
        }
        

        .gap {
            height: 20px;
        }

        .u2 li{
            float:left;
            box-sizing: border-box;
            margin:10px;
            padding: 50px;
            width: 50px;
            max-width: 50px;
            height: 100px;
            border: 5px solid #000;
        }
    </style>
</head>

<body>
    <ul class="u1">
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <div class="gap"></div>
    <!-- <hr> -->
    <ul class="u2">
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
</body>

</html>

效果图:

我在上面中设置的width是50px,但是这里展示的是110px,这里就有问题了,不符合我们平常的计算方式,这说明有其他标准在影响着计算。

查阅MDN:

border-boxicon-default.png?t=N7T8https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing#border-box

The width and height properties include the content, padding, and border, but do not include the margin. Note that padding and border will be inside of the box. For example, .box {width: 350px; border: 10px solid black;} renders a box that is 350px wide, with the area for content being 330px wide. The content box can't be negative and is floored to 0, making it impossible to use border-box to make the element disappear.
widthheight属性包括内容、内边距和边框,但不包括边距。请注意,填充和边框将位于框内。例如, .box {width: 350px; border: 10px solid black;}渲染一个 350px 宽的框,内容区域为 330px 宽。内容框不能为负数,并且被下限为0,因此无法使用border-box使元素消失。

 这里的描述还是不够清楚,为什么内容框下限为0呢?当下限为0的时候,宽度怎么计算呢。继续查阅。

查阅思否:

有这样一段回答: 

这是一个特殊情况,当你的 最小宽度大于 你设置的宽度的时候,容器会使用 最小宽度。

好,那么这个是从哪里来的呢?最小宽度是如何定义的呢?

显然需要继续寻找。

查阅w3c: 

CSS Basic User Interface Module Level 3 (CSS3 UI)icon-default.png?t=N7T8https://www.w3.org/TR/css-ui-3/#box-sizing在这里有关于最小宽度的描述,可以解决这个问题。

 

这里是content宽度最小为0的这个解释。

Visual formatting model detailsicon-default.png?t=N7T8https://www.w3.org/TR/CSS21/visudet.html#Computing_widths_and_margins

If the resulting width is smaller than 'min-width', the rules above are applied again, but this time using the value of 'min-width' as the computed value for 'width'.
如果结果宽度小于'min-width' ,则再次应用上述规则,但这次使用'min-width'的值作为'width'的计算值。

这是最小宽度和结果宽度的影响。

最后:

box-sizing:border-box的时候:最小宽度=padding+border。

但是没找到文档,希望有同学可以给予补充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值