简体   繁体   English

当我从px转换为rem单位时,应该如何确定要赋予宽度的rem <input>

[英]When I convert from px to rem units how should I decide how many rems to give the width of an <input>

I have the following: 我有以下内容:

<input maxlength="4" style="width: 50px;" type="text" data-ng-model="pidUpper" />

I would like my input box to hold a maximum of 4 characters. 我希望输入框最多容纳4个字符。 Is there some formula that I should use when deciding on the width if I am using rem units? 如果我使用rem单位,在确定宽度时是否应该使用一些公式?

Unless you use a monospace font you can't really know how wide it should be. 除非您使用等宽字体,否则您将无法真正知道其宽度。

here's a good font stack for monospaced fonts from CSS tricks 这是CSS技巧中的等宽字体的良好字体堆栈

/* Monospace stack */
font-family: Consolas, "Andale Mono WT", "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Liberation Mono", "Nimbus Mono L", Monaco, "Courier New", Courier, monospace;

also you can always tell the length of a string in Javascript if your just looking for validation. 如果您只是想验证,也可以始终用Javascript告诉字符串的长度。 with "string".length "string".length

As for the rems , Here's a great article that explains the differences between em, px, and rem. 至于rems ,这是一篇很棒的文章,解释了em,px和rem之间的区别。 http://snook.ca/archives/html_and_css/font-size-with-rem http://snook.ca/archives/html_and_css/font-size-with-rem

CSS3 introduces a few new units, including the rem unit, which stands for "root em". CSS3引入了一些新的单元,包括rem单元,它代表“ root em”。 If this hasn't put you to sleep yet, then let's look at how rem works. 如果这还没有使您入睡,那么让我们看一下rem的工作原理。 here's a quote 这是一个报价

The em unit is relative to the font-size of the parent, which causes the compounding issue. em单位相对于父级的字体大小,这会导致复合问题。 The rem unit is relative to the root—or the html—element. rem单位是相对于根元素或html元素的。 That means that we can define a single font size on the html element and define all rem units to be a percentage of that. 这意味着我们可以在html元素上定义单个字体大小,并将所有rem单位定义为该百分比。

REM is based on the root HTML, which arguably most of the time is 16 as a base. REM基于根HTML,可以说大多数情况下以16为基础。 If you want 4 max REM characters in a given selection, you would just do your base amount multiplied by the max number. 如果在给定的选择中需要4个最大REM字符,则只需将基本数量乘以最大数量即可。 16 * 4 = 64

You cannot convert from px to rem units any more than you can convert from bananas to apples. px转换为rem单位的数量不能超过从香蕉转换为苹果的数量。 That is, you can only convert if you postulate some specific relationship, like rem being 16px or one banana corresponding to two apples in some particular situation, and then the conversion is trivial. 也就是说,只有在假设某些特定的关系(例如rem16px或一个香蕉,对应于某些特定情况下的两个苹果)时,您才能进行转换,然后转换就变得微不足道了。 And if you assume a fixed relationship, then it's rather pointless to use rem . 而且,如果您假设固定关系,那么使用rem毫无意义。

It is generally impossible to make an element exactly 4 characters wide, since in most fonts, the widths of characters vary (possibly a lot, eg “i” vs. “W” in a typical font). 通常不可能将元素的宽度精确地设置为4个字符,因为在大多数字体中,字符的宽度会发生变化(可能会有很多差异,例如,典型字体中的“ i”与“ W”)。 To make its width the average width of 4 characters, the best attempt is to use just the HTML attribute 为了使其宽度成为4个字符的平均宽度,最好的方法是仅使用HTML属性

size=4

The size attribute of the input element sets the width in “average” characters. input元素size属性以“平均”字符设置宽度。 Implementations vary and are not particularly good in this issue. 实现方式各不相同,在此问题上并不是特别好。

For much more consistent rendering, you can additionally set the font to monospace, eg with font-family: Consolas, monospace . 为了使渲染更加一致,您还可以将字体设置为等宽字体,例如,使用font-family: Consolas, monospace This makes the concept of average-width character well-defined, of course, and browsers have an easier task. 当然,这使得平均宽度字符的概念得到了很好的定义,并且浏览器的任务更加简单。 They might make the field a few pixels too wide, but this should be tolerable. 它们可能会使视场太宽几个像素,但这应该可以接受。 On some very old browsers, the field could be too narrow, but this is difficult to fix without causing other problems. 在某些非常老的浏览器上,该字段可能太窄,但这很难解决而不会引起其他问题。 If the input is code of some kind, like a PID, then a monospace font would probably be just fine. 如果输入是某种类型的代码(例如PID),那么等宽字体可能就可以了。

In the first version of this answer, I suggested the use of the ch unit in CSS. 在此答案的第一个版本中,我建议在CSS中使用ch单元。 I will retain the essential part, as it may be relevant in some contexts where the browsers used are known. 我将保留必不可少的部分,因为在某些已知使用浏览器的情况下,它可能是相关的。 But IE 10 seems to have a horrendously buggy implementation of ch (its value is far too small). 但是IE 10似乎对ch实施了令人毛骨悚然的bug(其值太小了)。

From the first version of the answer: 从第一个版本的答案:

You can use the attributes 您可以使用属性

size=4 style="width: 2.4em; width: 4ch"

The ch unit in CSS denotes the width of the digit zero (0), but it is a fairly good approximation of “average” character width. CSS中的ch单位表示数字零(0)的宽度,但这是“平均”字符宽度的相当不错的近似值。 Since not all browsers support this unit, I have included a fallback using the em unit. 由于并非所有浏览器都支持该单元,因此我使用了em单元作为后备。 It is just a rough guess, based on some experience and experiments, that 2.4em is typically about 4 average characters wide. 根据一些经验和实验,这只是一个粗略的猜测, 2.4em通常大约有4个平均字符宽度。 (The fallback must precede the modern setting, so that the fallback will not override the setting you really want to use.) (回退必须现代设置之前进行,以便回退不会覆盖您真正要使用的设置。)

If the expected input is eg uppercase letters, you may need to consider some tuning here, possibly according to the characteristics of the font that you primarily suggest for the input element. 如果预期的输入是例如大写字母,则可能需要根据主要为input元素建议的字体特征在此处进行一些调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM