- The {'
component renders a form control with
- Bootstrap styling. The {'
component wraps a
- form control with proper spacing, along with support for a label, help
- text, and validation state. To ensure accessibility, set{' '}
- controlId
on {'
, and use{' '}
- {'
for the label.
-
- The {'
component directly renders the{' '}
- {''}
or other specified component. If you need to
- access the value of an uncontrolled {'
,
- attach a ref
to it as you would with an uncontrolled input,
- then call ReactDOM.findDOMNode(ref)
to get the DOM node.
- You can then interact with that node as you would with any other
- uncontrolled input.
-
- If your application contains a large number of form groups, we recommend - building a higher-level component encapsulating a complete field group - that renders the label, the control, and any other necessary components. - We don't provide this out-of-the-box, because the composition of those - field groups is too specific to an individual application to admit a - good one-size-fits-all solution. -
-
- For textual form controls—like input
s and{' '}
- textarea
s—use the FormControl
component.
- FormControl adds some additional styles for general appearance, focus
- state, sizing, and more.
-
- Use size
on {'
and{' '}
- {'
to change the size of inputs and labels
- respectively.
-
- Add the readOnly
prop on an input to prevent modification
- of the input's value. Read-only inputs appear lighter (just like
- disabled inputs), but retain the standard cursor.
-
- If you want to have readonly elements in your form styled as plain text,
- use the plaintext
prop on FormControls to remove the
- default form field styling and preserve the correct margin and padding.
-
- For the non-textual checkbox and radio controls, FormCheck
{' '}
- provides a single component for both types that adds some additional
- styling and improved layout.
-
- By default, any number of checkboxes and radios that are immediate - sibling will be vertically stacked and appropriately spaced with - FormCheck. -
-
- Group checkboxes or radios on the same horizontal row by adding the{' '}
- inline
prop.
-
- When you render a FormCheck without a label (no children
)
- some additional styling is applied to keep the inputs from collapsing.{' '}
-
- Remember to add an aria-label
when omitting labels!
-
-
- When you need tighter control, or want to customize how the{' '}
- FormCheck
component renders, it may better to use it's
- constituent parts directly.
-
- By provided children
to the FormCheck
you can
- forgo the default rendering and handle it yourself. (You can still
- provide an id
to the FormCheck
or{' '}
- FormGroup
and have it propagate to the label and input).
-
{''}
controls with
- {''}
. The track (the background) and thumb (the
- value) are both styled to appear the same across browsers. As only Firefox
- supports “filling” their track from the left or right of the thumb as a
- means to visually indicate progress, we do not currently support it.
- - You may also choose from small and large custom selects to match our - similarly sized text inputs. -
-
- Wrap a {'
element in{' '}
- {'
to enable floating labels with
- Bootstrap’s textual form fields. A placeholder
is required
- on each {'
as our method of CSS-only
- floating labels uses the :placeholder-shown
pseudo-element.
-
- By default, {'
s will be the same height as{' '}
- {''}
s. To set a custom height on your{' '}
- {'
, do not use the rows
attribute.
- Instead, set an explicit height
(either inline or via
- custom CSS).
-
- Other than {'
, floating labels are only
- available on {'
s. They work in the same way,
- but unlike {''}
s, they’ll always show the{' '}
- {'
in its floated state.
-
- When working with the Bootstrap grid system, be sure to place form - elements within column classes. -
-
- If you need greater control over the rendering, use the{' '}
- {'
component to wrap your input and label.
- Also note that the {'
must come first so we
- can utilize a sibling selector (e.g., ~).
-
- FormControl and FormCheck both apply display: block
with{' '}
- width: 100%
to controls, which means they stack vertically
- by default. Additional components and props can be used to vary this
- layout on a per-form basis.
-
- The FormGroup
component is the easiest way to add some
- structure to forms. It provides a flexible container for grouping of
- labels, controls, optional help text, and form validation messaging. By
- default it only applies margin-bottom. Use it with fieldset
- s, div
s, or nearly any other element.
-
- You also add the controlId
prop to accessibly wire the
- nested label and input together via the id
.
-
- More complex forms can be built using the grid components. Use these for - form layouts that require multiple columns, varied widths, and - additional alignment options. -
-More complex layouts can also be created with the grid system.
-
- You can size the {'
using the column prop as
- shown.
-
- As shown in the previous examples, our grid system allows you to place
- any number of {'
s within a {'
.
- They'll split the available width equally between them. You may also
- pick a subset of your columns to take up more or less space, while the
- remaining {'
s equally split the rest, with specific
- column classes like {'
.
-
- The example below uses a flexbox utility to vertically center the
- contents and changes {'
to{' '}
- {'
so that your columns only take up as
- much space as needed. Put another way, the column sizes itself based on
- on the contents.
-
- You can then remix that once again with size-specific column classes. -
-- And of course custom form controls are - supported. -
-
- Block-level help text in forms can be created using{' '}
- {'
. Inline help text can be flexibly
- implemented using any inline HTML element and utility classes like
- .text-muted
.
-
aria-describedby
attribute. This will
- ensure that assistive technologies—such as screen readers—will announce
- this help text when the user focuses or enters the control.
-
- Help text below inputs can be styled with {'
.
- This component includes display: block
and adds some top
- margin for easy spacing from the inputs above.
-
- Add the disabled
boolean attribute on an input to prevent
- user interactions and make it appear lighter.
-
- Add the disabled
attribute to a {'
{' '}
- to disable all the controls within.
-
{''}
, {'
and{' '}
- {'
elements) inside a{' '}
- {'
as disabled, preventing both
- keyboard and mouse interactions on them. However, if your form also
- includes {''}
elements, these will
- only be given a style of pointer-events: none
. As noted in
- the section about{' '}
-
- disabled state for buttons
- {' '}
- (and specifically in the sub-section for anchor elements), this CSS
- property is not yet standardized and isn’t fully supported in Internet
- Explorer 10, and won’t prevent keyboard users from being able to focus
- or activate these links. So to be safe, use custom JavaScript to disable
- such links.
- disabled
{' '}
- attribute on a {'
. Use custom JavaScript to
- disable the fieldset in these browsers.
- - Provide valuable, actionable feedback to your users with form validation - feedback. -
-
- For native HTML form validation–
-
- available in all our supported browsers
-
- , the :valid
and :invalid
pseudo selectors are
- used to apply validation styles as well as display feedback messages.
-
- Bootstrap scopes the :valid
and :invalid
{' '}
- styles to parent .was-validated
class, usually applied to
- the {'
(you can use the validated
prop
- as a shortcut). Otherwise, any required field without a value shows up
- as invalid on page load. This way, you may choose when to activate them
- (typically after form submission is attempted).
-
form
s. You can disable the default UI by adding the HTML{' '}
- noValidate
attribute to your {'
or{' '}
- {'
element.
-
- It's often beneficial (especially in React) to handle form validation
- via a library like Formik, or react-formal. In those cases,{' '}
- isValid
and isInvalid
props can be added to
- form controls to manually apply validation styles. Below is a quick
- example integrating with{' '}
- Formik.
-
- If your form layout allows it, you can use the tooltip
prop
- to display validation feedback in a styled tooltip. Be sure to have a
- parent with position: relative
on it for tooltip
- positioning. In the example below, our column classes have this already,
- but your project may require an alternative setup.
-
- To properly show rounded corners in an {'
{' '}
- with validation, the {'
requires the{' '}
- hasValidation
prop.
-
- For even more customization and cross browser consistency, use our - completely custom form elements to replace the browser defaults. They’re - built on top of semantic and accessible markup, so they’re solid - replacements for any default form control. -
-
- A switch has the markup of a custom checkbox but uses{' '}
- type="switch"
to render a toggle switch. Switches also
- support the same customizable children as {'
.
-
{''}
alias which
- encapsulates the above, in a very small component wrapper.
- + Create consistent cross-browser and cross-device checkboxes and radios with + our completely rewritten checks component. +
+ +For the non-textual checkbox and radio controls, `FormCheck` +provides a single component for both types that adds some additional +styling and improved layout. + +## Default (stacked) + +By default, any number of checkboxes and radios that are immediate +sibling will be vertically stacked and appropriately spaced with +FormCheck. + +{''}
alias which encapsulates
+ the above, in a very small component wrapper.
++ Create beautifully simple form labels that float over your input fields. +
+ +## Example + +Wrap a `