HTML and CSS Reference
In-Depth Information
in the field. If
maxlength
exceeds
size
, the text scrolls back and forth
within the text-entry box. If
maxlength
is smaller than
size
, there is ex-
tra blank space in the text-entry box to make up the difference between
the two attributes.
The default value for
size
depends on the browser, but typically it is 80
characters; the default value for
maxlength
is unlimited. We recommend
that you set them yourself. Adjust the
size
attribute so that the text-
entry box does not extend beyond the right margin of a typical browser
window (about 60 characters with a very short prompt). Set
maxlength
to a reasonable number of characters; for example, two for state abbre-
viations, 12 for phone numbers, and so on.
A text-entry field is usually blank until the user types something into it.
You may, however, specify an initial default value for the field with the
value
attribute. The user may modify the default, of course. If the user
presses a form's Reset button, the value of the field is reset to this de-
fault value. [
Reset buttons, 9.5.4.2
]
All of these are valid text-entry form controls:
<input type=text name=comments>
<input type=text name=zipcode size=10 maxlength=10>
<input type="text" name="address" size="30" maxlength="256" />
<input type="text" name="rate" size="3" maxlength="3" value="100" />
The first example is HTML and creates a text-entry field set to the
browser's default width and maximum length. As we argued, this is not
a good idea, because defaults may vary among browsers, and your form
layout is sure to look bad with some of them. Rather, fix the width and
maximum number of acceptable input characters as we do in the second
example: it lets the user type in up to 10 characters inside an input box
10 characters wide. Its value is sent to the server with the name
zipcode
when the user submits the form.
The third example is XHTML and tells the browser to display a text-input
box 30 characters wide into which the user may type up to 256 charac-