nz******@gmail.com (Neil Zanella) wrote:
However, it seems I cannot specify an input element with a size of
two, as my web browser keeps making the input element wider than
specified in terms of characters.
Setting width in terms of a number of characters is very vaguely defined,
since in most fonts, the widths of characters vary. A common interpretation
is that the _average_ width should be used, though some browsers use
_maximum_ width - which is generally regarded as a flaw. But "average"
isn't well defined either. Average over what? The Unicode character
repertoire? Some other repertoire? With or without weights based on
frequency of characters? (If you calculate the average over all Ascii
letters, you get something that is much bigger than average width in normal
text, since half of Ascii letters are uppercase and typically fairly wide.)
So there is no good answer. I have even seen browsers that implement
<input size="2"> as a box that cannot even accommodate _one_ letter "W".
If you wish to set the width to give a hint of the expected number of
characters (e.g., when asking for a day of month), setting the font to
monospace is probably the best shot. Not 100% safe of course, but at least
it is fairly well defined what a browser _should_ do, since the maximum
width is the same as (any) average width! (The font is best set using CSS,
though a few old browsers might obey <tt><input ...></tt>.)
--
Yucca,
http://www.cs.tut.fi/~jkorpela/
Pages about Web authoring:
http://www.cs.tut.fi/~jkorpela/www.html