Which is better: div or fieldset?

If you have a form in which you use labels for which you don’t want a legend, is it better to wrap the labels/inputs in a div or use a fieldset with a legend and set the legend to display: none in the CSS?

I’d rather just use a div. If you don’t want a legend you don’t want a legend.

wrap inputs in labels

If I wasn’t already doing that there would be no need for this thread.

what are you trying to achieve?

@baby2boy: The Strict DTD only allows block-level elements to be immediate children of a FORM element. The FIELDSET element type is normally well suited for this, but sometimes – as Tyssen says – you don’t need or want a legend. Therefore, the question is whether to still use FIELDSET but hide the LEGEND or to use a semantically neutral block-level container (DIV).

@Tyssen: I agree with Simon, use a DIV if you wouldn’t want a legend even if CSS were disabled.

That’s the way I was leaning but thought I’d canvas some opinions first. :tup:

Isn’t a fieldset and legend simply for organizing a series of related inputs? Whether or not the box and heading are displayed should not impact the decision to use a tag.

If I had a lengthy form (or any form for that matter) that obviously had inputs that belonged grouped together but stylistically want to display it anyway I wish, would it still not be more semantic to use fieldsets and legends and hide them rather than omit them? If my form were to be redrawn by an alternative user agent would it not be better to have fieldsets and legends to aid in graceful degradation? I thought that was a major component of the “Semantic Web”.

I wanted to add that a <div> is a generic container (as many have outlined on this forum before), but when text is inside the <div> it is usually wrapped by <p> tags and thus is self describing (or whatever tags the context calls for - text in a list etc.). Whatever is in the <div> still has more tags to describe it and I feel a form should also - as a bonus you don’t need a div because you have the <fieldset> tag to grab a hold of. Even something as simple as a two input login box or one input password field would benefit from a <fieldset> and <legend> (saying what the inputs are for) IMHO. Lean code is great but there is a proper place for it - the difference is beyond neglible to a page viewer - and the code is more machine and human readable.

I agree here with wozbk, but like to add a question:

Does a fieldset always need to have a legend contained within it? I thought it would just further increase accessibility if it has a legend but that it is not compulsory to have one for each and every fieldset.

HTML 4.01 requires that the <legend> element be the first element in a <fieldset> element.

XHTML 1.0 does not require the <legend> element, but recommends it for accessibility reasons.

From http://www.w3.org/TR/html4/interact/forms.html

The FIELDSET element allows authors to group thematically related controls and labels. Grouping controls makes it easier for users to understand their purpose while simultaneously facilitating tabbing navigation for visual user agents and speech navigation for speech-oriented user agents. The proper use of this element makes documents more accessible.

The LEGEND element allows authors to assign a caption to a FIELDSET. The legend improves accessibility when the FIELDSET is rendered non-visually.

Some of us already said this a little less stalely - but nonetheless it is from the “authority”.

I’m glad I’m not the only one who reads the specs. :slight_smile:

Is your point that those two paragraphs don’t mention that the <legend> element is required in <fieldset> element?

Having had to write my fair shair of XML pages in addition to (X)HTML, I have it kind of beaten into my head that the tags are supposed to describe what they contain, so when I have the opportunity to do it properly in HTML, meaning have the right tags for the job, I try to use 'em. The genericness of a <div> is exactly why I wouldn’t use it to contain a form field and the styling hook I would need is provided by the semantic <fieldset>.

As far as the spec is concerned, I feel I don’t need anybody to explicitly tell me to do what I describe above for it to be the right thing to do, as evidenced by the nice structure rendered with CSS disabled.

I guess to some it is bloat, verboseness - to me it is just having an organized document.