Labeling Input Fields

As noted earlier in the chapter, it's up to you as a form author to include prompting text in front of your form fields to suggest to a user how he or she should fill in the field. The XHTML <label> element formalizes the relationship between the prompting text (the label) and the form field it is paired with. <label> takes the for attribute, where for is set equal to the id attribute value of the associated form field. In the example

<label for="Zip">Enter your 9-digit ZIP code: </label>
<input type="text" name="ZipCode" id="Zip" />

the prompting text "Enter your 9-digit ZIP code:" composes the label. Note how the label is associated with the subsequent field with the matching for and id attributes.

How a label is rendered ...

Get Platinum Edition Using XHTML™, XML, and Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.