Semantic use of

2019-04-01 01:08发布

I have a doubt about using the tag label.

I have a div acting as a widget that simulates an advanced dropdown-list.

<div class="control">
    <label class="description">Choose:</label>
    <div class="widget__list">...some other content...</div>
</div>

Here the W3C specifications (http://www.w3.org/TR/html401/interact/forms.html#h-17.9) say:

The LABEL element is used to specify labels for controls that do not have implicit labels

(UPDATE: Please don't rely on the (outdated) quote above. Refer to HTML5 specs linked below)

Scrolling up to the "Controls" section (http://www.w3.org/TR/html401/interact/forms.html#h-17.2) I can see that in the list of "control types" there is the type menu.

What I wonder is:

  • does my div-widget belong to the category of menu controls?
  • if yes, should my widget be wrapped into a form element, even if I don't have any HTML native control (input, select, button, etc.) in it?
  • am I using incorrectly the label element (not being in the context of a form), and should I maybe change it to span?

What are the most semantically formal answers to these questions?

EDIT:

The quote refers to the HTML4 specs, leading to raise the above questions. The current HTML5 specs for label gives already the answers that I needed (thanks @PeterKrauss).

1条回答
劫难
2楼-- · 2019-04-01 01:44

Your document is valid according to the W3C validator.

That means your label case isn’t defined as an error in the current HTML standard. So you shouldn’t worry more about the semantics, and just use it as you have here—if it’s natural for you and if it works as expected.

Scrolling up to the "Controls" section (http://www.w3.org/TR/html401/interact/forms.html#h-17.2)

You don’t want to use the old HTML4 spec for anything. It was in published in 1999.

The current HTML standard is at https://html.spec.whatwg.org/multipage/. The section of that spec on the label element is at https://html.spec.whatwg.org/multipage/forms.html#the-label-element.

does my div-widget belong to the category of menu controls?

The current HTML standard doesn’t define a category called “menu controls“; so it doesn’t matter whether the label element fits into that as a category.

am I using incorrectly the label element (not being in the context of a form), and should I maybe change it to span?

You’re not using it incorrectly. If you were, the W3C validator would flag it as an error. In other words, your case isn’t defined as an error in the current HTML standard. So you’re good to go.

查看更多
登录 后发表回答