Arkodeep Ray
Arkodeep Ray

Reputation: 174

How to indicate a class in HTML label tag

Here is some code to understand the question.

<body>
    <h2>This is form tutorial.</h2>
    <form action="backend.php">
        <label for="name">Name:</label>
        <div>
            <input type="text" name="myName" id="name">
        </div>
    </form>
</body>

So my question is in the line <label for="name">Name:</label> the for="name" line indicates the label Name: goes to the div which has the id name but is it possible to use the label tag in which the label will go to all the classes with the specified class

Upvotes: 0

Views: 852

Answers (1)

Quentin
Quentin

Reputation: 943579

HTML provides no means to associate a label with elements using a class.

A label is designed to label a single form control.

An ID identifies a single element.

A class groups multiple elements.

It makes sense for labels to use IDs for association, but it wouldn't make sense to use class for that.

If you want to group multiple form controls with a "label" then use a fieldset with a legend instead.

<fieldset>
    <legend>Name</legend>
    <input name="given-name" id="given-name"> <label for="given-name">Given name</label> 
    <br>
    <input name="family-name" id="family-name"> <label for="family-name">Family name</label> 
</fieldset>

(Although I use names for this example, it is a bit contrives and you should be aware of some of the issues programmers have dealing with names before doing anything involving them).

Upvotes: 1

Related Questions