Lesson 08 / Forms

HTML Forms

Time
55 min
Type
Reading + Interactive
Level
Beginner
Use
Core

Use HTML form elements, input types, attributes, and accessibility practices to create user-friendly interactive interfaces.

Course Role

Core

Part of the main course path. Prioritize this before moving to optional polish or deeper references.

Teacher Notes / In-Class Use

Demo Live

  • Use the form lab to toggle labels, required fields, errors, and confirmation behavior one at a time.
  • Show how a label connects to an input through matching for and id values.

Try In Class

  • Have students build a small contact form with labels, inputs, a textarea, and a submit button.
  • Ask students to tab through the form and say each field purpose out loud.

Submit Or Check

  • Every input should have a visible label connected with for and id.
  • The form should point to a confirmation page since server-side processing is outside the class scope.

Watch For

  • Labels that look right visually but are not programmatically connected to inputs.
  • Placeholder text being used as the only label.

Learning Goals

  • Understand the form element as the container for form controls
  • Use input, textarea, select, option, label, and common form attributes
  • Create accessible forms with labels, instructions, keyboard support, and clear submission behavior

Interactive Demo

How to use this demo.

Use the demo as a small lab. Change one thing, observe the result, then connect it back to your own project.

What To Try

  • Toggle each form quality option on and off one at a time.
  • Tab through the preview after changing the label, required field, and error settings.

What Changes

  • The preview form, checklist, and code sample update together.
  • Missing labels and weak feedback become visible as accessibility and usability issues.

What To Notice

  • A form can look correct while still being hard to use with assistive technology.
  • Labels, requirements, and errors need to be communicated in markup, not only visually.

Apply It

  • Review one form in your project and confirm every input has a connected label.

Interactive Demo

Form Accessibility Lab

Toggle form details on and off to see how small markup choices change usability, validation, and screen reader support.

Enter your name before submitting.

  • Label: Pass
  • Required state: Pass
  • Error message: Pass
  • Submission target: Pass
<form action="thank-you.html" method="get">
  <label for="name">Name</label>
  <input id="name" name="name" required aria-describedby="name-error">
  <p id="name-error">Enter your name before submitting.</p>
  <button type="submit">Submit form</button>
</form>

What to notice

  • Placeholder text is not a replacement for a visible label.
  • Required fields should be communicated in code and visually.
  • Error messages need to be associated with the field they describe.

Try this

  • Turn off the label and explain what information disappears.
  • Submit with an empty name field and watch native validation respond.
  • Compare the generated markup to your own project forms.

This demo uses extra JavaScript for teaching. The code sample shows the pattern to practice. View full demo source.

Introduction

Forms are essential for creating interactive web experiences, allowing users to input data, submit information, and engage with your website.

This lesson introduces the HTML elements, attributes, labels, validation patterns, and accessibility practices that make forms usable.

Form Anatomy

A form needs a form element, labeled fields, useful name values, and a submit button.

For class projects without a backend, point the action to a confirmation page such as thank-you.html.

<form action="thank-you.html" method="get">
  <label for="email">Email</label>
  <input type="email" id="email" name="email" required>

  <button type="submit">Submit</button>
</form>

The form Element: The Container

The form element acts as a container for form controls. It defines where the data goes and how it is sent.

<form action="thank-you.html" method="get">
</form>

Form Attributes

  • action: Specifies the URL or page where the form data will be sent.
  • method: Defines how the data is sent, usually get or post.
  • enctype: Specifies how form data should be encoded when submitted.
  • target: Specifies where to display the response after submission.

For static class projects, use action="thank-you.html" so the form leads to a confirmation page.

Labels, for, and id

Every form control should have a visible label.

The label connects to the input when the label for value matches the input id value.

  • for="firstName" points to the field with id="firstName".
  • Connected labels help screen reader users and make the label clickable.
<label for="firstName">First Name</label>
<input type="text" id="firstName" name="firstName">

id vs. name

id and name do different jobs. Students often mix them up, so check both.

AttributeWhat it doesExample
idConnects the control to a label and can be used by CSS or JavaScript.id="email"
nameBecomes the key sent with the form data when the form submits.name="email"

Input Elements: Gathering User Data

The input element is the most versatile form element. Its behavior changes based on the type attribute.

Text Input

<label for="firstName">First Name:</label>
<input type="text" id="firstName" name="firstName" required placeholder="Enter your first name" autocomplete="given-name" autofocus>

Password Input

<label for="password">Password:</label>
<input type="password" id="password" name="password" required placeholder="Enter your password">

Email Input

<label for="email">Email Address:</label>
<input type="email" id="email" name="email" required placeholder="Enter your email" autocomplete="email">

Checkbox Input

<input type="checkbox" id="terms" name="terms" required>
<label for="terms">I agree to the terms and conditions.</label>

Radio Buttons

Radio buttons let users choose one option from a group. Inputs in the same radio group share the same name.

<fieldset>
  <legend>Preferred contact method</legend>

  <input type="radio" id="contactEmail" name="contactMethod" value="email">
  <label for="contactEmail">Email</label>

  <input type="radio" id="contactPhone" name="contactMethod" value="phone">
  <label for="contactPhone">Phone</label>
</fieldset>

Grouped Controls with fieldset and legend

Use fieldset and legend to group related controls, especially checkbox and radio groups.

<fieldset>
  <legend>Interests</legend>

  <input type="checkbox" id="design" name="interests" value="design">
  <label for="design">Design</label>

  <input type="checkbox" id="code" name="interests" value="code">
  <label for="code">Code</label>
</fieldset>

textarea

Use textarea when the user needs to enter multiple lines of text.

Avoid using <br> to space form fields. Use CSS for spacing instead.

<label for="comments">Comments</label>
<textarea id="comments" name="comments" rows="5" placeholder="Enter your comments here"></textarea>

select and option

Use select and option when the user should choose from a defined list.

<label for="country">Country</label>
<select id="country" name="country">
  <option value="">Choose a country</option>
  <option value="us">United States</option>
  <option value="ca">Canada</option>
  <option value="uk">United Kingdom</option>
</select>

Other Input Types

HTML includes input types that give browsers more information about the expected data.

TypeUse
telTelephone numbers.
urlWebsite URLs.
numberNumeric values.
dateDate picking.
fileFile uploads.
searchSearch fields.

Submit Buttons

A form needs a way to submit. Use a button with type="submit" for normal form submission.

  • type="submit" submits the form.
  • type="button" creates a button that does not submit unless JavaScript gives it behavior.
<button type="submit">Send</button>
<button type="button">Open menu</button>

Placeholders Are Not Labels

placeholder text can give an example, but it should not replace a visible label.

Placeholder text disappears when users type and can be harder to read.

<label for="email">Email</label>
<input type="email" id="email" name="email" placeholder="name@example.com">

Autocomplete

autocomplete helps browsers fill common information faster and more accurately.

<label for="email">Email</label>
<input type="email" id="email" name="email" autocomplete="email">

<label for="firstName">First Name</label>
<input type="text" id="firstName" name="firstName" autocomplete="given-name">

HTML Validation

HTML can provide basic validation before a form submits.

AttributeUse
requiredRequires a value before submission.
type="email"Checks for an email-like format.
minlength and maxlengthSets text length limits.
min and maxSets number or date limits.
patternMatches a custom pattern. Use carefully because it can be hard to write and explain.

Helper Text with aria-describedby

Use helper text when a field needs instructions. Connect the helper text to the field with aria-describedby.

<label for="email">Email</label>
<p id="email-help">Use your school email address.</p>
<input id="email" name="email" type="email" aria-describedby="email-help">

Accessibility Considerations

  • Use label elements for every form field.
  • Provide clear instructions and error messages.
  • Use native HTML first. Add ARIA only when it provides information HTML does not.
  • Ensure keyboard navigation and focus management.
  • Make sure focus states are visible.

Form Submission

When a form is submitted, data is sent to the location in the action attribute. Since this class doesn't cover server-side languages, forms should point to a confirmation page like thank-you.html.

If you use method="get", you can inspect the submitted values in the URL query string after submission.

Example: Contact Form

<form action="thank-you.html" method="get">
  <label for="name">Name</label>
  <input type="text" id="name" name="name" autocomplete="name" required>

  <label for="email">Email</label>
  <input type="email" id="email" name="email" autocomplete="email" required>

  <label for="message">Message</label>
  <textarea id="message" name="message" rows="5" required></textarea>

  <button type="submit">Send message</button>
</form>

Common Mistakes

MistakeWhy it mattersFix
Input missing labelThe field may be unclear, especially for assistive technology users.Add a visible label.
for does not match idThe label is not connected to the input.Make the values match exactly.
Missing nameThe value may not be included when the form submits.Add a useful name to every submitted control.
Placeholder used as the only labelThe instruction disappears when typing begins.Use a visible label; keep placeholder text optional.
Checkbox or radio group missing fieldsetThe group question may be unclear.Wrap related options in fieldset with a legend.
Submit button missingUsers may not know how to send the form.Add <button type="submit">Submit</button>.
Form action points nowhereSubmitting creates a confusing result.Use action="thank-you.html" for class projects.
Using autofocus too casuallyFocus may jump unexpectedly when the page loads.Use autofocus only when it truly improves the experience.

Checkpoint

Before moving on, make sure these feel true.

  • I can build a form with labels connected to inputs.
  • I can choose useful input types and attributes.
  • I can test the form with a keyboard before styling it.

Project Connection

This lesson supports current class projects.

Practice

  • Build a contact form with text, email, textarea, and select controls.
  • Add one checkbox or radio group using fieldset and legend.
  • Add a submit button with type="submit".
  • Set action="thank-you.html" and use method="get".
  • Check that every form control has a visible label.
  • Check that every label for value matches an input id.
  • Check that every submitted control has a name.
  • Test the form with keyboard only.
  • Submit the form and inspect the query string in the browser URL.