views:

107

answers:

2

What's the best structure to use when designing a form/ details view for maximum readability (accessibility) and versatility?

As an example, the ASP.NET MVC framework's scaffolding creates a fieldset with a legend at the top and all fields in p's (a label and then the input/ editor set).

What's the most versatile structure to use, do you think?

For example, if I want to change the layout later to have two or three fields side-by-side instead of top-down, I'd only want to do that via CSS, as it's not structurally relevant.

Thanks,
Kieron

EDIT: A friends recommendation was to use dl, dt and dds...does anyone have any thoughts on that?

A: 

I'd just create a basic form and let the structure grow around that.

<form>
  <input>
  <input>
  <input>
</form>

And then when you need groupings

<form>
  <div>
    <input>
    <input>
  </div>
  <input>
</form>

And then do all your presentation in CSS. The best way to test accessibility is to turn off all stylesheets and see if the site makes sense.

Forms are block level elements so they are semantically correct for containing other elements.

Paul Tarjan
+5  A: 

I use a very similar structure to the one generated by ASP.NET scaffolding, except that I use divs instead of ps:

<form>
    <fieldset>
        <legend>Legendary Fieldset</legend>
        <div>
            <label for="textBox">Text Input</label>
            <input name="textBox" id="textBox" />
        </div>
        <div>
            <label for="selectBox">Select box</label>
            <select name="selectBox" id="selectBox">
                <option>1</option>
                <option>2</option>
            </select>
        </div>
    </fieldset>
    <!-- more fieldsets if required -->
</form>

I use divs because to me thats more semantically correct than p elements as they are intended for paragraphs of text.

When it comes to styling its also a versatile structure because you could for example make the fieldset 500px wide and the fieldset div 250px wide and floated, thus achieving side by side. Or you could have the same width for fieldset as the fieldset div. OR you could have the fieldset and fieldset div the same width and then stick a class on some divs (say "half") that are half the width and floated. The possibilities really are endless.

In any case this is just what I use for day to day stuff - though its versatile, it may not fit all requirements.

EDIT As far as definition lists are concerned, they are specialised elements that semantically should not be used to lay out a form.

Darko Z
I think you're missing `id`'s on input elements, required for label association.
kangax
forgive me this was a quickly typed example
Darko Z
Labels are the key thing here - clicking the label if marked with for gives focus to the input - nice big click targets.
Rich Bradshaw
I agree except it would be more semantic if you replaced the div elements with p elements. A div is a division of a document, or rather a containing section while a p, short for paragraph, element is a block of content expressing a thought. The option elements also need the value attribute even though they contain text content. There are differences in access and software interpretation of the nodes when not using the value attribute. I also recommend using the fieldsets to group like minded paragraphs together to make them more meaningful.
@austin - i disagree. Paragraphs should be used for textual content only. Like in a blog or in an article. I can't see how someone can interpret a 'paragraph' as suitable for form elements semantically. That would be like saying a blocquote is good for dividing a page into sections, or a table is good for layouts...
Darko Z
These are all great answers, so thanks all. On the subject of paragraphs vs. div's, the solution should be acceptable for screen readers. So, if a paragraph make a screen reader read the content out of context, it's not the right solution. (P.S. I don't know if they do or not, just FYI'ing)
Kieron
Im not actually sure if they do, but its an interesting point worth some additional research
Darko Z