Is it important to use

2020-06-01 10:37发布

问题:

Excuse me for the maybe very stupid question, but my curiosity is killing me, and plus I'm new to this, but are using labels important in your markup, and why?

i.e.

<label for="birthdate">Birthdate:</label>
<input type="text" id="birthdate" name="birthdate" />

Why must I label this input, why is it beneficial to the user, why is going to be beneficial for future use, or is it search engine optimization thing. That's what I really wanna know :)

回答1:

It's important for a number of reasons:

  • Clicking the label focuses on the
    text field, which is something a lot of users expect.
  • It's helpful for the accessibility reasons.
  • How else is the user going to know which field is which? You could use just text or a span or something, but why would you?
  • It leads to a more semantic markup.


回答2:

It helps for accessibility, e.g. screen-readers.

Also for things like checkboxes it allows the user to click on the label and not just the checkbox itself (Try it!).



回答3:

It's important for accessibility, so blind people using screen readers can easily tell which text box is meant for which thing, since possibly otherwise their software cannot tell the purpose of the active text box from the page structure. Also, clicking on the label will focus the appropriate input control, which is convenient.



回答4:

Future use:

In a case where the input html form is directly linked with database ( happens in frameworks)

so the input form variables directly represent database columns.

So, instead of showing database column names to the user in the form, we can show simplified names to the user using Labels.