Forms and Semantic HTML
Build accessible forms with labels, input types and buttons, and structure whole pages with semantic elements such as header, nav, main and footer.
Welcome back!
Step 1 of 12Learn
Forms: how pages collect input
Sign-ups, searches, checkouts and contact pages all use forms. A simple contact form:
<form action="/contact" 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="5"></textarea>
<button type="submit">Send message</button>
</form>Key parts:
- form wraps the fields. action is where the data is sent and method is how (post for data that changes something, get for searches).
- input is a void element whose type decides what it is.
- textarea is a multi-line text box.
- button type="submit" sends the form.
- name labels each value in the submitted data (name=Ana, email=ana@example.com).
The most important accessibility rule for forms: every field needs a label. A label's for attribute must match the input's id. Then screen readers announce "Email, edit text" when the field is focused, and clicking the label text puts the cursor in the field, a bigger target for everyone, especially on phones.
A placeholder (gray hint text inside the field) is not a label: it disappears as soon as someone starts typing.
Step 2 of 12Practice · Spot the mistake
Tap the line where the label is not correctly connected to its input.
Step 3 of 12Learn
Input types do a lot of work for free
The type attribute changes how an input looks, what keyboard phones show and what the browser checks before sending:
| Type | What you get |
|---|---|
| text | A plain one-line text field |
| Checks for an @ sign; phones show an email keyboard | |
| tel | Phones show a number pad |
| number | Numeric entry with optional min, max and step |
| password | Characters are hidden |
| date | A date picker in most browsers |
| checkbox | Tick boxes for yes/no or several choices |
| radio | Pick exactly one option from a group (same name) |
| file | Upload a file |
Validation attributes add basic checks without any JavaScript:
- required: the form won't submit while the field is empty.
- minlength and maxlength: limits on text length.
- min and max: limits on numbers and dates.
Browser checks improve the experience, but they're easy to bypass. The server must always validate data again, because anyone can send anything to a server.
For radio buttons and related checkboxes, wrap the group in a fieldset with a legend ("Preferred contact method"), so screen readers announce the question along with each option.
Step 4 of 12Practice · Match the pairs
Match each piece of information with the best input type.
Tap an answer, then tap the place where it belongs (or drag it there).
- A customer's email address
- A phone number on a mobile checkout
- Choosing one shirt size from S, M or L
- Agreeing to the terms and conditions
- A birth date
Step 5 of 12Flashcards
Input types worth knowing
Tap a card to flip it.
Step 6 of 12Learn
Semantic HTML: say what things are
Older websites were built from endless generic boxes: <div class="header">, <div class="nav">, <div class="footer">. A div has no meaning; it's just a container. HTML now has semantic elements that describe the role of each region:
<body>
<header>
<a href="/">Fresh Bread Co.</a>
<nav>
<ul>
<li><a href="/menu">Menu</a></li>
<li><a href="/visit">Visit</a></li>
</ul>
</nav>
</header>
<main>
<article>
<h1>Why we bake overnight</h1>
<p>...</p>
</article>
<aside>Opening hours...</aside>
</main>
<footer>© Fresh Bread Co.</footer>
</body>- header: introductory content, often the logo and navigation.
- nav: a block of major navigation links.
- main: the page's main content; use only one per page.
- article: a self-contained piece, like a blog post or news story.
- section: a thematic group of content, usually with its own heading.
- aside: related but secondary content, like a sidebar.
- footer: closing information such as contact details and copyright.
Semantic elements look exactly like divs until you style them, so why bother? Screen reader users can jump straight to the main content or the navigation. Search engines better understand which part is the real content. And your code is easier to read. Use div only when no semantic element fits, typically as a styling wrapper.
Step 7 of 12Practice · Sort into groups
Which semantic element fits each part of a news site page?
Tap an item, then tap its group (or drag it there).
Step 8 of 12Diagram
A semantic page from top to bottom
Tap a shape with to see more.
- header
Site logo, name and often the main navigation.
- nav
The main links around the site.
- main
The unique content of this page; only one per page.
- article
A self-contained piece, like a news story or blog post.
- aside
Related but secondary content, like a sidebar.
- footer
Copyright, contact details, secondary links.
Step 9 of 12Note
Step 10 of 12Tap to reveal
More semantic elements
section vs. div
Use section for a thematic block of content that would appear in an outline, usually with its own heading, such as "Pricing" or "Testimonials." Use div when you only need a box for styling.
address
Contact information for the page or article author, often placed in the footer.
details and summary
A built-in disclosure widget: the summary text is always visible and clicking it shows or hides the rest. Perfect for FAQs, with keyboard support included and no JavaScript needed.
button vs. a
A link (a) takes the user somewhere: another page or another part of the page. A button does something on the current page: submits a form, opens a menu, plays a video.
Step 11 of 12Practice · True or false
Placeholder text inside an input field can replace a visible label.
Step 12 of 12Practice · Choose one
A developer builds a page entirely from div elements with class names like "header" and "menu". It looks perfect. What is the main drawback?
Lesson complete!