HTML Cheatsheet

Forms

Use this HTML reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

<form> Element

<form action="/submit" method="post">
  <!-- form controls -->
  <button type="submit">Submit</button>
</form>

<form> Attributes

AttributeValuesDescription
actionURLWhere to send the form data
methodget, post, dialogHTTP method
enctypeSee belowEncoding type (POST only)
novalidatebooleanSkip browser validation
autocompleteon, offEnable/disable autocomplete
target_self, _blank, _parent, _topWhere to display response
namestringForm name for document.forms
accept-charsetcharsetCharacter encoding
relnoopener, noreferrerFor target="_blank"

enctype values

ValueWhen to use
application/x-www-form-urlencodedDefault — key=value pairs, URL-encoded
multipart/form-dataRequired for file uploads (<input type="file">)
text/plainPlain text, rarely used
<!-- File upload form -->
<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="avatar">
  <button type="submit">Upload</button>
</form>

<input> Types

TypeRenders asNotes
textSingle-line text fieldDefault type
emailEmail fieldValidates email format
passwordMasked text field
numberNumber spinnermin, max, step
rangeSlidermin, max, step, value
telTelephone numberNo format validation
urlURL fieldValidates URL format
searchSearch fieldMay show clear button
dateDate pickerYYYY-MM-DD value
timeTime pickerHH:MM value
datetime-localDate + time pickerNo timezone
monthMonth pickerYYYY-MM value
weekWeek pickerYYYY-Www value
colorColor picker#rrggbb value
checkboxCheckboxchecked boolean
radioRadio buttonGroup by same name
fileFile pickeraccept, multiple
hiddenHidden fieldNot visible
submitSubmit buttonSubmits the form
resetReset buttonResets all fields
buttonGeneric buttonNo default behavior
imageImage submit buttonsrc, alt, width, height
<!-- Text inputs -->
<input type="text" name="username" placeholder="Username">
<input type="email" name="email" placeholder="you@example.com">
<input type="password" name="password" minlength="8">
<input type="tel" name="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
<input type="url" name="website" placeholder="https://example.com">
<input type="search" name="q" placeholder="Search...">

<!-- Number inputs -->
<input type="number" name="qty" min="1" max="99" step="1" value="1">
<input type="range" name="volume" min="0" max="100" step="5" value="50">

<!-- Date/time inputs -->
<input type="date" name="birthday" min="1900-01-01" max="2025-12-31">
<input type="time" name="meeting-time" min="09:00" max="17:00" step="900">
<input type="datetime-local" name="appointment">
<input type="month" name="billing-month">
<input type="week" name="sprint">

<!-- Other inputs -->
<input type="color" name="theme-color" value="#d4af37">
<input type="hidden" name="csrf" value="abc123">
<input type="file" name="doc" accept=".pdf,.doc,.docx">
<input type="file" name="photos" accept="image/*" multiple>

<input> Attributes

AttributeApplies toDescription
nameallKey in form submission
idallPairs with <label for="">
valueallInitial / submitted value
placeholdertext-likeHint text (not a label)
requiredallField must have a value
disabledallField cannot be interacted with
readonlytext-likeVisible but not editable
autofocusallFocus on page load
autocompletetext-likeBrowser autocomplete hint
spellchecktext-likeEnable/disable spellcheck
patterntext-likeRegex validation
minlengthtext-likeMinimum character count
maxlengthtext-likeMaximum character count
minnumeric/dateMinimum value
maxnumeric/dateMaximum value
stepnumeric/rangeLegal value intervals
multipleemail, fileAllow multiple values
acceptfileAllowed MIME types / extensions
capturefileuser or environment camera
checkedcheckbox, radioInitially checked
indeterminatecheckboxIndeterminate state (JS only)
sizetext-likeVisual width in characters
listtext-likePoints to <datalist id>
formallAssociate with a <form id> outside ancestor
formactionsubmit, imageOverride form action
formmethodsubmit, imageOverride form method
formenctypesubmit, imageOverride form enctype
formnovalidatesubmit, imageOverride form validation
formtargetsubmit, imageOverride form target
inputmodetext-likeVirtual keyboard hint

inputmode values

ValueShows keyboard for
textDefault text keyboard
numericNumbers only
decimalDecimal numbers
telPhone number
emailEmail (@ key prominent)
urlURL (.com key)
searchSearch (go key)
noneNo keyboard (custom widget)

autocomplete token values

<input type="text" autocomplete="name">
<input type="email" autocomplete="email">
<input type="tel" autocomplete="tel">
<input type="text" autocomplete="street-address">
<input type="text" autocomplete="postal-code">
<input type="text" autocomplete="country-name">
<input type="password" autocomplete="current-password">
<input type="password" autocomplete="new-password">
<input type="text" autocomplete="one-time-code">
<input type="text" autocomplete="username">
<input type="text" autocomplete="given-name">
<input type="text" autocomplete="family-name">
<input type="number" autocomplete="cc-number">     <!-- credit card -->

<label> Element

Always associate a label with its control. Clicking the label focuses the input.

<!-- Using for/id (can be far apart in DOM) -->
<label for="email">Email address</label>
<input type="email" id="email" name="email">

<!-- Wrapping (implicit association) -->
<label>
  Email address
  <input type="email" name="email">
</label>

<!-- Visually hidden label (still accessible) -->
<label for="search" class="sr-only">Search</label>
<input type="search" id="search" name="q" placeholder="Search...">

<textarea> Element

Multi-line text input.

<label for="bio">Bio</label>
<textarea id="bio" name="bio" rows="5" cols="40" placeholder="Tell us about yourself...">
  Existing content here
</textarea>

<textarea> Attributes

AttributeDescription
nameField name
rowsVisible rows
colsVisible columns
placeholderHint text
maxlengthMax characters
minlengthMin characters
requiredRequired field
readonlyNot editable
disabledDisabled
wrapsoft (default) / hard (adds newlines)
autocompleteAutocomplete hint
spellcheckSpellcheck
formAssociate with external form

<select> and <option>

<label for="country">Country</label>
<select id="country" name="country">
  <option value="">-- Select a country --</option>
  <option value="us">United States</option>
  <option value="gb">United Kingdom</option>
  <option value="ca" selected>Canada</option>
  <option value="au" disabled>Australia (unavailable)</option>
</select>

<optgroup> — grouped options

<select name="car">
  <optgroup label="Swedish Cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>
</select>

Multi-select

<select name="skills" multiple size="5">
  <option value="html">HTML</option>
  <option value="css">CSS</option>
  <option value="js" selected>JavaScript</option>
  <option value="ts">TypeScript</option>
  <option value="py" selected>Python</option>
</select>

<select> Attributes

AttributeDescription
nameField name
multipleAllow multiple selections
sizeVisible option count
requiredMust select a value
disabledDisabled
autofocusFocus on page load
formAssociate with external form

<datalist> — Input Suggestions

Provides an autocomplete suggestion list for any text-like input.

<label for="browser">Browser</label>
<input type="text" id="browser" name="browser" list="browsers">
<datalist id="browsers">
  <option value="Chrome">
  <option value="Firefox">
  <option value="Safari">
  <option value="Edge">
  <option value="Opera">
</datalist>

Checkbox and Radio

<!-- Checkbox group -->
<fieldset>
  <legend>Interests</legend>
  <label><input type="checkbox" name="interests" value="code"> Coding</label>
  <label><input type="checkbox" name="interests" value="design"> Design</label>
  <label><input type="checkbox" name="interests" value="music"> Music</label>
</fieldset>

<!-- Radio group — same name, only one selectable -->
<fieldset>
  <legend>Preferred contact</legend>
  <label><input type="radio" name="contact" value="email" checked> Email</label>
  <label><input type="radio" name="contact" value="phone"> Phone</label>
  <label><input type="radio" name="contact" value="mail"> Mail</label>
</fieldset>

<fieldset> and <legend>

Groups related controls. <legend> provides an accessible label for the group.

<fieldset>
  <legend>Billing Address</legend>
  <label for="street">Street</label>
  <input type="text" id="street" name="billing-street">

  <label for="city">City</label>
  <input type="text" id="city" name="billing-city">

  <label for="zip">ZIP Code</label>
  <input type="text" id="zip" name="billing-zip" pattern="[0-9]{5}">
</fieldset>

Disable an entire group:

<fieldset disabled>
  <legend>Payment (unavailable)</legend>
  <input type="text" name="card">
</fieldset>

<button> Element

<!-- Submit form (default type) -->
<button type="submit">Submit</button>

<!-- Reset form -->
<button type="reset">Reset</button>

<!-- Generic button (no form action) -->
<button type="button" onclick="doSomething()">Click me</button>

<!-- Disabled -->
<button type="submit" disabled>Submit</button>

<!-- With icon -->
<button type="button" aria-label="Close dialog">
  <svg aria-hidden="true" focusable="false">...</svg>
</button>

<!-- Override form attributes -->
<button type="submit" formaction="/save-draft" formmethod="post">Save Draft</button>
<button type="submit" formaction="/publish" formnovalidate>Publish Anyway</button>

<button> vs <input type="submit">

Prefer <button> — it can contain HTML (icons, text, rich content). <input type="submit"> only takes a plain value attribute.

<output> Element

Displays the result of a calculation or user action.

<form oninput="result.value = parseInt(a.value) + parseInt(b.value)">
  <input type="number" id="a" name="a" value="0">
  +
  <input type="number" id="b" name="b" value="0">
  =
  <output name="result" for="a b">0</output>
</form>

<progress> and <meter>

<!-- Indeterminate progress (no value) -->
<progress></progress>

<!-- Determinate progress -->
<progress value="75" max="100">75%</progress>

<!-- Meter — known range with semantic zones -->
<meter value="0.6">60%</meter>
<meter value="65" min="0" max="100" low="33" high="66" optimum="80">65</meter>
<meter value="3.5" min="1" max="5" low="2" high="4" optimum="5">3.5 / 5</meter>

Form Validation Attributes

<input type="email" required>
<input type="text" minlength="3" maxlength="50" required>
<input type="number" min="1" max="100" required>
<input type="text" pattern="[A-Za-z]{3}" title="Three letters only">

<!-- Disable all native validation -->
<form novalidate>
  <input type="email" name="email">
  <button type="submit">Submit</button>
</form>

Validation CSS pseudo-classes

<style>
  input:valid   { border-color: green; }
  input:invalid { border-color: red; }
  input:required { outline: 1px solid orange; }
  input:optional { /* not required */ }
  input:in-range  { /* within min/max */ }
  input:out-of-range { color: red; }
  input:placeholder-shown { /* placeholder visible */ }
  input:focus-within { /* descendant focused */ }
</style>

Associating Controls with a Form by ID

A control can live outside its <form> using the form attribute.

<form id="settings-form" action="/settings" method="post">
  <input type="text" name="username">
</form>

<!-- Elsewhere in the DOM -->
<button type="submit" form="settings-form">Save Settings</button>
<input type="hidden" name="source" value="sidebar" form="settings-form">

Complete Login Form Example

<form action="/login" method="post" autocomplete="on" novalidate>
  <fieldset>
    <legend>Sign In</legend>

    <div>
      <label for="login-email">Email</label>
      <input
        type="email"
        id="login-email"
        name="email"
        autocomplete="email"
        required
        placeholder="you@example.com"
        inputmode="email"
      >
    </div>

    <div>
      <label for="login-password">Password</label>
      <input
        type="password"
        id="login-password"
        name="password"
        autocomplete="current-password"
        required
        minlength="8"
      >
    </div>

    <label>
      <input type="checkbox" name="remember" value="1">
      Remember me
    </label>

    <input type="hidden" name="csrf_token" value="abc123xyz">

    <button type="submit">Sign In</button>
  </fieldset>
</form>