html - 为什么5rem可以装10个字符?

标签 html css font-size

我一直在玩弄单字体(因为每个字符都和下一个字符一样宽)以匹配输入字段的长度。

为什么 5rem 等于 10 个字符?在 Chrome 和 Firefox 中进入我的输入字段?您希望 10 个字符的宽度为 10rem:

input[type="text"] {
  border: 0;
  outline: 0;
  background: transparent;
  border-bottom: 2px solid black;
  font-family: 'monospace';
  overflow: hidden;
  width: 5rem;
}
<body id="body">
  <form onSubmit="return false">
    <input type="text" placeholder="your name" maxlength="10" required />
    <input type="submit" />
  </form>
</body>

http://codepen.io/alfredwesterveld/pen/RrypPv

最佳答案

你要找的单位是ch:这个单位表示当前字体中字符'0'的宽度。在等宽字体中,1ch 相当于所有字符的宽度。

input {
  border: 0;
  outline: 0;
  background: transparent;
  border-bottom: 2px solid black;
  font-family: monospace;
  width: 9ch;
}
<input type="text" placeholder="your name" maxlength="10" required />

为了回答您的问题,单幅字符的高度通常是其宽度的两倍。所以 1ch ≈ 0.5em5em ≈ 10ch

关于html - 为什么5rem可以装10个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35050986/

相关文章:

javascript - 循环遍历 html 单元格并在满足条件时执行函数

html - Gmail:大于内联 css 样式中定义的大小的元素

javascript - 检测 "powerful"浏览器能够以不错的速度做图形

css - 纯 CSS : Responsive images breaking columns with border-box and padding on Firefox and Opera

css - 切换到移动 View 时我的某些 CSS 不起作用

winforms - 确定给定矩形高度的最大字体(WinForms)

css - 字体大小在 IE9 中根本不起作用

javascript - 插入标记而不丢失 jQuery Mobile 样式

javascript - Modernizr 和 CSS - 不应用此规则的任何原因?

html - 在 HTML5 中,可以在 em 标签中使用强标签吗?