Form element anatomy and native controls
Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.
What you will be able to do
#- Choose the correct native control for a given input.
- Explain why native controls are preferred over custom div-based widgets.
Overview
#A form is a collection of controls the browser already knows how to operate. <form> groups them, <label> names them, and each control has a type that determines its behavior. Native controls come with keyboard support, mobile keyboards, autofill, validation and accessibility for free. Rebuilding them with divs means reimplementing all of that, usually incompletely.
The core controls are <input>, <textarea>, <select> and <button>, and <fieldset> groups related controls. The input type changes everything: type="email" shows an email keyboard on mobile, type="number" shows a numeric keypad, type="date" opens a date picker. Getting the type right is the cheapest accessibility and usability win available.
<form action="/subscribe" method="post">
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required />
<button type="submit">Subscribe</button>
</form>
Buttons default to type="submit" inside a form, so a button meant only for JavaScript must declare type="button" or it will submit the form. The name attribute is what the server receives; a control without a name is not submitted. The id links the label to the control, which is how clicking the label focuses the input and how assistive technology announces the name.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Build a login form with an email input, a password input and a submit button. Use correct input types, autocomplete hints and a label for each field.
starter.html<form action="/login" method="post"> <!-- add the controls --> </form>Reveal the reference answer
reference.html<form action="/login" method="post"> <label for="email">Email address</label> <input id="email" name="email" type="email" autocomplete="email" required /> <label for="password">Password</label> <input id="password" name="password" type="password" autocomplete="current-password" required /> <button type="submit">Sign in</button> </form>Authored expected output: Two labelled inputs with correct types and autocomplete tokens, plus a submit button.
The type selects the mobile keyboard, autocomplete lets password managers fill the fields, and label for links each label to its control.
Exercise 2Predict
Inside a form, what is the default type of a button element?
Common misconceptions
#- A div with a click handler is equivalent to a button element.
- The name attribute is optional because the id identifies the field.
Follow-up practice
#- html-css-07-labels-fieldsets
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.