Chapter 14 of 15

HTML Forms: Inputs, Labels and Buttons

Forms let visitors send you information, like a contact message, a sign-up or a search.

A simple contact form

Label for email connects to input id email; the form sends email equals the typed address
for and id connect a label to its box. name is the label used when the data is sent.
A form sends data to a server, which shows a thank you page
HTML collects the data. A server (or a form service) receives it.
HTML
<form action="/send" method="post">
  <label for="name">Your name</label>
  <input type="text" id="name" name="name" required>

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

  <label for="msg">Message</label>
  <textarea id="msg" name="message" rows="4"></textarea>

  <button type="submit">Send</button>
</form>
  • action: where the data goes (a server address). method="post" sends it privately.
  • label for="name" connects to id="name". Clicking the label selects the box. Screen readers read it. Always use labels.
  • name: the name of the data when it is sent.
  • required: the browser will not send the form if it is empty.
Where does the data go?HTML alone cannot save form data. You need a server, or a free form service. Many hosting sites like Netlify have a simple form feature you can turn on.

Useful input types

TypeWhat it gives
textNormal one-line text
emailChecks for an @; phone shows the @ key
telPhone number keypad on mobile
numberOnly numbers
passwordHides the letters
dateA calendar to pick a date
checkboxTick box (pick many)
radioRound button (pick one)

Let the browser check the input

HTML can check many things before the form is sent. No JavaScript needed:

HTML
<input type="email" name="email" required>
<input type="text" name="pin" pattern="[0-9]{6}" title="6-digit PIN code">
<input type="password" name="pass" minlength="8">
<input type="number" name="age" min="18" max="99">
  • required: cannot be empty.
  • pattern: must match a rule. [0-9]{6} means exactly six digits, like an Indian PIN code.
  • minlength and maxlength: number of characters.
  • min and max: smallest and biggest number.

You can style fields that are not valid yet:

CSS
input:user-invalid { border-color: #dc2626; }
Still check on the serverBrowser checks help honest visitors fix mistakes. They do not stop bad data, because anyone can skip them. The server that receives the form must check the data again.

Placeholder is not a label

HTML
<!-- Not good: the hint disappears when you start typing -->
<input type="email" placeholder="Email">

<!-- Good: a real label, and the placeholder shows an example -->
<label for="email">Email</label>
<input type="email" id="email" placeholder="you@example.com">

Placeholder text disappears as soon as someone types, so they forget what the field was for. It is also light grey and hard to read. Always use a label. Use the placeholder only for an example.

Choices: select, radio and checkbox

HTML
<label for="city">City</label>
<select id="city" name="city">
  <option>Delhi</option>
  <option>Mumbai</option>
  <option>Pune</option>
</select>

<p>Plan:</p>
<label><input type="radio" name="plan" value="free" checked> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>

<label><input type="checkbox" name="news"> Send me the newsletter</label>

Radio buttons with the same name are one group, so only one can be picked.

Radio buttons with one choice selected, and checkboxes with two ticked
Radio buttons with the same name are one group, so only one can be picked.

Group related fields

HTML
<fieldset>
  <legend>Delivery address</legend>
  <label for="street">Street</label>
  <input id="street" name="street" autocomplete="street-address">
  <label for="city2">City</label>
  <input id="city2" name="city" autocomplete="address-level2">
</fieldset>

<fieldset> draws a box around related fields and <legend> gives the group a title. Screen readers read the legend before each field, so users know which address they are filling in.

autocomplete tells the browser what the field is for. The browser can then fill in the user's saved name, email and address with one tap. This makes forms much faster on phones.

Make the form look good

CSS
label { display: block; margin-top: 14px; font-weight: 600; }
input, textarea, select {
  width: 100%;
  padding: 10px 12px;
  font-size: 16px;            /* 16px stops zoom on iPhone */
  border: 2px solid #ccc;
  border-radius: 8px;
}
input:focus, textarea:focus { border-color: #2563eb; outline: none; }
button {
  margin-top: 16px;
  padding: 12px 24px;
  background: #2563eb; color: white;
  border: 0; border-radius: 999px; font-size: 16px;
}
Result in the browser
Your nameAshaSend

Receive form data for free with Netlify

If you host your site on Netlify, you can collect form entries without writing any server code. Add two attributes to the form:

HTML
<form name="contact" method="POST" data-netlify="true">
  <label for="n">Name</label>
  <input id="n" name="name" required>
  <label for="m">Message</label>
  <textarea id="m" name="message" required></textarea>
  <button type="submit">Send</button>
</form>

After you publish, the messages appear in your Netlify dashboard under Forms, and you can get an email for each one. The free plan has a monthly limit, which is enough for a small site.

Mini project: a sign-up form

HTML
<form class="signup">
  <h2>Join the newsletter</h2>

  <label for="su-name">Your name</label>
  <input id="su-name" name="name" autocomplete="name" required>

  <label for="su-email">Email</label>
  <input id="su-email" name="email" type="email" autocomplete="email"
         placeholder="you@example.com" required>

  <label><input type="checkbox" name="agree" required> I agree to get one email a week</label>

  <button type="submit">Sign up</button>
</form>
CSS
.signup {
  max-width: 380px;
  padding: 24px;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
}
.signup label { display: block; margin-top: 14px; font-weight: 600; }
.signup input:not([type="checkbox"]) {
  width: 100%; padding: 10px 12px; font-size: 16px;
  border: 2px solid #d1d5db; border-radius: 8px;
}
.signup button {
  margin-top: 18px; width: 100%; padding: 12px;
  background: #2563eb; color: white; border: 0; border-radius: 8px; font-size: 16px;
}

Common mistakes

ProblemFix
Clicking the label does nothingThe label's for must match the input's id exactly
Form sends but data is missingEvery field needs a name
Page zooms in on iPhone when typingMake input text at least 16px
Two radio buttons can both be pickedThey need the same name
A button inside a form sends it by accidentButtons send forms by default. Add type="button" to buttons that should not.

Practice time

  1. Build a contact form with name, email, message and a Send button.
  2. Add a select box and a group of radio buttons.
  3. Style the inputs with padding, round corners and a green focus border.
  4. Add a PIN code field that only accepts 6 digits.
  5. Add autocomplete to your form and test it on your phone.

Quick summary

  • Every input needs a label and a name.
  • Pick the right type so phones show the right keyboard.
  • HTML collects data; a server or form service receives it.