Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Using Labels for Better Form Accessibility | HTML Forms and User Input
Ultimate HTML

bookUsing Labels for Better Form Accessibility

The <label> element is used to associate a label with an input field on a form. This association helps users understand the information being requested in the form field. When a user clicks on the label, it automatically focuses on the corresponding input field.

There are two ways to connect label and input elements:

Wrapping

You can nest an <input> inside a <label>, and the browser will link them automatically.

index.html

index.html

copy

Clicking "Name" focuses the input field.

Using the id Attribute

If the input cannot be placed inside the label, connect them manually:

index.html

index.html

copy

Here, the label uses for="name" and the input uses id="name". Matching values create the connection.

Note
Note

Using these methods, you establish a visual and semantic connection between the label and the input field. However, there may be situations where wrapping the input element within the label is impossible due to styling constraints or website logic. In such cases, you commonly rely on attribute connections to maintain flexibility in applying styles and implementing background logic.

Let's examine the difference between the two approaches by examining the code in the image.

Note
Note

From the browser's perspective, the chosen approach to establish the connection is inconsequential. Both methods achieve the same result of linking the label and input field together for improved usability and accessibility.

question mark

What is the purpose of the <label> element in HTML forms?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain the main differences between wrapping and using the id attribute for labels and inputs?

Which method is recommended for accessibility?

Are there any situations where one method is preferred over the other?

bookUsing Labels for Better Form Accessibility

Swipe to show menu

The <label> element is used to associate a label with an input field on a form. This association helps users understand the information being requested in the form field. When a user clicks on the label, it automatically focuses on the corresponding input field.

There are two ways to connect label and input elements:

Wrapping

You can nest an <input> inside a <label>, and the browser will link them automatically.

index.html

index.html

copy

Clicking "Name" focuses the input field.

Using the id Attribute

If the input cannot be placed inside the label, connect them manually:

index.html

index.html

copy

Here, the label uses for="name" and the input uses id="name". Matching values create the connection.

Note
Note

Using these methods, you establish a visual and semantic connection between the label and the input field. However, there may be situations where wrapping the input element within the label is impossible due to styling constraints or website logic. In such cases, you commonly rely on attribute connections to maintain flexibility in applying styles and implementing background logic.

Let's examine the difference between the two approaches by examining the code in the image.

Note
Note

From the browser's perspective, the chosen approach to establish the connection is inconsequential. Both methods achieve the same result of linking the label and input field together for improved usability and accessibility.

question mark

What is the purpose of the <label> element in HTML forms?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3
some-alt