<label>

HTML <label> Tag

The <label> tag defines a text label for the <input> tag. The label is a normal text, by clicking which, the user can select the form element. It facilitates the use of the form, since it is not always convenient to get into form elements with the cursor.

The <label> tag is also used to define keyboard shortcuts and jump to the active element like links.

An input can be associated with several labels. If you click or tap a <label> which is associated with a form control, the resulting click event will arise for the associated control.

Associating a <label> with an <input> element has some advantages:

  • The label text is both visually and pragmatically associated with the text input.

  • You can click on the associated label for focusing or activating the input, as well as on the input itself.

Syntax

Syntax

The <label> tag comes in pairs. The content is written between the opening (<label>) and closing (</label>) tags.

There are two ways to associate a text label and the form to which it belongs:

Set the identifier (id) inside the <input> element and specify its name as a for attribute for the <label> tag.

Example of the HTML <label> tag:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <form>
      <label for="lfname">First name:</label> 
      <input id="lfname" name="fname" type="text" />
    </form>
  </body>
</html>

Result

Insert the <input> into the <label> element.

Example of the HTML <label> tag with an <input> element inside:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <form>
      <label>Name
        <input id="User" name="Name" type="text" />
      </label>
    </form>
  </body>
</html>

Example of HTML <form> with a <label> tag used with radio type of an <input> tag:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <form>
      <label for="barca">Barcelona</label>
      <input type="radio" name="team" id="barca" value="Barcelona">
      <br />
      <label for="real">Real Madrid</label>
      <input type="radio" name="team" id="real" value="Real Madrid">
      <br />
    </form>
  </body>
</html>

Example of the HTML <label> tag with CSS font properties:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      body {
       padding: 20px;
      }
      label {
        font-size: 20px;
        font-weight: 700;
        color: #1c87c9;
      }
      input {
        width: 50%;
        height: 28px;
        padding: 4px 10px;
        border: 1px solid #666;
        background: #cce6ff;
        color: #1c87c9;
        font-size: 16px;
      }
    </style>
  </head>
  <body>
    <form>
      <label>Your  Name:</label>
      <input id="User" name="Name" type="text"/>
    </form>
  </body>
</html>

Attributes

Attributes

AttributeValueDescription
accesskeyaccesskeyDefines a hot key with which you can go to the attached to the label (through a for attribute) form element.
forelement_idSets the ID of the element to which the label should be bound.
formform_idSpecifies the form (s) with which the label will be associated. This attribute allows you to place tags in an arbitrary location in the document, and not just as a descendant of the <form> element.
Obsolete.

The <label> element also supports the Global Attributes and the Event Attributes.

How to style <label> tag? Common properties to alter the visual weight/emphasis/size of text in <label> tag:

CSS font-style property sets the style of the font. normal | italic | oblique | initial | inherit. CSS font-family property specifies a prioritized list of one or more font family names and/or generic family names for the selected element. CSS font-size property sets the size of the font. CSS font-weight property defines whether the font should be bold or thick. CSS text-transform property controls text case and capitalization. CSS text-decoration property specifies the decoration added to text, and is a shorthand property for text-decoration-line, text-decoration-color, text-decoration-style.

Coloring text in <label> tag:

CSS color property describes the color of the text content and text decorations. CSS background-color property sets the background color of an element.

Text layout styles for <label> tag:

CSS text-indent property specifies the indentation of the first line in a text block. CSS text-overflow property specifies how overflowed content that is not displayed should be signalled to the user. CSS white-space property specifies how white-space inside an element is handled. CSS word-break property specifies where the lines should be broken.

Other properties worth looking at for <label> tag:

CSS text-shadow property adds shadow to text. CSS text-align-last property sets the alignment of the last line of the text. CSS line-height property specifies the height of a line. CSS letter-spacing property defines the spaces between letters/characters in a text. CSS word-spacing property sets the spacing between words.



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部