Learn/HTML, CSS & JS/4. Forms and inputs
Lesson 4 of 8
Sign in

HTML, CSS & JS, lesson 4

Forms and inputs

Forms are how pages ask people for things: a search term, an email address, an order. HTML gives you the controls; the browser handles typing, focus and validation for free.

Inputs and labels

  • <input> is a single control. Its type changes what it is: text, email, number, password, checkbox, date and more. The right type gives people the right keyboard on phones and basic validation.
  • <label> names a control. Connect it with for set to the input's id. Then clicking the label focuses the input, and screen readers announce the label. Never leave an input without one; a is not a label.

One exercise to try what you just read.

Exercise 1

Order form

Press Ctrl Enter or Check to test your code.

placeholder
  • name is the key the value is sent under when the form is submitted.
  • required, min, max and maxlength let the browser check values before submitting.
  • Choices

    For a few options people should see at once, use radio buttons (type="radio") that share a name. Use a checkbox for a yes/no choice.

    Further reading

    • MDN: Your first form
    • MDN: The <input> element
    • MDN: The <label> element

    Build a <form> with:

    1. A text input with id="name" and a <label> connected to it, and make it required.
    2. An email input with id="email" and a connected label.
    3. A number input with id="qty", min="1" and max="12", with a connected label.
    4. A submit button with the text Order.
    <form>
      <label for="name">Your name</label>
      <input id="name" name="name" type="text" required>
    
      <label for="qty">How many pancakes?</label>
      <input id="qty" name="qty" type="number" min="1" max="12" value="3">
    
      <button type="submit">Order</button>
    </form>
    <label for="topping">Topping</label>
    <select id="topping" name="topping">
      <option>Maple syrup</option>
      <option>Berries</option>
    </select>