gpt4 book ai didi

css - 为什么输入字段会使用字体样式 :italic? 减小宽度

转载 作者:行者123 更新时间:2023-11-28 14:10:50 24 4
gpt4 key购买 nike

当将 font-style: italic; 应用于 HTML input 标签时,该字段比其他情况下更窄(计算宽度更小)。

最小可重现示例:

<!DOCTYPE html>
<html lang="en">
<body>
<input type="text" /><br>
<input type="text" style = "font-style: italic;" /><br />
</body>
</html>

当另存为 HTML 并在 Chrome 中呈现时,它看起来像这样: Two input boxes, with the bottom one less wide.
在其他浏览器中也是类似的。

注意:当上述 HTML 作为代码片段在 Stack Overflow 上运行时,此问题不可见。

这是为什么?
一个比另一个窄的因素在浏览器中是否恒定?

最佳答案

这是HTML5规范的效果。

在渲染部分14.5.5. The input element as a text entry widget.它说:

If an input element whose type attribute is [text] does not have a size attribute, then the user agent is expected to act as if it had a user-agent-level style sheet rule setting the 'width' property on the element to the value obtained from applying the converting a character width to pixels algorithm to the number 20.

The converting a character width to pixels algorithm returns (size-1)×avg + max, where size is the character width to convert, avg is the average character width of the primary font for the element for which the algorithm is being run, in pixels, and max is the maximum character width of that same font, also in pixels. (The element's 'letter-spacing' property does not affect the result.)

那个计算意味着输入框的宽度取决于字符的字体。 Chrome 将斜体作为与普通字符不同的字体,并应用不同的平均值,因此总宽度也不同。

您可以通过更改输入元素的字体系列来进一步了解这一点。在某些字体中,斜体版本导致方框变长,而在其他字体中,方框变短。

Firefox 和 Edge 目前没有对斜体使用不同的平均值。

关于css - 为什么输入字段会使用字体样式 :italic? 减小宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56499004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com