caption-detail/tables and borderline semantics

August 12, 2008 / 09:52

Since we looked into the idea of borderline semantics last time I guess it wouldn't hurt to take some time illustrating the concept with a good example. The html table is an ideal target for this. Tables have been abused since the beginning of time, and their abuse happily continues as the idea of tabular data isn't that easily explained. This article will take a look at caption-detail structures and how they can replace the use of tables.

always alone

tables, forms and presenting data

Remember all the discussions about forms, and how they could be interpreted as tables, semantically speaking? People tried to sell this idea on the basis that a form is nothing more than a table with one vertical header row (the field labels) and one vertical input row (the input fields). It's hard to contradict this, unless you think back of what I called borderline semantics. This is a prime case to apply such kind of reasoning. Just a quick reminder:

The semantic value of an element should not be determined by its current state, but should be seen as part of the broader behavior of the component it belongs too. There's nothing wrong with a list having only one item, but if that is all it can ever have, it's simply not a list and it's better to use a div element.

In a form you have one input field per label, and even if you have more (for example, if you're using three input fields for a date) they are not meant as a repetition of data. This already shows us that a table really isn't the best way to mark up a form, as a form will never have multiple rows, eliminating the possibility of "tabular data" in a form (unless of course you want one of those real table-forms, where you want the user to enter five times the same data for another entity). A similar reasoning can be made for presenting data (rows with labels and the actual data).

caption-detail

<div class="row clearfix"> <div class="caption">...</div> <div class="detail">...</div> </div>

So if we can't use a table for such structures, then what should we use instead? The answer is the caption-detail structure. If you check the piece of code above you'll see an example of how such structure is composed. It does mimic a table, with the row class taking the function of tr element and both caption and detail classes mimicking td elements. That said, each row will only contain one caption and one detail element. The row class might seem useless in some situations, though it has a clear structural function and often comes in handy when cssing a caption/detail component.

In case of a form, the label goes into the caption element, while the input element goes inside the detail element. Added benefit is that this code is mostly reusable for showing the inputted data, so it increases the consistency of your code. The only thing which changes is the input element, which becomes the actual data.

base html code

There are various ways to implement the caption-detail structure. You could argue that the rows should be li elements in an unordered list. You could also use a definition list (though I wouldn't recommend it, if only because it's a terribly structured element). And you could always leave the rows out if you like minimal html. This discussion is beyond the scope of this article, but suggestions are always welcome. So far, I've always been using divs myself.

There's also a question of using spans instead of divs (for the caption and detail elements). This is pretty much dependent on the way the data itself is entered. I believe that when the caption contains a ':' or other textual separator the span element can be used (as it works like inline data). If no textual separator is present, it's probably best to use the div element to clearly separate both elements and to avoid confusion.

Finally, we can leave the "detail" div out too if you want to write really minimal html. It is then implied that the data without extra structural wrapper is the detail part of the structure. It's up to you, just not my cup of tea.

so is it useful?

Most projects I've worked on contained one or more caption-detail components. So it's good to standardize this piece of code. You can wrap the rows to add extra context to the component, it's pretty flexible to css and even though the base html elements aren't too flexible, the class names give a pretty good idea of the semantics of the element. So next time you run into a similar component, consider the caption-detail.

blog archive

All my articles are neatly filed inside the archive. Search and filter your way to the article you like:

contact me

If you want to leave me a quick message or you have any questions, drop me a note.

Comment author
3 comments in total
August 12, 2008 11:01

I see semantic coding as using only the html you need to be able to identify parts of your page and no more than that.
Yes, sometimes you need maybe less semantic elements to implement things like columns and sidebars, but within these I try to only use semantic elements, like a paragraph or a header. You may call me a minimalist.

Coming to forms. A form is a collection of labels and input fields (textboxes, radiobuttons etc.). First off all there is the form-tag and for grouping questions one can use the fieldset with a legend. Within these fieldsets I still would only use labels and inputs.

Frankly, I do not see the need for extra divs for caption-detail constructions.
There could be special situations, where you may have a follow up question (maybe when you chose No as answer you need to give an explanation), where you may need that question styled a bit different from the normal question and you may need an extra div around the label and the textarea. That's OK by me, it is a relevant piece of html I am adding.

Another argument for adding surplus html could be that the form you are pulling together is generated by serverside code where you do not know where the question might be in a form (first, last or in the middle). I have done, for a web application where each question was wrapped in a div and also had some elements for indicating whether the question was mandatory and an information icon with some popup information. That added a few div-, span- and img-tags. All relevant to accomodate the requested functionailty.

For the rest I would suggest to not (never) add extra divs and classes if you can live without them.

August 12, 2008 16:11

I understand your reasoning, I guess my priorities are just elsewhere.

One point I prefer to stress is consistency in code. You are right that these element are semantically obsolete, but not structurally (which is also an important aspect of html). Leaving these elements out is fine, if you have no extra additions to your form elements. We often use field helps (question marks with overlay div), field hints (content structure indicators), input aids (like a datepicker) and even feedback in our forms. Some of these elements should be added to the caption container, some to the detail container, others (fe the feedback) are on the same level as both containers.

You could move and remove the divs according to the form you make, but that makes adaptations a real problem (especially since our company doesn't handle any back-end development). Consistency is key here, and to attain that you have to provide a good and solid base structure.

Of course I've used the example of a form here, but the use of the caption/detail structure is wider. Wherever you have a label (semantic label) and data linked to that label the structure can be used.

andy
August 25, 2008 19:40

No justification of tables for forms is necessary. The specs for html 4.01 say it can be done. End of discussion. It is not a maybe, not a "borderline", it is definitional. The form section discusses the requirements for semantically using a table for a form -- label in one cell, input in another. This is not ambiguous. Tables can be used for forms.

Fortunately those same specs also provide a structural semantically correct alternative container. Paragraphs and divisions can be used. But whatever method is used it must contain the salient structural semantic markup for forms: label, control, fieldset and legend. Using them is what the specs require. Table or non-table is a separate decision.

From this point on the discussion is about presentation not structure. Most of the discussions of the "cow paths" have been like this -- matters of presentation. Very interesting and informative discussions of presentation. It remains thus site centric. If I prefer to use a p element with label and input I could style it exactly the same as your caption-detail with no change in presentational semantic integrity. As long as the css specs are not violated, caption-detail has no inherent presentational semantic advantage over any of a number of alternatives including the table version.

Of course html5 will change some of this but even there the distinction between presentation and structure has to be respected.

* required fields

Leave your data
Leave a comment