<label>: The Label element - HTML: HyperText Markup Language | MDN (2024)

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The <label> HTML element represents a caption for an item in a user interface.

Try it

Associating a <label> with a form control, such as <input> or <textarea> offers some major advantages:

  • The label text is not only visually associated with its corresponding text input; it is programmatically associated with it too. This means that, for example, a screen reader will read out the label when the user is focused on the form input, making it easier for an assistive technology user to understand what data should be entered.
  • When a user clicks or touches/taps a label, the browser passes the focus to its associated input (the resulting event is also raised for the input). That increased hit area for focusing the input provides an advantage to anyone trying to activate it — including those using a touch-screen device.

To explicitly associate a <label> element with an <input> element, you first need to add the id attribute to the <input> element. Next, you add the for attribute to the <label> element, where the value of for is the same as the id in the <input> element.

Alternatively, you can nest the <input> directly inside the <label>, in which case the for and id attributes are not needed because the association is implicit:

html

<label> Do you like peas? <input type="checkbox" name="peas" /></label>

The form control that a label is labeling is called the labeled control of the label element. Multiple labels can be associated with the same form control:

html

<label for="username">Enter your username:</label><input id="username" name="username" type="text" /><label for="username">Forgot your username?</label>

Elements that can be associated with a <label> element include <button>, <input> (except for type="hidden"), <meter>, <output>, <progress>, <select> and <textarea>.

Attributes

This element includes the global attributes.

for

The value of the for attribute must be a single id for a labelable form-related element in the same document as the <label> element. So, any given label element can be associated with only one form control.

Note: To programmatically set the for attribute, use htmlFor.

The first element in the document with an id attribute matching the value of the for attribute is the labeled control for this label element — if the element with that id is actually a labelable element. If it is not a labelable element, then the for attribute has no effect. If there are other elements that also match the id value, later in the document, they are not considered.

Multiple label elements can be given the same value for their for attribute; doing so causes the associated form control (the form control that for value references) to have multiple labels.

Note: A <label> element can have both a for attribute and a contained control element, as long as the for attribute points to the contained control element.

Styling with CSS

There are no special styling considerations for <label> elements — structurally they are simple inline elements, and so can be styled in much the same way as a <span> or <a> element. You can apply styling to them in any way you want, as long as you don't cause the text to become difficult to read.

Accessibility

Interactive content

Don't place interactive elements such as anchors or buttons inside a label. Doing so makes it difficult for people to activate the form input associated with the label.

Don't do this:

html

<label for="tac"> <input id="tac" type="checkbox" name="terms-and-conditions" /> I agree to the <a href="terms-and-conditions.html">Terms and Conditions</a></label>

Prefer this:

html

<label for="tac"> <input id="tac" type="checkbox" name="terms-and-conditions" /> I agree to the Terms and Conditions</label><p> <a href="terms-and-conditions.html">Read our Terms and Conditions</a></p>

Headings

Placing heading elements within a <label> interferes with many kinds of assistive technology, because headings are commonly used as a navigation aid. If the label's text needs to be adjusted visually, use CSS classes applied to the <label> element instead.

If a form, or a section of a form needs a title, use the <legend> element placed within a <fieldset>.

Don't do this:

html

<label for="your-name"> <h3>Your name</h3> <input id="your-name" name="your-name" type="text" /></label>

Prefer this:

html

<label class="large-label" for="your-name"> Your name <input id="your-name" name="your-name" type="text" /></label>

Buttons

An <input> element with a type="button" declaration and a valid value attribute does not need a label associated with it. Doing so may actually interfere with how assistive technology parses the button input. The same applies for the <button> element.

Examples

Defining an implicit label

html

<label>Click me <input type="text" /></label>

Defining an explicit label with the "for" attribute

html

<label for="username">Click me to focus on the input field</label><input type="text" id="username" />

Technical summary

Content categories Flow content, phrasing content, interactive content, form-associated element, palpable content.
Permitted content Phrasing content, but no descendant label elements. No labelable elements other than the labeled control are allowed.
Tag omission None, both the starting and ending tag are mandatory.
Permitted parents Any element that accepts phrasing content.
Implicit ARIA role No corresponding role
Permitted ARIA roles No role permitted
DOM interface HTMLLabelElement

Specifications

Specification
HTML Standard
# the-label-element

Browser compatibility

BCD tables only load in the browser

<label>: The Label element - HTML: HyperText Markup Language | MDN (2024)
Top Articles
iCloud vs. Google Drive Comparison - 2024
IRS tax bracket changes could mean your paycheck is slightly bigger in 2024. Here's why
Creepshotorg
This website is unavailable in your location. – WSB-TV Channel 2 - Atlanta
Access-A-Ride – ACCESS NYC
What happened to Lori Petty? What is she doing today? Wiki
Miss Carramello
A Fashion Lover's Guide To Copenhagen
Pwc Transparency Report
Capitulo 2B Answers Page 40
Mens Standard 7 Inch Printed Chappy Swim Trunks, Sardines Peachy
Tcgplayer Store
Michigan cannot fire coach Sherrone Moore for cause for known NCAA violations in sign-stealing case
Trac Cbna
How do I get into solitude sewers Restoring Order? - Gamers Wiki
Google Doodle Baseball 76
Acts 16 Nkjv
Promiseb Discontinued
Robeson County Mugshots 2022
U Of Arizona Phonebook
Lakewood Campground Golf Cart Rental
Somewhere In Queens Showtimes Near The Maple Theater
Panolian Batesville Ms Obituaries 2022
Amazing Lash Studio Casa Linda
Like Some Annoyed Drivers Wsj Crossword
PCM.daily - Discussion Forum: Classique du Grand Duché
Weve Got You Surrounded Meme
Boise Craigslist Cars And Trucks - By Owner
Watertown Ford Quick Lane
Truvy Back Office Login
2015 Kia Soul Serpentine Belt Diagram
Wonder Film Wiki
Imagetrend Elite Delaware
The Posturepedic Difference | Sealy New Zealand
Transformers Movie Wiki
Orange Pill 44 291
Solve 100000div3= | Microsoft Math Solver
The 50 Best Albums of 2023
The best Verizon phones for 2024
Scottsboro Daily Sentinel Obituaries
19 Best Seafood Restaurants in San Antonio - The Texas Tasty
Craigs List Hartford
If You're Getting Your Nails Done, You Absolutely Need to Tip—Here's How Much
Sand Castle Parents Guide
Florida Lottery Claim Appointment
Thotsbook Com
Deezy Jamaican Food
The Sports Academy - 101 Glenwest Drive, Glen Carbon, Illinois 62034 - Guide
Waco.craigslist
Maurices Thanks Crossword Clue
Noaa Duluth Mn
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 5708

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.