To table or not to table…

16th May, 2005 @ 10:58pm UTC

Accessibility, CSS, Design, Development, Web Standards, XHTML / 4 Comments

Thanks to the wonders of XHTML and CSS, table based design is becoming a thing of the past. We’ve all discovered that, even though columns are easier to layout with tables, they damage our markup, annoy the search engines and play havoc with accessibility legislation. However, its also apparent that tables should be used in web sites to control tabulated data. But what defines tabulated data?

Let’s take a look at some good XHTML table markup:


<table cellspacing="0" id="toTableDemo1">
  <thead>
    <tr>
      <th scope="col">Name</th>
      <th scope="col">Bananas</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Tim</td>
      <td>32</td>
    </tr>
    <tr>
      <td>Paul</td>
      <td>1</td>
    </tr>
    <tr>
      <td>Stuart</td>
      <td>100</td>
    </tr>
    <tr>
      <td>Suzy</td>
      <td>75</td>
    </tr>
  </tbody>
</table>

This would display the following (with a little help from CSS):

Name Bananas
Tim 32
Paul 1
Stuart 100
Suzy 75

By incorporating a thead section we have defined a title for each column: making it easier to read for normal users and also for those using text readers.

Unfortunately, not all tabulated data can be presented in a way that allows us to clearly seperate the header, body and footer sections using the thead, tbody and tfoot tags. A good example of this would be tabulated data that requires a legend down both axis of the grid. This would be similar to a spreadsheet in design. Below is an example:

  Mon Tue Wed Thu Fri Sat Sun
Tim 1 2 3 4 5 6 7
Paul 7 6 5 4 3 2 1
Stuart 100 100 100 100 100 100 100
Suzy 100 34 3667 3 19 20 21

And the markup:


<table cellspacing="0" id="toTableDemo2">
  <tr>
    <th scope="col">&nbsp;</th>
    <th scope="col">Mon</th>
    <th scope="col">Tue</th>
    <th scope="col">Wed</th>
    <th scope="col">Thu</th>
    <th scope="col">Fri</th>
    <th scope="col">Sat</th>
    <th scope="col">Sun</th>
  </tr>
  <tr>
    <th scope="row">Tim</th>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td>7</td>
  </tr>
  <tr>
    <th scope="row">Paul</th>
    <td>7</td>
    <td>6</td>
    <td>5</td>
    <td>4</td>
    <td>3</td>
    <td>2</td>
    <td>1</td>
  </tr>
  <tr>
    <th scope="row">Stuart</th>
    <td>100</td>
    <td>100</td>
    <td>100</td>
    <td>100</td>
    <td>100</td>
    <td>100</td>
    <td>100</td>
  </tr>
  <tr>
    <th scope="row">Suzy</th>
    <td>100</td>
    <td>34</td>
    <td>3667</td>
    <td>3</td>
    <td>19</td>
    <td>20</td>
    <td>21</td>
  </tr>
</table>

As you can see, I’ve defined the legends using the th tag. This method is less confusing than using the thead section in this particular instance.

Now that we have two examples of good table markup in XHTML, let’s think about the role of the table on an XHTML page. Functionally, the table makes the data easier to read, allowing us to follow the rows and columns easily and associate the values with the relevant headers. With the design, the table also allows us to make data more presentable, which also makes it easier to read. Ultimately then, the XHTML table is simply about data presentation.

So what have I been leading up to? Recently I was arranging an XHTML form for a client when I found myself looking at the presentation from a different angle. Normally I would try and arrange the form fields within labels, those labels in turn would then be arranged within fieldsets. I can then arrange the fieldsets, labels and fields using floats and absolute positioning in the CSS to cobble together a nice table-less design. Of course, in the old days, I would have slapped the whole lot in a table and not thought twice about it. These days, however, table based design is, as we discussed earlier, frowned apon. But in this context, is it really so bad?

Imagine we lay out our form like so (here’s the markup followed by what it would display):


<form>
  <fieldset>
    <table cellspacing="0" id="toTableDemo3">
      <tr>
        <th scope="row">
          <label for="input1">Name:</label>
        </th>
        <td>
          <input id="input1" name="input1" type="text"/>
        </td>
      </tr>
      <tr>
        <th scope="row">
          <label for="input2">Rank:</label>
        </th>
        <td>
          <input id="input2" name="input2" type="text"/>
        </td>
      </tr>
      <tr>
        <td colspan="2">
<input id="sub_form" name="sub_form"
type="submit" value="Submit"/>
</td>
      </tr>
    </table>
  </fieldset>
</form>


As you can see, the labels are in th tags (because that is effectively what they are) and the inputs themselves are considered standard data - much the same as if you were entering data into a spreadsheet. This markup is not only logically valid but also allows us flexibility in design. However, I still find myself struggling to lay things out without using the table because something inside me feels dirty. Really dirty. And although I might like feeling dirty, the CSS Nazi in me is disgusted.

So what do you think? When laying out forms, are tables valid markup or nasty, dirty, filthy markup?

Edit: In reply to Stuart’s comment below, I have added the scope attribute to my th tags. This allows the browser or text reader to have a better understanding of the header.

Like this post? Digg it or Del.icio.us it!

Comments (4)

Skip to the comment form…

  1. Gravatar Image Stu May 17, 2005 @ 1:54 am

    Hi Tim,

    I like a bit of squeaky clean and a slice of muddy filth both at the same time!

    As we all know the obvious place for tables should be when you need to display tabulated data that cannot be easily re-created in CSS. This would be pulling rows of data out of a database, complex forms (more that one field per row?), and any other display of tabulated data. There’s nothing dirty about that, as that what tables are there for.

    However, when you are talking about defining a simple login I would certainly look to ignore tables for something that simple. It can be done as you’ve said above simply and cleanly without tagtastic markup. With the CSS control over fieldset, input and the label tags you can position everything neatly without relying on extra classes and container divs.

    Quick additional point: In your second example you could make use of the scope attribute which defines whether the th applies to rows or columns.

    A nice related article re tables and the many tags that can be used to aid accessibility can be found here.

  2. Gravatar Image Paul Velocity May 17, 2005 @ 9:18 am

    It all depends on the content of the form. If the form is based on tabulated data then it should be in a table. For example:

    Question, Answer;
    Name, input;
    Address, input;
    How many bananas?, input;

    It is a table that the user is filling in. In this respect, I would use a table to mark up the code. I believe this would make it easier for screen reader users to review their answers prior to submission of the form.

    Im all for nasty dirty filthy things, but using tables to mark up forms in this respect is not one of them.

  3. Gravatar Image Samuel Cochran June 9, 2005 @ 5:18 pm

    I think it’s easy enough in this situation to use CSS. Using a couple of selectors, a fieldset tag, and your label/input tags, you can create a nice-looking form that lines up. Tables seem an unneccessary dirtyness.

  4. Gravatar Image Tim June 9, 2005 @ 9:20 pm

    Samuel: I agree, it’s fairly straight forward to completely style a simple form using CSS. I have had problems, however, when creating large survey style forms for clients.

    The biggest problem I have found when adopting this technique, is styling groups of radio buttons or checkboxes. Obviously, semantically, we would keep them within a fieldset with a legend that is the overall question. The labels against each radio or checkbox are then the answers. This is great in the XHTML but when trying to style it in a meaningful way for the user, it becomes a bit of a nightmare!

    This relatively minor problem has resulted in my boss branding standards based development “useless” - even after a group of us displayed several methods of styling forms (inputs within labels, inputs and labels within definition lists etc) but he always refered back to the “good old table way”…

    Sometimes you just can’t win. :(

Leave a comment





Categories

Syndication

Technorati

© 2010 Tim Huegdon, All Rights Reserved / Website design and development by Nefarious Designs

Powered by Wordpress / Log in

Due to the dodgy manufacturing process, this website may contain traces of nuts!