Working with Web Forms
Total Page:16
File Type:pdf, Size:1020Kb
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
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 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: 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 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: 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 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 Another advantage that Method C has over the previous two is that while each group (label and field) is wrapped in 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: 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 Method D: Defining a form 1. Open a new HTML file, and enter the following code: 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 05226e52b832b11a85dccccc334a096b.doc Version 1 Page 11 of 30 Each form label is wrapped in a definition term element By default, most visual browsers indent the or Defining style The simplest style we could add would be to easily remove the default indenting of 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 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 In fact, because of the presence of the 05226e52b832b11a85dccccc334a096b.doc Version 1 Page 13 of 30 Summary You have looked at four different ways to mark up the same simple form noting the pros and cons of each. It is important to point out that the accessibility features that you added to Methods C and D could, of course, be easily added to the first two methods as well – and those methods would be better because of those added features. Neither one of the methods that you have looked at here are necessarily miles ahead of the others in terms of a ‘best solution’. However, it is valuable to know your options. Let us recap the differences between the methods presented. Method A: Visually, it is a neat way to organize form controls and labels - especially for larger complex forms. However, using a table for such a simple form seems a bit unnecessary. Method B: Simple markup will degrade nicely in text browsers and small-screened devices. Visually, just using Method C: Simple markup will degrade nicely in text browsers and small-screened devices. This method allows for labels and controls of different lengths without any lining up issues. This method contains an important accessibility feature. Method D: Structured markup will degrade nicely in text browsers and small-screened devices. This method contains an important accessibility feature. Labels and form controls could be placed on the same line or separate lines using CSS. There is also room for improvement on Method C and you will take a look at a few additional features. The tabindex attribute 05226e52b832b11a85dccccc334a096b.doc Version 1 Page 14 of 30 A feature that we can easily add to your form is the tabindex attribute. Adding tabindex and a numerical value enables users to navigate the focus of form controls with the keyboard - typically using the Tab key. Repeatedly hitting the Tab key will change the focus to the next form control in an order that we can specify. By default, every interactive element has an implied tab order but using the tabindex attribute takes that ordering away from the browser - putting you in full control. As an example, add the tabindex attribute to the form controls in your Method C example: 1. Edit the file Checkbox_option.html as shown below: 2. Save your HTML file as tabindex_attribute.html. 3. View your web page in a browser and tab through the form controls using the Tab key. Now, when the user tabs through the form, you will be ensuring the focus of the cursor follows the exact order you intended - Name:, Email:, Remember this info?, and the Submit button. Using tabindex to set focus order becomes even more useful for complex forms and those where there might be multiple input boxes or other form controls for a single label. In addition to being simple to implement on your form, you will again be helping mobility-impaired users by letting them navigate the form entirely with the keyboard. Rather than using the mouse to enter each form item, the user will be able to tab through each control, in the correct order. accesskey attribute Similar to tabindex, the accesskey attribute is another easily added feature that can be useful for mobility-impaired users. For example, if we add the accesskey attribute to the Let us take a look at the code that will make this happen: Depending on the system, the user will either use the Alt or Ctrl key in conjunction with the 9 key that you have specified in the markup. Focus will immediately shift to the Name: field in our form. Adding the accesskey attribute can be especially helpful when used on frequently used forms such as a search box or membership login. Without having to reach for the mouse, users can instantly change focus and start their query or input using only the keyboard. It is important to note that, while not all browsers handle accesskey, it is an added benefit for those that do. For instance, to access the search form field where you have added accesskey="9", Windows users would press Alt+9, while Mac users would press Command+9 to shift the focus to the search field. 05226e52b832b11a85dccccc334a096b.doc Version 1 Page 17 of 30 Styling Forms You will now look at a few CSS techniques that can be used to visually customize the form. Setting the width of text inputs Form controls can be difficult to deal with due to the varying widths and heights that are dependent on browser type. In your form example, you have not specified a size for the text inputs and have left the width of these up to the browser’s defaults. Typically, a designer might specify a width using the size attribute, adding it to the element like this: Setting a size of "20" specifies the width of the text input box at 20 characters - and not pixels. Depending on the browser’s default font for form controls, the actual pixel width of the box could vary. This makes fitting forms into precise layouts difficult. Using CSS, we can control the width of input boxes (and other form controls) by pixels if we wish. For instance, let us assign a width of 200 pixels to all elements in our form example. You will take advantage of the id that is assigned to the form - in this case thisform. #thisform input { width: 200px; } 1. Edit the file tabindex_attribute.html as shown below: 2. Save your HTML file as styling_forms1.html. 3. View your web page in a browser. 05226e52b832b11a85dccccc334a096b.doc Version 1 Page 19 of 30 The check box and submit button are also an element and therefore receive that same value. Therefore, instead of applying the width to all elements, you should use the IDs that you set for the Name and Email controls only. #name, #email { width: 200px; } 1. Edit the embedded style as shown above. 2. View your web page in a browser. 05226e52b832b11a85dccccc334a096b.doc Version 1 Page 20 of 30 The above screenshot shows the corrected results in a browser with only the two text input boxes at 200 pixels wide. Using We have a few different options for customizing the size, face and colour of text that is contained within our form. One method is using the Alternatively, you could add styles to all paragraph elements that fall within your form using a unique style. #thisform p { font-family:Verdana, sans-serif; font-size:12px; font-weight:bold; color: #66000; } This would style all text contained in paragraphs within your form with a bold, burgundy, Verdana 12-pixel font. The same results can be achieved by applying those same rules to just #thisform label { font-family:Verdana, sans-serif; font-size:12px; font-weight:bold; color: #66000; } 1. Add the above style to styling_forms1.html. 2. Save your HTML file as styling_forms2.html. 05226e52b832b11a85dccccc334a096b.doc Version 1 Page 21 of 30 3. View your web page in a browser. If the form has additional instructions or text contained within elements, this additional text would inherit the same styles if you applied them to elements within your form. You could instead apply a generic style to all text within your form, and then use the label styling specifically for customizing form controls uniquely. The CSS would go something like this: #thisform { font-family:Georgia, serif; font-size:12px; color: #999; } #thisform label { font-family:Verdana, sans-serif; font-weight:bold; color: #660000; } You will notice that you do not have to repeat the font-size:12px; rule in the #thisform label declaration. Since 05226e52b832b11a85dccccc334a096b.doc Version 1 Page 22 of 30 to set shared rules at a high level and then override only those that are unique and necessary further down the element tree. This will save bytes of code and also makes for easier updates later on. If you wish to change the font- family for the entire form you need only update one rule - rather than each place that the rule is repeated. Use
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.
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. to mark up a form is a perfect example.
elements, you have a readable form layout for those browsing without CSS. element to lay out a form, the layout does not have to put each element on its own line.
,
elements results in a cramped layout.