Favorites

Form Basics - HTML Tutorial

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

Let's start with a simple form that asks for a visitor's name and has a button to send it. Along the way, you'll meet the four elements that almost every form uses.

Your First Form

Example: A Simple Form
<form action="" method="get">
  <label for="name">Your name</label>
  <input type="text" id="name" name="name">

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

Save this in a practice file and open it in your browser. Type your name and click Send. The page reloads, and if you look at the address bar, you'll see something like ?name=Sam on the end of the URL. That's your form data. Let's look at how each part works.

The form Element

The form element wraps all of the fields and buttons that belong together. It has two key attributes:

  • action is the URL the form data is sent to. On a real site, this is usually a script on the server that processes the data. An empty value sends the data back to the current page, which is handy for practice.
  • method is how the data is sent:
    • get adds the data to the end of the URL, as you just saw. It's good for searches and filters, because the results page can be bookmarked or shared.
    • post sends the data separately from the URL. Use it for anything that changes something on the server, such as sign-ups, messages and orders, and for anything private, such as passwords.

The input Element

input creates a form field. It's a void element, so it has no closing tag. Its most important attributes are:

  • type sets the kind of field. text is a single line of text. You'll meet many other types in Form Controls.
  • name is the label the server receives along with the value. In ?name=Sam, "name" comes from this attribute. A field without a name is not sent at all, so don't forget it.
  • id uniquely identifies the field on the page, so a label can be connected to it.

The label Element

Every field needs a label that says what to enter. To connect a label to its field, set the label's for attribute to the same value as the field's id:

<label for="name">Your name</label>
<input type="text" id="name" name="name">

Connecting labels properly matters for two reasons:

  • Screen readers read the label aloud when the field is selected, so the visitor knows what to type.
  • Clicking the label puts the cursor in the field. For small controls like checkboxes, this makes a much bigger target to click or tap.

You can also connect a label by wrapping the field inside it, with no for or id needed:

<label>
  Your name
  <input type="text" name="name">
</label>
Caution

The placeholder attribute shows faint example text inside an empty field, such as placeholder="e.g. Sam Smith". It's not a replacement for a label: it disappears as soon as someone starts typing, it's often low in contrast and some screen readers ignore it. Always use a real label, and treat placeholders as an optional extra hint.

The button Element

A button with type="submit" sends the form. The text between the tags appears on the button, so make it describe the action, such as "Send message" or "Subscribe" rather than just "Submit".

Visitors can also submit a form by pressing Enter while they're in a text field.

Grouping Fields with fieldset

In longer forms, a fieldset groups related fields together, and its legend gives the group a caption. Browsers draw a border around the group by default:

Example: A Contact Form
<form action="" method="post">
  <fieldset>
    <legend>Your details</legend>

    <p>
      <label for="full-name">Full name</label>
      <input type="text" id="full-name" name="full-name" autocomplete="name">
    </p>

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

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

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

This form introduces a few new things:

  • Each label and field pair is wrapped in a p, which puts them on their own line with some space between them. A div works just as well.
  • The autocomplete attributes tell the browser what kind of information each field wants, so it can offer to fill it in from the visitor's saved details.
  • The textarea element creates a multi-line text box. Unlike input, it has a closing tag.

Basic Form Styling

Browsers give form fields a small font and a narrow width by default. A few CSS rules make forms much more comfortable to use:

input,
textarea,
select,
button {
  font: inherit;          /* use the same font as the rest of the page */
}

label {
  display: block;         /* put each label on its own line, above its field */
  margin-bottom: 0.25rem;
}

input[type="text"],
input[type="email"],
textarea {
  width: 100%;
  max-width: 30rem;
  padding: 0.5rem;
}

input[type="text"] is an attribute selector. It selects only input elements whose type is text, so checkboxes and other controls aren't stretched to full width.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee