Menu
Coddy logo textTech

Labels for Inputs

Part of the Fundamentals section of Coddy's HTML journey — lesson 39 of 60.

It’s not always very clear what an input is about without a label. A label gives users a clear visual cue on what type of input is expected in that field. Labels are created using the <label> tag.

Here’s the basic syntax:

<label for="field">Label text</label>
<input type="text" id="field">
  • <label>: The tag that creates the label.
  • for: A link between the label and a form field. The value of for should match the id of the form field.
  • Label text: The text that describes what the form field is for.
  • <input>: The form field that the label refers to.
  • id: The unique identifier for the form field, which matches the for attribute in the label.
challenge icon

Challenge

Easy

Add the following tags inside the <form> tag:

  • A <label> with the text 'First Name:' and the for attribute set to 'firstname'.
  • An <input> field with type="text".
  • The <input> field should have an id that matches the label's for attribute.

Cheat sheet

Labels provide clear visual cues for form inputs using the <label> tag:

<label for="field">Label text</label>
<input type="text" id="field">

The for attribute in the label must match the id attribute of the corresponding input field to create a proper connection.

Try it yourself

<!DOCTYPE html>
<html>
    <body>
        <form>
        <!-- Write code here -->
        
        </form>
    </body>
</html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals