Professional Documents
Culture Documents
Content
Tables, forms, and nesting them
Should tables be used to structurize forms?
An example
Notes
o Avoid too large tables
o Avoid table align="left" (or "right")
o Positioning the submit button
o Using table header cells
o Tuning the visual appearance
An alternative: using just line breaks
Another alternative: using paragraphs
Yet another alternative: FIELDSET
Using CSS to create tabular look
Tables and forms can be nested either way. But if you put forms into tables, each form must be
completely included into a single table cell (one TD element in practice). Thereby the forms are
each completely independent. See my document on choices in HTML forms for some simple examples.
What people probably want to do more often is to nest them the other way: a TABLE element inside
a FORM element. Then all form fields within the table cells, being in TR elements within
theTABLE element which is within the FORM element, are fields of the same form.
It is best to organize forms simply: don't use tables to force some particular layout but to indicate
the structure when needed. Browsers are expected to present tables, and HTML documents in general, in
a manner which corresponds to the structure expressed in HTML.
To take a fairly common example: Assume that you wish to let the user pick up a product from a list in
and order form. Probably the product information consists of some fields like product name, order
number, and unit price for each product. This means that the data itself is inherently tabular. Why not
make it table? That would make the appearance reflect the structure. There are some technicalities
involved here; see notes on using checkboxes or radio buttons (instead of SELECT) for such purposes.
An example
As a simple example, consider the situation where some text (field label or something like that) is
associated with an input field. (There are better ways for doing that in principle in HTML 4.0, but
currently the table idea might be better.) You could set up a table where the first column contains the
texts and the second column contains the corresponding input fields:
The presentation can be affected by style sheets. In particular, this document comes with a style sheet which makes some
presentational suggestions, so presentation may differ from your browser's usual way of displaying forms. See the
document Affecting the presentation of form fields on Web pages.
Another typical case for using a table is to organize a set of radio buttons. There is an example of this
in Choices in HTML forms as well as at the end of the current document.
Notes
Avoid too large tables
Although this method can be used for a form with a large number of fields, a practical word of
warning is in order: the larger a table is, the longer it takes from a browser to display it. (The browser
has to process the entire table before displaying anything of it, since it must take all cells into account
when selecting suitable column widths.) Perhaps you can divide the content of a form into major
sections, with headings, and structurize each section separately as a small table.
Also notice that contrary to popular belief among authors, a reset button is not obligatory or even
useful, as a rule. It is especially dangerous when presented alongside with a submit button!
<TR>
<TH>Your name</TH>
<TD>
<INPUT TYPE="TEXT" NAME="name" SIZE="20">
</TD>
</TR>
A typical presentation of TH elements uses more emphatic style than for TD cells, e.g. by using bold
face. Moreover, by default the content of a heading cell is centered whereas normal cells are left-
aligned; this can be affected by using the ALIGN attribute if desired. The following shows
how your browser displays (perhaps affected by a style sheet suggested in this document) our sample
form when TH markup is used for the texts which are logically field titles:
As regards to issues specific to the combination of tables and forms, note that if you
put a form into a table cell, then you there might be some unwanted extra vertical
space below the form. There are various ways to try to suppress that. If there is only
one form in the table, one of the ways is to change the structure so that the form and the table are nested
the other way around: the form contains the table, even though only one cell contains fields of the form.
Technically you'd just move the start and end tags of the form. This means something like the following
(in a case where the form appears in the last cell of the table):
The modified markup may look illogical, since structurally there is no need to have all the stuff in the
other cells inside the form. But it can be done in valid HTML and with well-defined meaning: when the
form is submitted, only the form fields matter, so the other content in the form has no effect on how
form submission works. And since the (probable) extra spacing after a form appears only after the entire
table, the presentational problem is removed, or at least made less important.
There are, however, the following problems with this approach, where the form element contains much
more than what logically constitutes a form: A visual browser might have (e.g., due to a user style sheet)
a special presentation for a form as a whole, e.g. using a distinctive background color or image to make
the structure clear. When the markup is illogical, the user will be confused. Similarly, a speech-based
user agent probably indicates the start of a form by saying, for example, "start of form 1". This is
confusing, if the actual start of the form is far ahead.
The BR elements cause line breaks. It is largely a matter of taste whether you prefer this style to using
tables. The main difference in typical presentation is that when using a table, the input fields "line up"
forming a column. And style sheets or presentational attributes in table-related elements could be used to
suggest the details of the presentation.
But since it might be argued that a field name and a field do not constitute a paragraph, you could
alternatively use DIV elements instead of P elements. This would introduce no extra vertical spacing.
It is also possible to combine the use of FIELDSET with the use of a table to structurize e.g. a set of
radio buttons. For radio buttons, and for checkboxes, the conventional method is to put the associated
text after the radio button or checkbox. This is somewhat illogical but so common that accessibility tools
might handle it better than the logical order. Example:
<FIELDSET><LEGEND><strong>Gender</strong></LEGEND>
<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="5">
<TR><TD>
<LABEL><INPUT TYPE="RADIO" NAME="gender" CHECKED
VALUE="Unspecified">Unspecified
</LABEL><TD>
<LABEL><INPUT TYPE="RADIO" NAME="gender"
VALUE="Female">Female</LABEL>
<TD>
<LABEL><INPUT TYPE="RADIO" NAME="gender" VALUE="Male">Male</LABEL>
</TABLE>
</FIELDSET>
This results in the following presentation on your current browser:
It would perhaps be better (in cases like this at least, when there is a small number of alternatives) if the
legend (which is logically a heading for the set of radio buttons) appeared at the same line as the
alternatives. But the syntax of FIELDSET makes this impossible: the first element there must
be LEGEND, so one cannot organize the legend and the buttons with their labels as one row in a table.
For accessibility reasons, and for clarity, it would be better to make each alternative appear on a line
(row) of its own, using the same approach as above but a one-column table instead of a one-row table.
Example:
In this approach, on the other hand, the table markup is rather unnecessary. You could just wrap
each LABEL element inside a DIV element.