Forms cheat sheet
Tags
-
<form method="post" action="…">Tag that wraps around all the form elements.
actionpoints to where the data should be submitted.
-
<fieldset>Used to group elements together, like radio buttons.
Must always have a legend.
-
<legend>Adds a title to the fieldset.
Must be inside a fieldset.
-
<button type="submit">Every form needs a button.
-
<label for="…">Adds a name to any field.
Every field must have a label.
-
<input id="…" type="…">Adds an input field of varying types.
Must always have an
idto associate a label.The
type="…"attribute defaults totext.
-
<textarea id="…">Adds a large, multi-line text field.
Must always have an
idto associate a label.
-
<select id="…">Creates a drop-down choice input.
Populate the choices with
<option>tags.Must always have an
idto associate a label.
-
<datalist id="…">Creates a list of items for autocompletion.
Populate the choices with
<option>tags.Won’t be visible until the associated field is typed into.
-
<option>Creates an entry inside
<select>or<datalist>.Use the
selectedattribute to set a default value.For select:
<option>Triceratops</option>For datalist:
<option value="Pteranodon">
-
<optgroup label="…">Creates a group of options inside a
<select>.label="…"is used as a visible name for the group.
-
<output for="…">Represents the result of a calculation performed by JavaScript.
for="…"is a space-separate list of inputids that contributed to the calculation.
Input types
-
textdefaultSingle line text field.
If you want, you can leave
type="text"off the input.
-
numberAccepts numbers; has a up/down switch.
-
emailAccepts valid email addresses.
-
telFor telephone numbers.
There is no restricted format to accommodate all different countries.
-
urlAccepts a valid website URL.
-
passwordFor passwords; hides typed characters.
-
timeFor accepting time: hours, minutes, seconds.
-
dateFor accepting dates; shows a calendar picker.
-
datetime-localFor accepting a both a date and a time; shows a calendar & time picker.
-
weekFor accepting a specific week; likely shows a calendar picker.
-
monthFor accepting a specific month; likely shows a calendar picker.
-
colorFor picking a specific colour; shows a colour palette.
Outputs as a hex colour.
-
rangeFor selecting from a range of numbers.
-
fileFor choosing a file to upload.
Use
accept="…"to limit filetypes.<input type="file" accept=".jpg,.png,.gif" id="photo">
-
searchSpecifies the input as a search field.
-
checkboxTurns the input into a check toggle.
-
radioTurns the input into a radio button.
All radio buttons in the group should have the same
name="…"
-
hiddenMakes the input field invisible.
Input attributes
Some of these attributes also apply to select and textarea.
-
requiredDefine the input as being compulsory.
<input id="dino" required>
-
checkedWhether the
radioorcheckboxare selected.<input type="checkbox" id="dino" checked>
-
value="…"Puts a default value into the field.
<input type="range" value="4" id="dino">
-
placeholder="…"Adds a temporary, helpful hint into the field.
<input type="email" placeholder="dino@extinct.com" id="email">Do not use this as a replacement for
<label>
-
autocomplete="off"Disable auto completion in the field.
-
autocapitalize="none"Disable auto capitalization in the field.
Non-standard: only works in iOS.
-
inputmode="…"Hint the browser to display a specific keyboard.
verbatim,numeric, etc.
-
list="…"The
idof an associated<datalist>
-
maxlength="…"For setting a maximum number of characters.
<input type="email" maxlength="256" id="email">
-
minlength="…"For setting a minimum number of characters.
<input type="text" minlength="6" id="postal-code">
-
min="…"Sets a minimum numerical value on
range&number.<input type="number" min="5" id="dino">
-
max="…"Sets a maximum numerical value on
range&number.<input type="range" max="100" id="dino">
-
step="…"Controls how the number will increment in
range&number.<input type="number" step="0.1" id="dino">
-
pattern="…"A JavaScript regular expression to control what is valid input.
<input type="text" pattern="[A-Za-z][0-9][A-Za-z] ?[0-9][A-Za-z][0-9]" id="postal-code">
-
multipleFor allowing multiple entries in the field.
Works for:
<select>,email,file
-
spellcheckTrigger the browser to perform spell checking in the field.
-
readonlyStops the user from modifying the value of the field.
-
disabledStops any interaction on the field.
-
autofocusWhen the page loads, this field will be focused.
Be really careful, only use this when the whole purpose of the page is to fill the form.
CSS selectors
-
:focusStyle an element when its keyboard focused.
-
:optionalStyle an element when it doesn’t have the
requiredattribute.
-
:requiredStyle an element when it does have the
requiredattribute.
-
:validStyle an element when its contents are a acceptable (email, url, pattern, etc.)
-
:invalidStyle an element when its contents are not acceptable.
-
:checkedStyle a checkbox or radio button when it is selected.
-
:disabledStyle an input when it has the
disabledattribute.
-
:enabledStyle an element when it doesn’t have the
disabledattribute.
-
:in-rangeStyle a
numberorrangewhen the selected value is within theminandmax.
-
:out-of-rangeStyle a
numberorrangewhen the selected value is outside theminandmax.
-
:indeterminateStyle a checkbox when its set to an undetermined state by JavaScript.
Examples
-
Simple form
<form method="post" action="//formspree.io/dino@extinct.com"> <label for="email">Email address</label> <input type="email" id="email" required> <button type="submit">Sign up!</button> </form>
-
Radio button group
<fieldset> <legend>Favourite dinosaur</legend> <input type="radio" id="trex" name="dinos"> <label for="trex">T. rex</label> <input type="radio" id="tri" name="dinos"> <label for="tri">Triceratops</label> <input type="radio" id="stego" name="dinos"> <label for="stego">Stegosaurus</label> </fieldset>
-
Autocomplete field
<label for="province">Province</label> <input id="province" list="province-list" required> <datalist id="province-list"> <option value="Alberta"> <option value="Ontario"> <option value="Québec"> <option value="Nova Scotia"> </datalist>