Every calculator, search box, login page, and checkout starts the same way: a form. Forms are how a page asks for information.
The good news: HTML gives you most of the behavior for free. Validation, keyboard support, mobile keyboards, autofill — you get all of that by picking the right input type.
The form element
<form action="/submit" method="post">
<!-- fields go here -->
<button type="submit">Save</button>
</form>
actionis where the data goes when submitted.methodis how it goes:getputs data in the URL,postsends it in the request body.- A
<button type="submit">submits the form.
We’re not sending data anywhere yet, so our form will just stay on the page. The JavaScript part will take over the submit event later.
Labels are not optional
Every input needs a label. A label tells people what the field is for, and it makes the whole text clickable — click “Age” and the age field focuses.
<label for="age">Age</label>
<input id="age" name="age" type="number">
The for attribute on the label must match the input’s id. That’s the connection. If they don’t match, the label is just text.
The input types you’ll actually use
| Type | Use it for | Example |
|---|---|---|
text |
Names, search, short text | <input type="text"> |
number |
Numbers with min/max | <input type="number" min="1" max="120"> |
email |
Email addresses | <input type="email"> |
password |
Hidden text | <input type="password"> |
checkbox |
On/off, multiple choices | <input type="checkbox"> |
radio |
One choice from a group | <input type="radio"> |
range |
A slider | <input type="range" min="0" max="10"> |
date |
A date picker | <input type="date"> |
Useful attributes:
name— the key sent with the form. Without it, the field’s value is ignored.value— the starting value.required— the browser refuses to submit an empty field.min/max/step— limits and increments for numbers.checked— a radio or checkbox starts selected.disabled— can’t be used or submitted.
Groups: fieldset and legend
When several inputs belong together — like a set of radio buttons — wrap them in a <fieldset> with a <legend>:
<fieldset>
<legend>Sex</legend>
<label><input type="radio" name="sex" value="female"> Female</label>
<label><input type="radio" name="sex" value="male"> Male</label>
</fieldset>
The shared name="sex" is what makes them a group: only one can be selected at a time. The value is what gets submitted.
Build the calculator form
Here’s the full form. Try tabbing through it — every field should be reachable and every label should make sense.
Edit the code, then hit Run (or Ctrl + Enter) and watch the preview. This is a safe sandbox — you can't break the page.
Build the form
the plan's verify stepAdd the form to your calculator page, inside the first section. Include all the fields: age, height, weight, sex, activity, and goal. Use labels with matching for/id attributes, and wrap the radio buttons in a fieldset with a legend.
Test it with the keyboard: Tab should move through every field, Space should select a radio, and the labels should focus their inputs when clicked.
The mistakes everyone makes
- No label. Placeholders don’t count.
fordoesn’t matchid. The label looks fine but does nothing.- No
nameon an input. The value won’t be submitted. type="text"for numbers. You lose the numeric keyboard on phones and built-in validation.- Radio buttons with different
names. They’ll all be selectable at once. - A
<div>around every field instead of a<label>. Labels are interactive; divs aren’t. - No
requiredon fields that must be filled. Let the browser help you.
A form collects data; a submit button sends it.
Every input needs a label with a matching for/id.
Pick the input type that matches the data: number, email, date, radio, select.
name is the key; value is what gets sent.
fieldset + legend group related inputs, especially radios.
required, min, max, and step give you validation for free.
Next: the HTML checkpoint. We’ll put everything together into one complete calculator page — no styling, no behavior, just a solid structure.
Up next
HTML checkpoint: build the calculator page
Time to put the whole page together: header, form, results, food log table, and footer. It will look plain — that's exactly where we want to be before CSS.