HTML form tags
The tags you need to know about when creating a form
There are quite a few specialist tags for forms. What follows is an attempt to cover the most commonly used ones, highlighting where necessary their potential to be customised for a particular use.
Every good form should have a structure not unlike the one about to be explained and illustrated. Form tags exist for a reason so we should always make sure we use them, and use them well.
Note: the <input /> tag is self-closing.
- <form action="">
- <fieldset>
- <legend>Your details</legend>
- <label for="firstname">First name</label>
- <input type="text" id="firstname" />
- <label for="lastname">Last name</label>
- <input type="text" id="lastname" />
- <label for="agerange">Age range</label>
- <select id="agerange">
- <option>16-21</option>
- <option>22-34</option>
- <option>35-45</option>
- <option>46 and over</option>
- </select>
- <label for="thoughts">Tell us what you think</label>
- <textarea id="thoughts"></textarea>
- <input type="submit" value="Submit" />
- </fieldset>
- </form>
Forms. They've got form.
- Every <form> tag needs the attribute action. It's only essential when you want the form to actually work. When you hand over the HTML the developer will know what to do with it.
- Every form should have a <fieldset> or group of fieldsets. These enable us to group the form into meaningful sections.
- Every fieldset should have a <legend>. This acts as the title of the fieldset, enabling you to tell the customer what the section is about. You're more than welcome to write a <p>-tags worth of extra copy after the legend if you feel it adds useful information.
- Every <input /> (except those of type="submit"), <select> and <textarea> should have a <label>. The label contains the words that describe what the input box or selection drop-down means. Without it, customers won't know what they're being asked to fill in.
- Every form needs a <input type="submit" value="Submit" />. A customer needs to be able to send the information they've entered somewhere, so you'll need to make sure this is present and styled appropriately.
That form in the visible spectrum (with no style)
Adaptable inputs
The <input /> tag is pretty versatile and can be attributed in a few different ways to offer alternative function.
Use the type="" attribute in the following ways:
Text input
- <input type="text" />
Radio button
- <input type="radio" />
Checkbox
- <input type="checkbox" />
The select tag
This form tag enables you to compose a list (like a list of countries of the world), kept in a drop down menu. Its functionality is the same as an <input> tag attributed with a type of radio. This effectively means you can only choose one of the available options. The tag must be constructed like so:
- <select>
- <option>Denmark</option>
- <option>Hungary</option>
- <option>Peru</option>
- </select>
The textarea
The <textarea> tag allows us to provide a free text field (i.e. one that enables customers to write anything they want). This can be useful if you're asking customers to answer a question.
You can set attributes for it that govern how many rows and columns it will take up. You are still able to use CSS to control its width and height, though. Here's the markup:
- <textarea rows="5" columns="10"></textarea>