Is the <fieldset> + <legend> top border “erasing” behavior defined by any standard (HTML or CSS)?

喜欢而已 提交于 2019-12-07 17:35:26

问题


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.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/39541432/is-the-fieldset-legend-top-border-erasing-behavior-defined-by-any-standa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!