css - 将最大高度与边框框一起使用

标签 css

为什么 max-height 没有应用到 this example 中的盒子上?似乎忽略了边框模式(在 Chrome 上测试过),但这似乎违反直觉。

最佳答案

box-sizing 属性不会被忽略,这正是 border-box 值应该如何表现:

顶部和底部的内边距设置为 100 像素,因此元素的 高度 的 200 像素被内边距占用

如果指定 height 为 200px,则计算出的高度将为 0,因为 200 - 200 为 0。

如果指定 height 为 201px,则计算出的高度将为 1,依此类推。

来自box-sizing文档:

The content width and height are calculated by subtracting the padding widths of the respective sides from the specified ‘width’ and ‘height’ properties. As the content width and height cannot be negative, this computation is floored at 0.

使用边框而不是填充很容易证明这一点:

#test {
  background: #000;
  border-width: 100px 0;
  border-style: solid;
  border-color: red;
  height: 200px;
  box-sizing: border-box;
}

这里我们的元素有黑色背景和红色边框,但是由于它的高度等于顶部和底部边框宽度的总和,因此元素最终计算为 0px:

Example 1

如您所见,盒子完全是红色的。该元素没有高度,因此看不到黑色背景。如果我们将元素的 height 调整为 250px,我们最终会得到:

Example 2

这里元素的计算高度是 50px,所以我们看到 50px 的背景。剩余的 200px 被边框占用。

关于css - 将最大高度与边框框一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19834948/

相关文章:

html - 如果我使用 "cursive"或 "fantasy"字体系列,表情符号仅显示在 Chrome 上

html - Twitter Bootstrap ProgressBar 左右两侧带有徽章

html - css max-width 属性和外观

javascript - 检测输入的搜索栏

css - 我有一个 css 样式,但对齐方式在 chrome 中是固定的,但在 firefox 中不是

java - 有没有办法获取浏览器生成的 HTML 和 CSS

html - 背景图片不可见

html - CSS 动画移动位置与变换

asp.net - 通过asp.net控件修改css显示

html - Bootstrap 4,根据我们在第一列上滚动的位置更改第二列内容