What is a <label> tag?
Experience Level: Junior
Tags: HTML
Answer
The <label>
tag is used to render a label of an input element.
Imagine you have a form with two text fields - first name and last name. Each text field is be defined by <input type="text"/ >
tag. How will the user know what to enter into each text field when it isn't labeled?
The solution is to use the <label>
tag.
You could obviously use <span>
but <label>
is better for usability, because the screen readers can understand that <label>
is labeling some field whereas <span>
is understood to be just some text.
Related HTML job interview questions
What is a <ul> tag?
HTML JuniorWhat is a <input> tag?
HTML JuniorWhat is a <p> tag?
HTML JuniorWhat is a <span> tag?
HTML JuniorWhat is a <div> tag?
HTML Junior