Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a label only refer to input elements?

Tags:

html

label

w3c

W3Schools have this to say about labels:

The <label> tag defines a label for an input element. [Emphasis mine]

Does this mean that the following HTML isn't valid?

<!doctype html>
<html>
    <head>
         <title>Example document</title>
    </head>
    <body>
        <label for="x">Label</label> 
        <hr>
        <div id="q" contentEditable="true">Hello</div>
        <hr>
        <div id="x" contentEditable="true">World</div>
    </body>
</html>

Both Chrome and IE8 give focus to World when Label is clicked, Firefox does not. Which is correct?

like image 811
Motti Avatar asked Jul 07 '09 19:07

Motti


People also ask

Can you use label without input?

Not all inputs need labelsAn input with type="hidden" is also fine without a label. But all other inputs, including <textarea> and <select> elements, are happiest with a label companion.

What is the label elements used for?

The <label> element is used to associate a text label with a form <input> field. The label is used to tell users the value that should be entered in the associated input field.

Which tag defines a label for an input element?

The <label> tag defines a label for several elements: <input type="checkbox"> <input type="color">

What is input type label?

The <label> tag defines the label for <button>, <input>, <meter>, <output>, <progress>, <select>, or <textarea> element. The <label> tag can be used in two ways: Firstly, use <label> tag by providing the <input> and id attribute. The <label> tag needs a for attribute whose value is the same as input id.


1 Answers

According to the W3C it applies to Form Controls, and Form Controls are defined as:

  • Buttons
  • Checkboxes
  • Radio buttons
  • Menus
  • Text input
  • File select
  • Hidden controls
  • Object tags

So FireFox is technically right, although I'd hardly consider it to be "breaking" if a browser didn't restrict it to those elements.

like image 51
Steven Robbins Avatar answered Oct 12 '22 04:10

Steven Robbins