Working with Web Forms

Total Page:16

File Type:pdf, Size:1020Kb

Working with Web Forms

WORKING WITH WEB FORMS

Interactivity has always been an integral part of the web letting the user and site communicate through the exchange of information. Forms allow us to collect input from users in an organized, predetermined fashion - and have traditionally been an ‘anything goes’ area when building websites. Marking up a form and styling a form can be handled in many different ways.

There are several options to consider as well as steps that can be taken to ensure our forms are structured in a way that will benefit both the user and site owner.

What are our options when marking up a form?

We will take a look at four different ways to mark up the same, simple form - all of which achieve similar results. We will go over each method and talk about the pros and cons that are involved.

1. Create a new folder in your My Sites folder called Working with Web Forms.

2. Within the folder Working with Web Forms create an images folder and copy the file input-bg.gif in to this folder.

3. Create a new Dreamweaver site pointing to the Working with Web Forms folder. You could add Remote Info and create a new folder on the server Web Forms to which you will publish.

Method A: Using a table

4. Open a new HTML file, and enter the following code:

05226e52b832b11a85dccccc334a096b.doc Version 1 Page 1 of 30 Method A – Using a Table

Name:
Email:
 

5. Save your HTML file as Method_A-Using_tables.html.

6. View your web page in a browser.

05226e52b832b11a85dccccc334a096b.doc Version 1 Page 2 of 30 Tables have long been used to mark up forms. Because of this, seeing forms laid out in this particular way has become familiar to us - right-aligned text labels in the left column, left-aligned form controls in the right column. In fact, using a simple, two-column table is one of the easiest ways to achieve a usable form layout, and some designers believe that forms could be considered tabular data.

In addition, using a table is sometimes the best way to achieve certain form layouts - especially complex forms that involve multiple controls like radio buttons, select boxes etc. Relying solely on CSS to control the layout of complex forms can be frustrating and often involves adding extraneous and

elements - with more code than would be employed laying out with a table.

You can see from the above diagram that by using a table, the labels and form elements line up nicely. For such a simple form, however, you would probably opt to avoid the table altogether in favour of something that requires less markup. Unless this particular layout is crucial to the visual design of the form, using a table here is not necessary. There are also a few accessibility concerns which you will look at shortly.

Method B: Tableless, but cramped

1. Open a new HTML file, and enter the following code:

Method B – Tableless

Name:
Email:

05226e52b832b11a85dccccc334a096b.doc Version 1 Page 3 of 30

2. Save your HTML file as Method_B-Tableless.html.

3. View your web page in a browser.

Using a single paragraph and a few
elements to separate the items is a passable solution - but could visually render a bit on the cramped side. We also run into the problem of the form controls not lining up perfectly.

We could alleviate some of the crowding by adding some margins to the elements using CSS like this: input { margin: 6px 0; }

4. Edit the file Method_B-Tableless.html to add an embedded style as shown above.

05226e52b832b11a85dccccc334a096b.doc Version 1 Page 4 of 30 5. View your web page in a browser.

The preceding would add a 6-pixel margin to both the top and bottom of each element (the name, e-mail, and submit controls) - spacing out the elements as shown.

While there is nothing particularly wrong with Method B, there are a few adjustments you can make to build a better form.

Method C: Simple and more accessible

1. Open a new HTML file, and enter the following code:

Method C – Simple but more accessible


05226e52b832b11a85dccccc334a096b.doc Version 1 Page 5 of 30


2. Save your HTML file as Method_C-Accessible.html.

3. View your web page in a browser.

For a simple form like this example, it is convenient to contain each label and control in its own

. When viewed un-styled, the default behaviour of a
(each on its own line) should be enough to set the items apart in a readable way. Later, you could control precise spacing with CSS on
elements that are contained within the form.

05226e52b832b11a85dccccc334a096b.doc Version 1 Page 6 of 30 You have even gone a step further and have given this form a unique - id="thisform". Therefore, that precise spacing referred to above could go something like this:

#thisform div { margin: 6px 0; }

4. Edit the file Method_C-Accessible.html to add an embedded style as shown above.

5. View your web page in a browser.

Essentially, you are saying that all

elements in this form should have top and bottom margins of 6 pixels.

Another advantage that Method C has over the previous two is that while each group (label and field) is wrapped in

elements, a
puts each on its own line. Using a
to separate the items gets around the issue of fields not lining up perfectly due to text labels of different lengths.

Aside from the visual aspects of Method C, we should consider the most important advantage - an accessibility improvement.

The

There are two steps for utilizing the

05226e52b832b11a85dccccc334a096b.doc Version 1 Page 7 of 30 to use

The second step is adding the for attribute to the

For instance, in Method C we wrap the



Why

Creating label/ID relationships allows screen readers to properly read the correct label for each form control - regardless of where each falls within the layout.

In addition, the

An additional benefit to using

For example, if you add a check box option to your form that gives the user the option to Remember this info, you can use the

05226e52b832b11a85dccccc334a096b.doc Version 1 Page 8 of 30 1. Open a new HTML file, and enter the following code:

Checkbox option



2. Save your HTML file as Checkbox_option.html.

3. View your web page in a browser.

05226e52b832b11a85dccccc334a096b.doc Version 1 Page 9 of 30 Click on Remember this info? to toggle the checkbox.

By marking the check box option up this way, you have gained two advantages. Screen readers will read out the form control with the correct label (even though in this case, the label comes after the control), and the target for toggling the check box becomes larger – and now includes the text as well as the check box itself (in most browsers).

We have looked at the use of tables, paragraphs and

s to markup forms. There is another common method – using a definition list.

Method D: Defining a form

1. Open a new HTML file, and enter the following code:

Method D – Definition list

05226e52b832b11a85dccccc334a096b.doc Version 1 Page 10 of 30

2. Save your HTML file as Method_D-Definition_list.html.

3. View your web page in a browser.

The last method for implementing form layout involves the use of a definition list to define label and form control pairs. It is a somewhat controversial move - skirting the fringe of what a definition list is designed to do. However, it is a method that is gaining in widespread use.

Definition lists are certainly capable of more uses than most designers are aware of. Using a

to mark up a form is a perfect example.

05226e52b832b11a85dccccc334a096b.doc Version 1 Page 11 of 30 Each form label is wrapped in a definition term element

followed by its associated form control wrapped in a definition description element
. Doing this creates a pairing of label and form control - which when viewed in a browser without any style applied is displayed as shown above.

By default, most visual browsers indent the

element on its own line. Therefore, without adding any additional

or
elements, you have a readable form layout for those browsing without CSS.

Defining style

The simplest style we could add would be to easily remove the default indenting of

elements within our form: form dd { margin: 0; }

1. Edit the file Method_D-Definition_list.html to add an embedded style as shown above.

2. View your web page in a browser.

05226e52b832b11a85dccccc334a096b.doc Version 1 Page 12 of 30 In addition, the table-like format of Method A could also be achieved by floating

elements in our form: form dt { float: left; padding-right:10px; }

1. Edit the file Method_D-Definition_list.html to add an embedded style as shown above.

2. View your web page in a browser.

By floating the

elements to the left, the form controls contained in
elements will align themselves to the right as seen above. You will notice that the form controls do not line up with each other perfectly - but at the very least this illustrates that while it is possible to use a
element to lay out a form, the layout does not have to put each element on its own line.

In fact, because of the presence of the

,
, and
elements - which are in addition to the form
Recommended publications