【发布时间】:2010-11-18 07:45:41
【问题描述】:
我研究了一段时间,还没有找到 CSS 解决方案,em 和 ex 单位在这种情况下不正确。我想要的只是一个完全适合 80x25 等宽文本的 div 框。我必须求助于 Javascript 解决方案吗?
【问题讨论】:
标签: javascript html css
我研究了一段时间,还没有找到 CSS 解决方案,em 和 ex 单位在这种情况下不正确。我想要的只是一个完全适合 80x25 等宽文本的 div 框。我必须求助于 Javascript 解决方案吗?
【问题讨论】:
标签: javascript html css
尝试使用ch 描述的in the CSS3 specification 单元:
等于在用于呈现它的字体中找到的“0”(零,U+0030)字形的使用提前度量。
因此,width: 80ch 指定了 80 个字符的宽度限制。
【讨论】:
ch 单元是一个相对较新的东西(甚至没有一个 caniuse 矩阵),这就是为什么上面的旧答案是公认的。
ch 在 IE11 上仍然无法正常工作。我希望能够使用它,因为它正是所需的解决方案,但它根本无法在任何地方工作。
em 在这种情况下确实有效,如果您知道与您的字体相关的正确比例。试试下面的 HTML:
(这样做的危险在于某些浏览器会调整字体,这可能会改变字体的宽度/高度。为了 100% 的准确性,您可能必须使用 JavaScript。)
<style type="text/css">
#text-container {
border: #f00 solid 1px;
font: 10px/1.2 Courier, monospace;
width: 48em; /* 80 x 0.6 (the width/height ratio of Courier) */
height: 30em; /* 25 x 1.2 (line-height is 1.2) */
overflow: hidden;
}
</style>
<div id="text-container">
00000000001111111111222222222233333333334444444444555555555566666666667777777777
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
</div>
【讨论】:
高度你可以使用em,但你也必须设置line-height:
height: 25em; line-height: 1em;
或者多一点间距:
height: 30em; line-height: 1.2em;
字符宽度没有CSS单元,所以恐怕你需要一些Javascipt...
【讨论】:
em、ex 和ch 在用于高度属性时被解释为字符高度,而在宽度属性中被解释为宽度。
em 和 ex 是高度,ch 是宽度。所以这里真正的答案是你可以使用ch 像em 来设置宽度。
ch 是一个相对较新的属性,在 2009 年编写上述内容时实际上并不存在。)
Blixt 的回答是正确的,但如果它使数学更容易,您也可以这样做:
font-family: Courier;
font-size: 0.57em;
line-height: 1.2em;
width: 80em;
height: 30em; /* 1.2 * 25; you could set the line-height at 1em and the box height at 25,
but that would squish things too much */
【讨论】: