Build forms with the Chassis form components — inputs, checks, ranges, input groups, floating labels, layout patterns, and validation.
Overview
The form components build on the form styles of Reboot with classes that give inputs, selects, checks, and ranges the same look in every browser. The pages of this section cover one component each; this page covers what they share.
Every form component reads its colors, sizes, and spacing from custom properties, so you can theme all of them at once or override a single control.
Use the type attribute that matches the data (email, number, tel, and so on): the browser then validates the value and shows the fitting keyboard and controls.
A form with a labeled input, a help text, a checkbox, and a submit button looks like this:
<form>
<div class="mb-md">
<label for="exampleInputEmail1" class="form-label">Email address</label>
<input type="email" class="form-input" id="exampleInputEmail1" aria-describedby="emailHelp">
<div id="emailHelp" class="form-help">We'll never share your email with anyone else.</div>
</div>
<div class="mb-md">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" autocomplete="current-password" class="form-input" id="exampleInputPassword1">
</div>
<div class="mb-md form-check">
<input type="checkbox" class="check-input" id="exampleCheck1">
<label for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="button primary">Submit</button>
</form>Disabled forms
Add the disabled attribute to an input to make it unavailable. The input takes the disabled form colors, and the browser stops interaction with it.
<input class="form-input" id="disabledInput" type="text" placeholder="Disabled input here..." disabled>To disable several controls at once, add disabled to a parent <fieldset>. The browser disables every native form element inside it (<input>, <select>, <textarea>, and <button>), and they take their disabled styles.
A link styled as a button, such as <a class="button">, is not a form element, so a disabled fieldset does not disable it. Chassis gives it the disabled button styles and pointer-events: none, which stops mouse interaction only. Add tabindex="-1" to keep it out of the tab order and aria-disabled="true" to tell assistive technologies that it is disabled.
<form>
<fieldset disabled>
<legend>Disabled fieldset example</legend>
<div class="mb-md">
<label for="disabledTextInput" class="form-label">Disabled input</label>
<input type="text" id="disabledTextInput" class="form-input" placeholder="Disabled input">
</div>
<div class="mb-md">
<label for="disabledSelect" class="form-label">Disabled select menu</label>
<select id="disabledSelect" class="form-input">
<option>Disabled select</option>
</select>
</div>
<div class="mb-md">
<div class="form-check">
<input class="check-input" type="checkbox" id="disabledFieldsetCheck">
<label for="disabledFieldsetCheck">
Can't check this
</label>
</div>
</div>
<button type="submit" class="button primary">Submit</button>
</fieldset>
</form>Dark mode
Form components take their colors from custom properties that follow the color mode. Add data-cx-theme="dark" to a container, or to a single form element, to render it with the dark palette.
Inheritance and override
Form elements inherit the theme of their parent, and a data-cx-theme attribute on a single control overrides it:
data-cx-theme="dark"on a container puts every form control inside it in dark modedata-cx-theme="light"on a control inside a dark container sets that control apart
The example has a light and a dark form, each with controls of the opposite theme:
<div class="grid grid-cols-1 md:grid-cols-2 gap-xl">
<form class="context p-md" data-cx-theme="light">
<div class="mb-md">
<label for="exampleSelectDark" class="form-label">Example Select</label>
<select class="form-input" id="exampleSelectDark" data-cx-theme="dark">
<option selected>Choose an option</option>
<option>Option 1</option>
<option>Option 2</option>
</select>
</div>
<div class="mb-md">
<label for="exampleInputDark" class="form-label">Example Input</label>
<input type="text" class="form-input" id="exampleInputDark" data-cx-theme="dark" placeholder="Dark themed input">
</div>
<div class="mb-md form-check">
<input type="checkbox" class="check-input" id="exampleCheckDark" data-cx-theme="dark">
<label for="exampleCheckDark">Example checkbox</label>
</div>
<button type="submit" class="button primary">Submit</button>
</form>
<form class="context p-md" data-cx-theme="dark">
<div class="mb-md">
<label for="exampleSelectLight" class="form-label">Example Select</label>
<select class="form-input" id="exampleSelectLight" data-cx-theme="light">
<option selected>Choose an option</option>
<option>Option 1</option>
<option>Option 2</option>
</select>
</div>
<div class="mb-md">
<label for="exampleInputLight" class="form-label">Example Input</label>
<input type="text" class="form-input" id="exampleInputLight" data-cx-theme="light" placeholder="Light themed input">
</div>
<div class="mb-md form-check">
<input type="checkbox" class="check-input" id="exampleCheckLight" data-cx-theme="light">
<label for="exampleCheckLight">Example checkbox</label>
</div>
<button type="submit" class="button primary">Submit</button>
</form>
</div>Where a whole form shares a theme, set data-cx-theme once on its container instead of on each control: the markup stays shorter, and a change of theme is made in one place.
Accessibility
The form components style native controls, so roles, states, and keyboard interaction come from the browser. Names, groups, and error messages depend on your markup:
Accessible names
Every form control needs an accessible name that says what it is for. Without one, users of screen readers cannot tell which information a field asks for.
Use explicit labels:
- Connect inputs with labels using the
forattribute that matches the input'sid - Place checkboxes and radios inside label elements for automatic association
When a visible label is not possible:
- Hide a label visually with the
.visually-hiddenclass - Reference existing content with
aria-labelledby - Use
aria-labelfor a short, explicit name - Add a
titleattribute only as a last resort
Never rely on the placeholder attribute alone for the name of a control. Placeholders:
- Disappear when the field contains content
- Often have low color contrast
- Cannot be accessed by all assistive technologies consistently
State communication
A form control has to convey its state (required, invalid, disabled) in the markup as well as visually:
- Use the
requiredattribute for mandatory fields - Set
aria-invalid="true"on a field with a validation error - Associate each error message with its field through
aria-describedby
The page of each form component has an accessibility section with the details for that component.
CSS
Form components can be customized through Sass variables at compile time.
Form elements share one set of custom properties, named by state: --form-idle-*, --form-focus-*, --form-disabled-*. A component assigns the property of the current state to a local property such as --bg-color and uses that in its declarations. The idle value reads an override property first, so you can restyle one control without touching the shared set:
.form-input {
--bg-color: var(--input-bg-color, var(--form-idle-bg-regular));
background-color: var(--bg-color);
}
.form-input:disabled {
--bg-color: var(--form-disabled-bg-regular);
}Custom properties
Form components expose CSS custom properties to control their appearance at runtime.
The local color properties of a form input, each with its override property:
--fg-color: var(--input-fg-color, var(--form-idle-fg-active));
--bg-color: var(--input-bg-color, var(--form-idle-bg-regular));
--border-color: var(--input-border-color, var(--form-idle-border-color));
--icon-color: var(--input-icon-color, var(--form-idle-fg-active));The shared color properties of all form elements, one group per state:
// Idle state colors
--form-idle-bg-regular: #{$form-idle-bg-regular};
--form-idle-fg-active: #{$form-idle-fg-active};
--form-idle-fg-inactive: #{$form-idle-fg-inactive};
--form-idle-border-color: #{$form-idle-border-color};
--form-idle-help-color: #{$form-idle-help-color};
--form-idle-bg-floating: #{$form-idle-bg-floating};
// Focus state colors
--form-focus-bg-regular: #{$form-focus-bg-regular};
--form-focus-fg-active: #{$form-focus-fg-active};
--form-focus-fg-inactive: #{$form-focus-fg-inactive};
--form-focus-border-color: #{$form-focus-border-color};
--form-focus-help-color: #{$form-focus-help-color};
--form-focus-bg-floating: #{$form-focus-bg-floating};
// Disabled state colors
--form-disabled-bg-regular: #{$form-disabled-bg-regular};
--form-disabled-fg-active: #{$form-disabled-fg-active};
--form-disabled-fg-inactive: #{$form-disabled-fg-inactive};
--form-disabled-border-color: #{$form-disabled-border-color};
--form-disabled-help-color: #{$form-disabled-help-color};
--form-disabled-bg-floating: #{$form-disabled-bg-floating};
// Invalid state colors
--form-invalid-bg-regular: #{$form-invalid-bg-regular};
--form-invalid-fg-active: #{$form-invalid-fg-active};
--form-invalid-fg-inactive: #{$form-invalid-fg-inactive};
--form-invalid-border-color: #{$form-invalid-border-color};
--form-invalid-help-color: #{$form-invalid-help-color};
--form-invalid-bg-floating: #{$form-invalid-bg-floating};
// Valid state colors
--form-valid-bg-regular: #{$form-valid-bg-regular};
--form-valid-fg-active: #{$form-valid-fg-active};
--form-valid-fg-inactive: #{$form-valid-fg-inactive};
--form-valid-border-color: #{$form-valid-border-color};
--form-valid-help-color: #{$form-valid-help-color};
--form-valid-bg-floating: #{$form-valid-bg-floating};
// Default focus ring color that changes based on validation states
--form-focus-ring-color: #{$form-input-focus-ring-color};
--form-valid-focus-ring-color: #{$form-valid-focus-ring-color};
--form-invalid-focus-ring-color: #{$form-invalid-focus-ring-color};The sizing and spacing properties of form controls:
@include map-font($form-md-text-font, input);
--padding-x: var(--input-padding-x, #{$form-md-padding-x});
--padding-y: var(--input-padding-y, #{$form-md-padding-y});
--gap: var(--input-gap, #{$form-md-gap});
--min-height: var(--input-min-height, #{$form-input-min-height});
--border-width: var(--input-border-width, #{$form-regular-border-width});
--border-radius: var(--input-border-radius, #{$form-md-border-radius});
--caret-size: var(--input-caret-size, #{$form-md-caret-size});
--icon-size: var(--input-icon-size, #{$form-md-icon-size});Sass variables
Form components use Sass variables in scss/config/_defaults.scss to define their defaults.
$form-input-plaintext-color: var(--fg-main);
$form-input-min-height: calc(var(--line-height) + var(--padding-y) * 2);
$form-input-box-shadow: none; // var(--box-shadow-inset);
$form-input-focus-ring-color: $focus-ring-color;
$form-input-focus-ring-offset: 0px;
$form-input-transition: border-color .15s ease-in-out, box-shadow .15s ease-in-out;Design tokens
Form components consume design tokens from Chassis Tokens with the $cx- prefix. — See the design tokens page.
The typography tokens of form elements, per size:
$form-md-label-font: $cx-font-form-input-medium-label;
$form-lg-label-font: $cx-font-form-input-large-label;
$form-sm-label-font: $cx-font-form-input-small-label;
$form-md-text-font: $cx-font-form-input-medium-text;
$form-lg-text-font: $cx-font-form-input-large-text;
$form-sm-text-font: $cx-font-form-input-small-text;
$form-floating-text-font: $cx-font-form-input-floating-text;
$form-floating-label-font: $cx-font-form-input-floating-label;The spacing tokens, for the padding and the gaps of form controls:
$form-md-padding-y: $cx-space-form-input-medium-padding-y;
$form-md-padding-x: $cx-space-form-input-medium-padding-x;
$form-md-gap: $cx-space-form-input-medium-gap;
$form-lg-padding-y: $cx-space-form-input-large-padding-y;
$form-lg-padding-x: $cx-space-form-input-large-padding-x;
$form-lg-gap: $cx-space-form-input-large-gap;
$form-sm-padding-y: $cx-space-form-input-small-padding-y;
$form-sm-padding-x: $cx-space-form-input-small-padding-x;
$form-sm-gap: $cx-space-form-input-small-gap;The border tokens, for the width and the radius of form controls:
$form-regular-border-width: $cx-border-width-form-input-regular;
$form-md-border-radius: $cx-border-radius-form-input-medium;
$form-lg-border-radius: $cx-border-radius-form-input-large;
$form-sm-border-radius: $cx-border-radius-form-input-small;
$form-floating-border-width: $cx-border-width-form-input-floating;
$form-floating-border-radius-t: $cx-border-radius-form-input-floating-blunt;
$form-floating-border-radius-b: $cx-border-radius-form-input-floating-sharp;The color tokens, for the idle, focused, and disabled states:
$form-idle-bg-regular: $cx-color-form-input-idle-bg-regular;
$form-idle-bg-floating: $cx-color-form-input-idle-bg-floating;
$form-idle-fg-active: $cx-color-form-input-idle-fg-active;
$form-idle-fg-inactive: $cx-color-form-input-idle-fg-inactive;
$form-idle-border-color: $cx-color-form-input-idle-border;
$form-idle-help-color: $cx-color-form-input-idle-help;
$form-idle-caret-color: $cx-color-form-input-idle-caret;
$form-disabled-bg-regular: $cx-color-form-input-disabled-bg-regular;
$form-disabled-bg-floating: $cx-color-form-input-disabled-bg-floating;
$form-disabled-fg-active: $cx-color-form-input-disabled-fg-active;
$form-disabled-fg-inactive: $cx-color-form-input-disabled-fg-inactive;
$form-disabled-border-color: $cx-color-form-input-disabled-border;
$form-disabled-help-color: $cx-color-form-input-disabled-help;
$form-disabled-caret-color: $cx-color-form-input-disabled-caret;
$form-focus-bg-regular: $cx-color-form-input-focus-bg-regular;
$form-focus-bg-floating: $cx-color-form-input-focus-bg-floating;
$form-focus-fg-active: $cx-color-form-input-focus-fg-active;
$form-focus-fg-inactive: $cx-color-form-input-focus-fg-inactive;
$form-focus-border-color: $cx-color-form-input-focus-border;
$form-focus-help-color: $cx-color-form-input-focus-help;
// $form-focus-caret-color: $cx-color-form-input-focus-caret;
$form-invalid-bg-regular: $cx-color-form-input-error-bg-regular;
$form-invalid-bg-floating: $cx-color-form-input-error-bg-floating;
$form-invalid-fg-active: $cx-color-form-input-error-fg-active;
$form-invalid-fg-inactive: $cx-color-form-input-error-fg-inactive;
$form-invalid-border-color: $cx-color-form-input-error-border;
$form-invalid-help-color: $cx-color-form-input-error-help;
// $form-invalid-caret-color: $cx-color-form-input-error-caret;
$form-valid-bg-regular: $cx-color-form-input-success-bg-regular;
$form-valid-bg-floating: $cx-color-form-input-success-bg-floating;
$form-valid-fg-active: $cx-color-form-input-success-fg-active;
$form-valid-fg-inactive: $cx-color-form-input-success-fg-inactive;
$form-valid-border-color: $cx-color-form-input-success-border;
$form-valid-help-color: $cx-color-form-input-success-help;
// $form-valid-caret-color: $cx-color-form-input-success-caret;