图例标签在字段集中的位置重要吗?

Does the position of legend tag within fieldset matter?

我有一个字段集如下:

<fieldset>
    <legend>Colour</legend>
    <label><input type="radio" name="colour" value="blue"> Blue </label>
    <label><input type="radio" name="colour" value="red"> Red </label>
</fieldset>

在这里,如果我将 legend 标记保留在所有 label 标记下方,则 HTML 会呈现与之前完全相同的 fieldset

<fieldset>
    <label><input type="radio" name="colour" value="blue"> Blue </label>
    <label><input type="radio" name="colour" value="red"> Red </label>
    <legend>Colour</legend>
</fieldset>

即使这样呈现相同。

所以这里的问题是,在 fieldset 内的任何地方编写图例标记在编程上是否正确并符合标准。

So question here is, whether it is programmatically correct and as per standards to write legend tag at any place within fieldset.

不,不是。 legend 元素(如果存在) 必须fieldset 元素的第一个子元素。


HTML5 spec. 是这么说的:

4.10.15 The fieldset element
[...]

Content model:

Optionally a legend element, followed by flow content.


HTML4.01 spec.定义fieldset元素如下:

<!ELEMENT FIELDSET - - (#PCDATA,LEGEND,(<a href="https://www.w3.org/TR/html4/sgml/dtd.html#flow" rel="nofollow noreferrer">%flow;</a>)*) -- form control group -->

其中逗号分隔的列表定义了子元素必须出现的顺序,即 LEGEND 必须出现在其他流(块或内联)元素之前。