Forms provide a way for you to get feedback from your site visitors, perhaps as a contact form or a comment form on your blog posts. They’re a more advanced part of your site but very useful to have.
A form can consist of text inputs, checkboxes, radio buttons, select lists, textareas, hidden inputs and file uploads, all of which will be explained next week. This week I’m going to explain the basic layout/markup of a form.
The standard markup of a form consists of the following HTML tags:
The form tag surrounds the entire form. It has at least 2 required attributes and a number of optional ones, the more common are listed below:
A popular but deprecated attribute for the form tag is ‘name’. This is still very commonly used however instead of name, the id attribute should be used.
An example form markup is:
<form id="formname" method="post" action="form-process.php"> ... </form>
The fieldset tag logically groups together specific field elements, for example, a form asking for personal details and then comments could have two fieldsets, one to group the personal details together, and the second to group the comments section together.
By default a fieldset will draw a box around the grouped elements. Fieldsets can also be nested within each other.
This defines a title for the fieldset and must be used within a fieldset, right after the opening fieldset tag, eg.
<form id="formname" method="post" action="form-process.php">
<fieldset><legend>Personal Details</legend>
....
</fieldset>
<fieldset><legend>Comments</legend>
....
</fieldset>
</form>
The legend appears within a gap in the fieldset box/border, and is usually bold by default.
Every form element, except buttons, should have a label associated with it. A label contains the text relating to the element. There are two ways to connect the label and the element; you can wrap the text and the field element within the label tag, however the more ideal way is to use the for attribute. The value of the for attribute should be equal to the value of the id attribute on the field element eg.
<label for="firstname">Firstname:</label> <input type="text" id="firstname" name="firstname" />
By default the label tag has no styling associated with it (of course this can be targeted in the CSS). The purpose of the label tag is to connect the text label to the input, and from an accessibility and usability point of view, if the user clicks on the label text it will insert the cursor into the field, check a checkbox or radio button, or select the select list (for further use with the arrow keys).
As mentioned, you can also just wrap a field element and its text within the label tag, which is fine for most of the field elements, however this can stop select lists from working in certain browsers, so it’s usually a better idea to use the for attribute as explained above.
Next week I’ll explain the various field elements that you can use, and how/when to use them.
Author comments are in a darker gray color for you to easily identify the posts author in the comments
[...] the full post – HTML Forms Part I on [...]
[...] Last week I explained the markup required for setting up the main aspects of a form. This week I’m going to explain the form input tag and its various uses. [...]
[...] Go to Source [...]
[...] Last week I explained the markup required for setting up the main aspects of a form. This week I’m going to explain the form input tag and its various uses. [...]
[...] of this series about forms, their usage and markup. You can catch up on the previous posts at Part 1 and Part [...]
[...] of this series about forms, their usage and markup. You can catch up on the previous posts at Part 1 and Part [...]
Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums
Thanks ,Have known many.
@Sarah: For wordpress blogs, I just use wordpress comment form to allow readers to connect
A couple of things, labels should be within a div or should have a class / id so that their styling can be controlled by CSS.
Hi Jeet, labels don’t have to be in divs and they don’t need a class or id. You should give your form an id then can target that specific form, or you just target the label tag in your CSS.
No need to add extra markup when it isn’t necessary. However I’ll be doing some example form layouts next week for part 3 of the series.
@Sarah: Agreed
Just a form id should be able to get most details. Would love to see layouts examples in coming weeks.