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
for and id connect a label to its box. name is the label used when the data is sent.<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 toid="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.
Useful input types
| Type | What it gives |
|---|---|
text | Normal one-line text |
email | Checks for an @; phone shows the @ key |
tel | Phone number keypad on mobile |
number | Only numbers |
password | Hides the letters |
date | A calendar to pick a date |
checkbox | Tick box (pick many) |
radio | Round button (pick one) |
Let the browser check the input
HTML can check many things before the form is sent. No JavaScript needed:
<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.minlengthandmaxlength: number of characters.minandmax: smallest and biggest number.
You can style fields that are not valid yet:
input:user-invalid { border-color: #dc2626; }Placeholder is not a label
<!-- 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
<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.
name are one group, so only one can be picked.Group related fields
<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
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;
}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:
<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
<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>.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
| Problem | Fix |
|---|---|
| Clicking the label does nothing | The label's for must match the input's id exactly |
| Form sends but data is missing | Every field needs a name |
| Page zooms in on iPhone when typing | Make input text at least 16px |
| Two radio buttons can both be picked | They need the same name |
| A button inside a form sends it by accident | Buttons send forms by default. Add type="button" to buttons that should not. |
Practice time
- Build a contact form with name, email, message and a Send button.
- Add a select box and a group of radio buttons.
- Style the inputs with padding, round corners and a green focus border.
- Add a PIN code field that only accepts 6 digits.
- Add
autocompleteto your form and test it on your phone.
Quick summary
- Every input needs a
labeland aname. - Pick the right
typeso phones show the right keyboard. - HTML collects data; a server or form service receives it.