当填充大于定义的尺寸时会发生什么?

发布于 2025-02-13 10:27:28 字数 370 浏览 1 评论 0原文

<html>
<head>
<style>
* {
  box-sizing: border-box;
}
div {
  width: 10px;
  padding: 70px;
  border: 1px solid #4CAF50;
}
</style>
</head>
<body>

<h2>CSS Padding</h2>
<div>This element has a padding of 70px.</div>

</body>
</html>

我尝试了这个,但不明白结果。当填充大于给定宽度时,到底会发生什么?

<html>
<head>
<style>
* {
  box-sizing: border-box;
}
div {
  width: 10px;
  padding: 70px;
  border: 1px solid #4CAF50;
}
</style>
</head>
<body>

<h2>CSS Padding</h2>
<div>This element has a padding of 70px.</div>

</body>
</html>

I tried this but don't understand the result. What exactly happens when the padding is bigger than the given width?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ偶尔ゞ 2025-02-20 10:27:29

您的元素的宽度等于70px*2 + 1px*2 = 142px。填充和边框的总和大于指定的宽度,因此就像您的宽度等于0,并且只有填充/边框定义 final 宽度

也就是说,在该指定的宽度和高度内布置并绘制元素上指定的任何填充或边框。内容宽度和高度通过减去从指定的宽度和高度特性中的边界和填充宽度。由于内容宽度和高度不能为负([CSS2],第10.2节),此计算位于0 ref

您可以清楚地看到为什么 content 宽度将等于0,最终宽度仅是填充和边框。

为了使用数学表达这一点,您有以下公式:

content-width + padding + border = final-width

content-width = max(0,specified-width - (padding + border))

正在使用max()来表达我们仅占据正值的事实。

进行计算,您将获得142px

如果指定的宽度 - (Padding + Border)是正面的,您将获得指定宽度=最终的逻辑结果-Width

Your element will have a width equal to 70px*2 + 1px*2 = 142px. The sum of the padding and border is bigger than the specified width so it's like you have width equal to 0 and only the padding/border are defining the final width

That is, any padding or border specified on the element is laid out and drawn inside this specified width and height. The content width and height are calculated by subtracting the border and padding widths of the respective sides from the specified width and height properties. As the content width and height cannot be negative ([CSS2], section 10.2), this computation is floored at 0 ref

You can clearly see why the content width will end being equal to 0 and the final width is only the padding and border.

To express this using math, you have the following formula:

content-width + padding + border = final-width

And

content-width = max(0,specified-width - (padding + border))

I am using max() to express the fact that we take only positive value.

Do the calculation and you will get 142px

If specified-width - (padding + border) is positive, you will get the logical result of specified-width = final-width

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文