4

Back in the IE6 days, I used to simulate Windows-style Group Boxes (see example) in HTML by creating a div with a border, and positioning a span with the text over the border with a solid background color to "erase" the box border. It was trivial to position the group text at the top or bottom of the box.

enter image description here

This technique worked well, except in cases where the background was not a solid color. I realize the <fieldset> and <legend> tags provide the exact same behavior I am describing, except non-solid backgrounds are supported but the text can only go above the fieldset.

The fieldset and legend combo seems to work magically such that whatever the legend touches will be erased from the fieldset border (never drawn actually). Why does it do this? I have found examples from W3 that illustrate the behavior, but I cannot find anything in the HTML4/5 spec that instructs the UA to "calculate the width of the legend and don't draw the border there." Is this behavior defined anywhere, or have the browser vendors unanimously decided it should draw this way?

If the behavior is not defined... I want to replace the old IE6 code with something modern, but I'm not sure if I should use fieldset (even apart from a form) or some CSS feature I am not aware of to erase borders just like fieldset implementations do already. What do the standards offer to either depend on fieldset or emulate it reliably?

jimp
  • 16,999
  • 3
  • 27
  • 36

1 Answers1

5

The position of the legend is standard. HTML5 rendering says

A fieldset element's rendered legend, if any, is expected to be rendered over the top border edge of the fieldset element as a 'block' box (overriding any explicit 'display' value).

It doesn't explicitly say that the border behind the legend is erased, but otherwise the legend would appear to have a line-through decoration. So browsers erase the border.

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • At least the spec says the legend should be applied over the top border--not above it, inside it, optional, etc. That should be good enough to depend on it to render the way my previous code did. Thanks! – jimp Sep 20 '16 at 16:03