关于CSS 2.2规范10.6.2节内联替换元素高度计算的疑问
先把你提到的规范原文贴出来,方便上下文理解:
否则,若‘height’的计算值为‘auto’,且不满足上述任一条件,则‘height’的使用值必须设为满足2:1比例、高度不超过150px、宽度不超过设备宽度的最大矩形的高度。
问题A:若不存在满足2:1比例的最大矩形框,该如何处理?
Great question—this edge case isn't explicitly spelled out in the spec, but we can reason through it using CSS's core layout principles and the intent behind this rule.
First, let's break down what the spec is asking for: it wants the largest possible rectangle that meets three constraints:
- A 2:1 width-to-height aspect ratio (so width = 2 × height, or height = 0.5 × width)
- Height doesn't exceed 150px
- Width doesn't exceed the device's width
The "largest" here refers to the rectangle with the maximum area. Now, when would there be no such "maximum" rectangle? The only realistic scenario is when the available width (either the device width or the container's available space) is 0. In that case, the only rectangle that fits all constraints is a 0×0 box—since any non-zero width would violate the "width ≤ device width" rule (which is 0 here).
In this extreme case, the computed height would be 0. This aligns with CSS's general behavior for elements with no available space: they collapse to zero dimensions rather than trying to render something that can't fit.
Also, it's worth noting that this rule applies specifically to replaced elements (like <img>, <iframe>, <video>) where the intrinsic dimensions aren't available or don't fit the earlier conditions in 10.6.2. For non-replaced elements, this rule doesn't apply at all—so you only hit this edge case in very specific, rare scenarios with replaced elements in zero-width containers.
内容的提问来源于stack exchange,提问作者hobby-coder

