Forms
Examples and usage guidelines for form control styles, layout options, and custom components for creating a wide variety of forms.
Overview
Idea React's form components build on Bootstrap's form styles and provide a consistent API for rendering controls across browsers and devices.
For further customization, pass a CSS Module class through className. The CSS reference sections show custom property defaults from the published stylesheet; no preprocessor configuration is required.
Be sure to use an appropriate type attribute on all inputs (e.g., email for email address or number for numerical information) to take advantage of newer input controls like email verification, number selection, and more.
Here’s a quick example using the form components. Keep reading for component props, form layout, and more.
<Form>
<div className="mb-3">
<FormLabel htmlFor="exampleInputEmail1">
Email address
</FormLabel>
<FormControl
aria-describedby="emailHelp"
id="exampleInputEmail1"
type="email"
/>
<FormText
block
id="emailHelp"
>
We’ll never share your email with anyone else.
</FormText>
</div>
<div className="mb-3">
<FormLabel htmlFor="exampleInputPassword1">
Password
</FormLabel>
<FormControl
id="exampleInputPassword1"
type="password"
/>
</div>
<FormField className="mb-3">
<Checkbox id="exampleCheck1" />
<FormLabel
htmlFor="exampleCheck1"
plain
>
Check me out
</FormLabel>
</FormField>
<Button
theme="primary"
type="submit"
variant="solid"
>
Submit
</Button>
</Form>Disabled forms
Set the disabled prop on FormControl to prevent user interaction and give it a disabled appearance.
<FormControl id="disabledInput" type="text" placeholder="Disabled input here..." disabled />
Add the native disabled attribute to a fieldset to disable all nested controls. Browsers treat native inputs, selects, and buttons inside a disabled fieldset as disabled, preventing keyboard and mouse interaction.
This does not disable custom links styled as buttons. Use Button's disabled prop for button-like links; it removes navigation and updates keyboard focus and accessibility state.
<Form>
<fieldset disabled>
<FormLegend>
Disabled fieldset example
</FormLegend>
<div className="mb-3">
<FormLabel htmlFor="disabledTextInput">
Disabled input
</FormLabel>
<FormControl
id="disabledTextInput"
placeholder="Disabled input"
type="text"
/>
</div>
<div className="mb-3">
<FormLabel htmlFor="disabledSelect">
Disabled select menu
</FormLabel>
<FormSelect id="disabledSelect">
<option>
Disabled select
</option>
</FormSelect>
</div>
<FormField className="mb-3">
<Checkbox
disabled
id="disabledFieldsetCheck"
/>
<FormLabel
htmlFor="disabledFieldsetCheck"
plain
>
Can’t check this
</FormLabel>
</FormField>
<Button
theme="primary"
type="submit"
variant="solid"
>
Submit
</Button>
</fieldset>
</Form>Accessibility
Ensure that all form controls have an appropriate accessible name so assistive technologies can convey their purpose. Use FormLabel for form controls, or descriptive content for buttons.
When a visible FormLabel or button text is not appropriate, provide an accessible name by:
- Pointing to an existing element with
aria-labelledby - Providing a
titleattribute - Explicitly setting the accessible name on an element using
aria-label
Do not rely on placeholder as a control's accessible name; it is only a fallback in some assistive technology and disappears when the control has a value.
While aria-label can provide an accessible name, a visible label is generally best for accessibility and usability.
CSS
Variables
Some form variables are set at the root level to be re-used and extended by individual form components, but you’ll most often see variables set at the form control level. Root variables include --bs-btn-input-*, which are shared with buttons, and --bs-control-* variables.
.example {
--bs-control-checked-bg: var(--bs-primary-base);
--bs-control-checked-border-color: var(--bs-control-checked-bg);
--bs-control-active-bg: var(--bs-primary-base);
--bs-control-active-border-color: var(--bs-control-active-bg);
--bs-control-disabled-bg: var(--bs-bg-3);
--bs-control-disabled-opacity: 0.65;
--bs-control-transition-duration: 0.15s;
--bs-control-transition-timing: cubic-bezier(0.34, 1.56, 0.64, 1);
--bs-btn-input-fg: var(--bs-fg-body);
--bs-btn-input-bg: var(--bs-bg-body);
--bs-btn-input-gap: 0.375rem;
--bs-btn-input-min-height: 2.375rem;
--bs-btn-input-padding-y: 0.375rem;
--bs-btn-input-padding-x: 0.75rem;
--bs-btn-input-font-size: var(--bs-body-font-size);
--bs-btn-input-font-weight: var(--bs-font-weight-normal);
--bs-btn-input-line-height: var(--bs-body-line-height);
--bs-btn-input-border-radius: var(--bs-radius-5);
--bs-btn-input-xs-gap: var(--bs-spacer-1);
--bs-btn-input-xs-min-height: 1.5rem;
--bs-btn-input-xs-padding-y: 0.125rem;
--bs-btn-input-xs-padding-x: 0.5rem;
--bs-btn-input-xs-font-size: var(--bs-font-size-xs);
--bs-btn-input-xs-line-height: 1.125;
--bs-btn-input-xs-border-radius: var(--bs-radius-5);
--bs-btn-input-sm-gap: 0.375rem;
--bs-btn-input-sm-min-height: 2rem;
--bs-btn-input-sm-padding-y: 0.25rem;
--bs-btn-input-sm-padding-x: 0.625rem;
--bs-btn-input-sm-font-size: var(--bs-font-size-sm);
--bs-btn-input-sm-line-height: var(--bs-line-height-sm);
--bs-btn-input-sm-border-radius: var(--bs-radius-5);
--bs-btn-input-lg-gap: 0.375rem;
--bs-btn-input-lg-min-height: 2.75rem;
--bs-btn-input-lg-padding-y: 0.5rem;
--bs-btn-input-lg-padding-x: 1rem;
--bs-btn-input-lg-font-size: var(--bs-font-size-md);
--bs-btn-input-lg-line-height: var(--bs-line-height-md);
--bs-btn-input-lg-border-radius: var(--bs-radius-7);
}