Detailed introduction to the label tag in HTML
label label introduction
The label label defines the label (mark) for the input element, it will not Present any special effects to the user, similar to the span tag. But the biggest difference between the label label and the span label is that it improves usability for mouse users and can be associated with specific form controls.
After the label label is associated with a specific form control, if the user clicks the text within the label element, the associated form control will be triggered. That is to say, when the user selects the label label, the browser will automatically turn the focus to the form control related to the label label.
Main usage scenarios
label labels are often used to associate with checkbox or radio, so that the checkbox or radio can be selected/cancelled by clicking text. As shown in the figure below, clicking on text has the same effect as clicking on the previous radio button, that is, the clickable area of the control is enlarged, because clicking on the label or control will activate the control, which is especially useful for check boxes and radio buttons.
How label labels are associated with specific form elements
There are two main ways to associate label labels, Display association Implicit association with :
Method 1: Explicit association
The explicit association is through the label tagfor Property that is explicitly associated with another form control. It should be noted that the value of the for attribute must be the id of the markable form element in the same document as the label label. Note that it is the id rather than the name. For example:
爱好: <input type='checkbox' name='basket' id='basketball'> <label for="basketball">篮球</label> <input type='checkbox' name='football' id='football'> <label for="football">足球</label>
Rendering:
Implicit association
The implicit association is to directly place the form control on the label Within the label, in this case, the label label can only contain one form element, and multiple ones are only valid for the first one. As follows:
<label>点击我可以使文本框获得焦点 <input type='text' name='theinput' id='theinput'></label>
The rendering is as follows. Click on the text to focus the text box:
The advantages and disadvantages of display association and implicit association:
Explicit association advantages:
- Can reduce the number of label nesting levels
- label labels and forms can be in different locations
Disadvantages of display association:
- The control needs to define the id attribute
- The label label and the form control are not conducive to control as a whole
Implicit association Advantages:
- The control does not need to define an id
- The label and control are convenient to control as a whole
Implicit association Disadvantages:
- Increased the number of label nesting levels
- You cannot place labels and associated controls in different locations
The above are my personal views on the two methods, you can use them as needed Choose to be explicit or implicit.
Browser support for label tags and associated form elements
All major browsers support label tags. Safari 2 or earlier does not support label tags.
The form elements that can be used to display associations are:
input type="text"
text box. When the label is clicked, the associated text box gets focus.input type="checkbox"
Checkbox, check or uncheck the checkbox when clicking the label.input type="radio"
Radio button, select the radio button when clicking the label.input type="file"
File upload, the file selection dialog box will be opened when the label is clicked.input type="password"
Password box, the password box gets focus when the label is clicked.textarea
Text area, the text area gets focus when the label is clicked.select
Drop-down box, when the label is clicked, the drop-down box gets focus, but the drop-down box options are not expanded.
The form attribute of the label label
The form attribute specifies the form to which the label label element belongs. As shown below, although the label label is outside the myform form, it still belongs to the myform form. As follows:
<form action="http://songguoliang.com/test.html" id="myform"> <input type="radio" name="sex" id="male" value="male"> <br> <label for="female">女</label> <input type="submit" value="提交"></form><label for="male" form="myform">男</label>
The rendering is as follows, click "Male" to also select the first radio button
Note:
This form attribute has been changed in 2016 Removed from the HTML specification on April 28. However, scripts can still access the read-only HTMLLabelElement.form property; it returns the form that the label's associated control is a member of, or null if the label is not associated with a control or the control is not part of the form.
Thank you everyone for reading, I hope you will benefit a lot.
This article is reproduced from: https://pocket.blog.csdn.net/article/details/72852150
Recommended tutorial: "HTML Tutorial"
The above is the detailed content of Detailed introduction to the label tag in HTML. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
