form
Container for interactive form controls that collects and submits user data. The form element provides structure, validation, and submission handling.
When to Use
- Collecting user input (contact forms, login, registration)
- Search functionality
- Any interaction that submits data to a server
- Client-side form validation with HTML5 attributes
Basic Usage
A minimal form with essential attributes.
Code
Form Attributes
| Attribute | Purpose | Example |
|---|---|---|
action |
URL to submit data to | action="/api/contact" |
method |
HTTP method (GET or POST) | method="POST" |
enctype |
Encoding type for POST data | enctype="multipart/form-data" |
novalidate |
Disable browser validation | novalidate |
autocomplete |
Enable/disable autofill | autocomplete="on" |
name |
Form name for scripting | name="contact-form" |
Layout Variants
.stacked (Vertical Layout)
Fields arranged vertically with consistent spacing. Best for general-purpose forms.
.inline (Horizontal Layout)
Fields arranged horizontally. Best for search bars and compact forms.
.grid (Two-Column Layout)
Labels on the left, inputs on the right. Best for data entry forms.
Code
Form Groups
The .group class wraps a label-input pair with appropriate spacing.
.group (Vertical)
.group.horizontal
For checkbox and toggle layouts where label and input are side by side.
Code
Form Actions
The .actions class provides a container for submit/cancel buttons.
Code
Help and Error Text
Provide additional context with help and error text classes.
Code
File Upload Forms
For file uploads, use enctype="multipart/form-data".
Code
Complete Contact Form
A full example combining multiple form elements.
Conditional Fields (data-show-when)
Use data-show-when or data-hide-when on any element inside a form to conditionally display sections based on another field’s value. Hidden elements get hidden and inert attributes, preventing them from blocking validation.
Show When
Show a section when a field has a specific value.
Hide When
Hide a section when a condition is met — the inverse of data-show-when.
| Attribute | Format | Description |
|---|---|---|
data-show-when |
name=value |
Show when the named field equals the value. |
data-hide-when |
name=value |
Hide when the named field equals the value. |
Works with <select>, <input type="radio">, <input type="checkbox">, and text inputs. The name refers to the name attribute of the controlling field within the same <form>.
Form Autosave (data-autosave)
Add data-autosave="key" to a <form> to automatically save its data to localStorage as the user types. On page reload, the draft is restored and a “Draft restored” toast appears. Drafts are cleared on submit or reset and expire after 24 hours.
| Attribute | Description |
|---|---|
data-autosave |
Storage key for this form’s draft. Must be unique per form. |
Behavior
- Save: Debounced (500ms) on every
inputandchangeevent - Restore: On page load if a non-expired draft exists
- Clear: On form
submitorreset - Skip: Password and file inputs are never saved
- Expire: Drafts older than 24 hours are discarded
Accessibility Notes
- Label all inputs: Every form control needs an associated label
- Use fieldsets: Group related fields with fieldset and legend
- Error identification: Use
aria-invalidandaria-describedbyfor errors - Autocomplete: Add appropriate
autocompleteattributes for autofill - Focus management: Use
autofocuson the first field (one per page) - Submit button: Always include a submit button, not just Enter key handling