How do you mark up forms?
posted by Kravvitz at 10:06 PM on Nov. 5th, 2008
Categories: HTML, Semantics, Forms 6 comments
Many people who otherwise use tableless layouts still use tables for marking up forms. Yes, it makes getting things to line up correctly easy most of the time, but it's hardly semantically correct. You see, a table should have at least 2 rows and 2 columns of data. And they usually have a column of headers for the rows and a row of headers for the columns. Now, if you think about it, the labels for form controls make up a column of headers for the rows; that leaves you with one column of data which hardly could be called tabular data.
Now please don't just write me off as a "table basher" or something similar. I'm talking about using semantic markup as best you can. I accept that many people will have a period of transition as they move away from using tables for layout. My point is that many people seem to have stopped their transition prematurely. This would be a good time to mention CSS Tables, since IE8 will support them. Therefore, we can start using them and just use an alternate layout technique for IE6 and 7.
Some people are in the habit of using paragraph elements (<p>) for grouping each label with its form control. How does a label and its form control constitute a paragraph? Sometimes you may want to have a paragraph of text explaining part of the form though.
After having discussed how tables are inappropriate for marking up form controls, how should it be done? Other people like using definition lists (<dl>). This is where we've reached the gray area. I don't like using <dl>s for this myself, but I can see how other people would. For one thing, I prefer having an element to contain each label and form control pair.
Some people use ordered or unordered lists (<ol>s and <ul>s, respectively). I like using them for radio button and checkbox groups sometimes, but prefer just using <div>s for much of marking up forms.
Now I should point out that each form control should have its own <label> element and that it's good to group related groups of form controls with <fieldset> elements. Some people go wrong by using a <label> for the question that is answered by clicking on one of the radio buttons in a group of them. That's what a fieldset's legend element is good for. Styling those can be tricky, but that's a topic for another post.
So to summarize. Don't misuse <table>s or <p>s, always use <label>s, and use <fieldset>s where appropriate.
Now please don't just write me off as a "table basher" or something similar. I'm talking about using semantic markup as best you can. I accept that many people will have a period of transition as they move away from using tables for layout. My point is that many people seem to have stopped their transition prematurely. This would be a good time to mention CSS Tables, since IE8 will support them. Therefore, we can start using them and just use an alternate layout technique for IE6 and 7.
Some people are in the habit of using paragraph elements (<p>) for grouping each label with its form control. How does a label and its form control constitute a paragraph? Sometimes you may want to have a paragraph of text explaining part of the form though.
After having discussed how tables are inappropriate for marking up form controls, how should it be done? Other people like using definition lists (<dl>). This is where we've reached the gray area. I don't like using <dl>s for this myself, but I can see how other people would. For one thing, I prefer having an element to contain each label and form control pair.
Some people use ordered or unordered lists (<ol>s and <ul>s, respectively). I like using them for radio button and checkbox groups sometimes, but prefer just using <div>s for much of marking up forms.
Now I should point out that each form control should have its own <label> element and that it's good to group related groups of form controls with <fieldset> elements. Some people go wrong by using a <label> for the question that is answered by clicking on one of the radio buttons in a group of them. That's what a fieldset's legend element is good for. Styling those can be tricky, but that's a topic for another post.
So to summarize. Don't misuse <table>s or <p>s, always use <label>s, and use <fieldset>s where appropriate.
AFAIC that's the best asnwer so far!
Matty 5:49 PM on Jun. 12th, 2011Login or Register to post a comment