Tag cloud
See my latest projects
About this site
This site is proudly powered by WordPress. If you like what you see, why not subscribe?
Copyright © 2008-2010 Aaron Russell. All rights reserved.
Wednesday, 13 January 2010

Last week I asked the question on Twitter, “What HTML element do you use for each line of a form? P, DIV, or something else?”
Normally this is the kind of conversation I like to keep well out of. Experience tells me that some people get a bit too passionate for their own health when talking about markup and semantics.
I don’t see myself as a member of the markup police and really, I don’t think there is a “wrong” answer to the question (… well, there is, but I’ll give people the benefit of the doubt).
Historically, I’ve always used DIVs to wrap each LABEL and INPUT pairing. Then at some point about a year ago I started using Ps. I don’t know why I changed, I think it’s because I saw lots of other people using Ps and I just followed suit.
The more I think about it, the less sense using a P makes. Firstly, from a semantic point of view you are basically describing each label and form element as a paragraph. Which it’s not.
Secondly, from a practical point of view, if you need to slip in some actual paragraphs of text somewhere in the middle of a form, you’ll probably need to style them differently from the form rows which means adding additional classes to tags and generally having scrappier CSS.
At least two people responded to me on Twitter suggesting that I didn’t need any tags at all to wrap each LABEL and INPUT pairing and that I was adding superfluous markup. I don’t agree.
Again, semantically I believe each pairing needs to be grouped to adequately describe that a particular LABEL belongs with a particular form element, rather than just have a bundle of orphan elements living together in some kind of HTML workhouse.
And from a practical perspective, more markup is good because it provides greater flexibility for styling the form and highlighting errors and validation messages.
A DIV defines, and I quote, “a division or section of an HTML document”. Well, I suppose that’s not exactly wrong in our case. I certainly think a DIV is a better candidate than a P.
But the problem with a DIV is that it doesn’t really mean anything other than “enclosed within here is some stuff“. I think we can be more descriptive than that.
Today, my preferred technique for marking up forms is with an ordered list. Here’s an example:
<form> <ol> <li> <label for="name">Name</label> <input type="text" name="name" /> </li> <li> <label for ="email">Email</label> <input type="text" name="email" /> </li> </ol> </form>
Now, I expect the people who prefer their markup minimal are going to take some convincing. But to me, this makes absolute perfect sense!
There is more than enough markup there to allow us to style the form to our hearts content. And semantically we are saying, this form is a list of fields. That’s pretty descriptive, and I really like the idea of a screen reader saying “a form with a list of 5 items”.
For complex forms with multiple FIELDSETs the example above could be extended so that each form contained a list of FIELDSETs, and each FIELDSET contained a nested list of form fields.
As I said at the start of this article, I don’t necessarily think there is a “wrong” way to mark up forms. If you’re happy with your DIVs and your Ps, then that’s good enough for me.
But personally, I genuinely feel I’ve brought some order to my form building and am much happier doing so as a result.
I believe I was one of the people who disagreed with you on Twitter
I’ve never used OL and LI to order my forms as I’ve always felt it’s redundant. At the end of the day, a form isn’t a list, it’s a form, and my long-held view is that using a list where it’s not really needed is looking for a solution for a problem that doesn’t exist.
But I’ve been thinking about this a bit more and can understand why some would choose to use it. I can also see that it might be useful in certain situations within parts of a form, though not necessarily for each distinct form line. For example, it could be useful to wrap a group of related radio buttons or checkboxes in a list. In addition, I can see that having an extra wrapping element is useful if you want to add fancy inline error checking using jQuery or similar.
That said , I still think that for simple forms, the simplest HTML is the most appropriate — assuming you’re also marking that up correctly, with the correct LABEL for each INPUT and the associated FOR attributes (which a lot of people seem to forget).
Clearly, there’s no right or wrong and we all have our own way of doing things. As long as the form is usable and accessible, that’s the most important thing.
i personally dont see the reason for ordered list in this case. why not unordered?
Just stumbled across your post. It’s an interesting question and one that I guess one doesn’t really think about very much. It’s made me go and look back at some sites and see what I’ve been using. Looks like I vary from site to site! Mainly it looks like a DIV or a LI. I’ll see what happens next time now I’ve read this….!
Would a definition list be a more suitable solution?
Last week I asked the question on Twitter, “What HTML element do you use for each line of a form? P, DIV, or something else?” So, how do you do your forms?
This years season of 24 ways article has come to a close and with it a reoccurring theme of controversy has arisen: designing in the browser. I offer my thoughts on why it misses the point.
The problem with CSS pre-processing frameworks is that they don’t really fit within the average web designers’ work flow. So I built an extension to LESS for creating cached stylesheets your PHP projects can use.
Are you a web designer or are you a web developer? Let me guess, you are a bit of both. Does that mean you are “doing it wrong”?
If you’re like me then your life revolves around email. Unfortunately the grip that email now has on all our lives creates as many problems as it solves. Learn how I control my Inbox.
“RT @stephenfry: I So want to see this film. Oh wait. I already have. Genius. http://bit.ly/9cbRsQ < Haha very good! :)”
Posted about 5 hours ago.
This site is proudly powered by WordPress. If you like what you see, why not subscribe?
Copyright © 2008-2010 Aaron Russell. All rights reserved.