OCUI Forms
General Rules
Forms are always rendered as .form-horizontal
. Field length is justified within the grid. Forms may contain <fieldset>
, <hr>
separators and intersecting <h2>
— <h4>
headlines, but these should be used sparingly, and only if necessary.
Each form always has a headline, a gridded set of fields and a .form-submit
below the form content, with at least one submit action.
Form Submit
Each form has at least one form-submit component located at its bottom end. A redundant form-submit may be placed on top of the form if the form is either very long or may be sumbitted without change. Use wisely!
Buttons are classed and sorted by importance / likeliness of usage. Each Form has one (!) primary action/submit, and may have several secondary actions, as well as a tertiary abort / cancel action. Order is right aligned, from primary (right) to tertiary (left)
Form Heading
Use The Grid, Luke
Make sure to use an appropriate amount of columns keeping minimum field lengths and label sizes in mind.
Don't Reduce Fields to Content Size
It is often requested to have field length match the content dimensions. This is not recommended with bootstrap forms, as they are designed to work as justified/block level objects in columns.
Form Heading
Form Heading
BS 3.3 Forms
Basic example
Individual form controls automatically receive some global styling. All textual <input>
, <textarea>
, and <select>
elements with .form-control
are set to width: 100%;
by default. Wrap labels and controls in .form-group
for optimum spacing.
Don't mix form groups with input groups
Do not mix form groups directly with input groups. Instead, nest the input group inside of the form group.
Inline form
Add .form-inline
to your form (which doesn't have to be a <form>
) for left-aligned and inline-block controls. This only applies to forms within viewports that are at least 768px wide.
May require custom widths
Inputs and selects have width: 100%;
applied by default in Bootstrap. Within inline forms, we reset that to width: auto;
so multiple controls can reside on the same line. Depending on your layout, additional custom widths may be required.
Always add labels
Screen readers will have trouble with your forms if you don't include a label for every input. For these inline forms, you can hide the labels using the .sr-only
class. There are further alternative methods of providing a label for assistive technologies, such as the aria-label
, aria-labelledby
or title
attribute. If none of these is present, screen readers may resort to using the placeholder
attribute, if present, but note that use of placeholder
as a replacement for other labelling methods is not advised.
Horizontal form
Use Bootstrap's predefined grid classes to align labels and groups of form controls in a horizontal layout by adding .form-horizontal
to the form (which doesn't have to be a <form>
). Doing so changes .form-group
s to behave as grid rows, so no need for .row
.
Supported controls
Examples of standard form controls supported in an example form layout.
Inputs
Most common form control, text-based input fields. Includes support for all HTML5 types: text
, password
, datetime
, datetime-local
, date
, month
, time
, week
, number
, email
, url
, search
, tel
, and color
.
Type declaration required
Inputs will only be fully styled if their type
is properly declared.
Input groups
To add integrated text or buttons before and/or after any text-based <input>
, check out the input group component.
Textarea
Form control which supports multiple lines of text. Change rows
attribute as necessary.
Checkboxes and radios
Checkboxes are for selecting one or several options in a list, while radios are for selecting one option from many.
Disabled checkboxes and radios are supported, but to provide a "not-allowed" cursor on hover of the parent <label>
, you'll need to add the .disabled
class to the parent .radio
, .radio-inline
, .checkbox
, or .checkbox-inline
.
Default (stacked)
Inline checkboxes and radios
Use the .checkbox-inline
or .radio-inline
classes on a series of checkboxes or radios for controls that appear on the same line.
Checkboxes and radios without label text
Should you have no text within the <label>
, the input is positioned as you'd expect. Currently only works on non-inline checkboxes and radios. Remember to still provide some form of label for assistive technologies (for instance, using aria-label
).
Selects
Note that many native select menus—namely in Safari and Chrome—have rounded corners that cannot be modified via border-radius
properties.
For <select>
controls with the multiple
attribute, multiple options are shown by default.
Static control
When you need to place plain text next to a form label within a form, use the .form-control-static
class on a <p>
.
Focus state
We remove the default outline
styles on some form controls and apply a box-shadow
in its place for :focus
.
Demo :focus
state
The above example input uses custom styles in our documentation to demonstrate the :focus
state on a .form-control
.
Disabled state
Add the disabled
boolean attribute on an input to prevent user interactions. Disabled inputs appear lighter and add a not-allowed
cursor.
Disabled fieldsets
Add the disabled
attribute to a <fieldset>
to disable all the controls within the <fieldset>
at once.
Caveat about link functionality of <a>
By default, browsers will treat all native form controls (<input>
, <select>
and <button>
elements) inside a <fieldset disabled>
as disabled, preventing both keyboard and mouse interactions on them. However, if your form also includes <a ... class="btn btn-*">
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 Opera 18 and below, or in Internet Explorer 11, 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.
Cross-browser compatibility
While Bootstrap will apply these styles in all browsers, Internet Explorer 11 and below don't fully support the disabled
attribute on a <fieldset>
. Use custom JavaScript to disable the fieldset in these browsers.
Readonly state
Add the readonly
boolean attribute 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.
Help text
Block level help text for form controls.
Associating help text with form controls
Help text should be explicitly associated with the form control it relates to using the 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.
Validation states
Bootstrap includes validation styles for error, warning, and success states on form controls. To use, add .has-warning
, .has-error
, or .has-success
to the parent element. Any .control-label
, .form-control
, and .help-block
within that element will receive the validation styles.
Conveying validation state to assistive technologies and colorblind users
Using these validation styles to denote the state of a form control only provides a visual, color-based indication, which will not be conveyed to users of assistive technologies - such as screen readers - or to colorblind users.
Ensure that an alternative indication of state is also provided. For instance, you can include a hint about state in the form control's <label>
text itself (as is the case in the following code example), include a Glyphicon (with appropriate alternative text using the .sr-only
class - see the Glyphicon examples), or by providing an additional help text block. Specifically for assistive technologies, invalid form controls can also be assigned an aria-invalid="true"
attribute.
With optional icons
You can also add optional feedback icons with the addition of .has-feedback
and the right icon.
Feedback icons only work with textual <input class="form-control">
elements.
Icons, labels, and input groups
Manual positioning of feedback icons is required for inputs without a label and for input groups with an add-on on the right. You are strongly encouraged to provide labels for all inputs for accessibility reasons. If you wish to prevent labels from being displayed, hide them with the .sr-only
class. If you must do without labels, adjust the top
value of the feedback icon. For input groups, adjust the right
value to an appropriate pixel value depending on the width of your addon.
Conveying the icon's meaning to assistive technologies
To ensure that assistive technologies – such as screen readers – correctly convey the meaning of an icon, additional hidden text should be included with the .sr-only
class and explicitly associated with the form control it relates to using aria-describedby
. Alternatively, ensure that the meaning (for instance, the fact that there is a warning for a particular text entry field) is conveyed in some other form, such as changing the text of the actual <label>
associated with the form control.
Although the following examples already mention the validation state of their respective form controls in the <label>
text itself, the above technique (using .sr-only
text and aria-describedby
) has been included for illustrative purposes.
Optional icons in horizontal and inline forms
Optional icons with hidden .sr-only
labels
If you use the .sr-only
class to hide a form control's <label>
(rather than using other labelling options, such as the aria-label
attribute), Bootstrap will automatically adjust the position of the icon once it's been added.
Control sizing
Set heights using classes like .input-lg
, and set widths using grid column classes like .col-lg-*
.
Height sizing
Create taller or shorter form controls that match button sizes.
Horizontal form group sizes
Quickly size labels and form controls within .form-horizontal
by adding .form-group-lg
or .form-group-sm
.
Column sizing
Wrap inputs in grid columns, or any custom parent element, to easily enforce desired widths.
Buttons
Button tags
Use the button classes on an <a>
, <button>
, or <input>
element.
Context-specific usage
While button classes can be used on <a>
and <button>
elements, only <button>
elements are supported within our nav and navbar components.
Links acting as buttons
If the <a>
elements are used to act as buttons – triggering in-page functionality, rather than navigating to another document or section within the current page – they should also be given an appropriate role="button"
.
Cross-browser rendering
As a best practice, we highly recommend using the <button>
element whenever possible to ensure matching cross-browser rendering.
Among other things, there's a bug in Firefox <30 that prevents us from setting the line-height
of <input>
-based buttons, causing them to not exactly match the height of other buttons on Firefox.
Options
Use any of the available button classes to quickly create a styled button.
Conveying meaning to assistive technologies
Using color to add meaning to a button only provides a visual indication, which will not be conveyed to users of assistive technologies – such as screen readers. Ensure that information denoted by the color is either obvious from the content itself (the visible text of the button), or is included through alternative means, such as additional text hidden with the .sr-only
class.
Sizes
Fancy larger or smaller buttons? Add .btn-lg
, .btn-sm
, or .btn-xs
for additional sizes.
Create block level buttons—those that span the full width of a parent— by adding .btn-block
.
Active state
Buttons will appear pressed (with a darker background, darker border, and inset shadow) when active. For <button>
elements, this is done via :active
. For <a>
elements, it's done with .active
. However, you may use .active
on <button>
s (and include the aria-pressed="true"
attribute) should you need to replicate the active state programmatically.
Button element
No need to add :active
as it's a pseudo-class, but if you need to force the same appearance, go ahead and add .active
.
Anchor element
Add the .active
class to <a>
buttons.
Disabled state
Make buttons look unclickable by fading them back with opacity
.
Button element
Add the disabled
attribute to <button>
buttons.
Cross-browser compatibility
If you add the disabled
attribute to a <button>
, Internet Explorer 9 and below will render text gray with a nasty text-shadow that we cannot fix.
Anchor element
Add the .disabled
class to <a>
buttons.
We use .disabled
as a utility class here, similar to the common .active
class, so no prefix is required.
Link functionality caveat
This class uses pointer-events: none
to try to disable the link functionality of <a>
s, but that CSS property is not yet standardized and isn't fully supported in Opera 18 and below, or in Internet Explorer 11. In addition, even in browsers that do support pointer-events: none
, keyboard navigation remains unaffected, meaning that sighted keyboard users and users of assistive technologies will still be able to activate these links. So to be safe, use custom JavaScript to disable such links.
Dropdowns
Toggleable, contextual menu for displaying lists of links. Made interactive with the dropdown JavaScript plugin.
Example
Wrap the dropdown's trigger and the dropdown menu within .dropdown
, or another element that declares position: relative;
. Then add the menu's HTML.
Dropdown menus can be changed to expand upwards (instead of downwards) by adding .dropup
to the parent.
Alignment
By default, a dropdown menu is automatically positioned 100% from the top and along the left side of its parent. Add .dropdown-menu-right
to a .dropdown-menu
to right align the dropdown menu.
May require additional positioning
Dropdowns are automatically positioned via CSS within the normal flow of the document. This means dropdowns may be cropped by parents with certain overflow
properties or appear out of bounds of the viewport. Address these issues on your own as they arise.
Deprecated .pull-right
alignment
As of v3.1.0, we've deprecated .pull-right
on dropdown menus. To right-align a menu, use .dropdown-menu-right
. Right-aligned nav components in the navbar use a mixin version of this class to automatically align the menu. To override it, use .dropdown-menu-left
.
Headers
Add a header to label sections of actions in any dropdown menu.
Divider
Add a divider to separate series of links in a dropdown menu.
Disabled menu items
Add .disabled
to a <li>
in the dropdown to disable the link.
Button groups
Group a series of buttons together on a single line with the button group. Add on optional JavaScript radio and checkbox style behavior with our buttons plugin.
Tooltips & popovers in button groups require special setting
When using tooltips or popovers on elements within a .btn-group
, you'll have to specify the option container: 'body'
to avoid unwanted side effects (such as the element growing wider and/or losing its rounded corners when the tooltip or popover is triggered).
Ensure correct role
and provide a label
In order for assistive technologies – such as screen readers – to convey that a series of buttons is grouped, an appropriate role
attribute needs to be provided. For button groups, this would be role="group"
, while toolbars should have a role="toolbar"
.
One exception are groups which only contain a single control (for instance the justified button groups with <button>
elements) or a dropdown.
In addition, groups and toolbars should be given an explicit label, as most assistive technologies will otherwise not announce them, despite the presence of the correct role
attribute. In the examples provided here, we use aria-label
, but alternatives such as aria-labelledby
can also be used.
Basic example
Wrap a series of buttons with .btn
in .btn-group
.
Button toolbar
Combine sets of <div class="btn-group">
into a <div class="btn-toolbar">
for more complex components.
Sizing
Instead of applying button sizing classes to every button in a group, just add .btn-group-*
to each .btn-group
, including when nesting multiple groups.
Nesting
Place a .btn-group
within another .btn-group
when you want dropdown menus mixed with a series of buttons.
Vertical variation
Make a set of buttons appear vertically stacked rather than horizontally. Split button dropdowns are not supported here.
Justified button groups
Make a group of buttons stretch at equal sizes to span the entire width of its parent. Also works with button dropdowns within the button group.
Handling borders
Due to the specific HTML and CSS used to justify buttons (namely display: table-cell
), the borders between them are doubled. In regular button groups, margin-left: -1px
is used to stack the borders instead of removing them. However, margin
doesn't work with display: table-cell
. As a result, depending on your customizations to Bootstrap, you may wish to remove or re-color the borders.
IE8 and borders
Internet Explorer 8 doesn't render borders on buttons in a justified button group, whether it's on <a>
or <button>
elements. To get around that, wrap each button in another .btn-group
.
See #12476 for more information.
With <a>
elements
Just wrap a series of .btn
s in .btn-group.btn-group-justified
.
Links acting as buttons
If the <a>
elements are used to act as buttons – triggering in-page functionality, rather than navigating to another document or section within the current page – they should also be given an appropriate role="button"
.
With <button>
elements
To use justified button groups with <button>
elements, you must wrap each button in a button group. Most browsers don't properly apply our CSS for justification to <button>
elements, but since we support button dropdowns, we can work around that.
Button dropdowns
Use any button to trigger a dropdown menu by placing it within a .btn-group
and providing the proper menu markup.
Plugin dependency
Button dropdowns require the dropdown plugin to be included in your version of Bootstrap.
Single button dropdowns
Turn a button into a dropdown toggle with some basic markup changes.
Split button dropdowns
Similarly, create split button dropdowns with the same markup changes, only with a separate button.
Sizing
Button dropdowns work with buttons of all sizes.
Dropup variation
Trigger dropdown menus above elements by adding .dropup
to the parent.
Input groups
Extend form controls by adding text or buttons before, after, or on both sides of any text-based <input>
. Use .input-group
with an .input-group-addon
or .input-group-btn
to prepend or append elements to a single .form-control
.
Textual <input>
s only
Avoid using <select>
elements here as they cannot be fully styled in WebKit browsers.
Avoid using <textarea>
elements here as their rows
attribute will not be respected in some cases.
Tooltips & popovers in input groups require special setting
When using tooltips or popovers on elements within an .input-group
, you'll have to specify the option container: 'body'
to avoid unwanted side effects (such as the element growing wider and/or losing its rounded corners when the tooltip or popover is triggered).
Don't mix with other components
Do not mix form groups or grid column classes directly with input groups. Instead, nest the input group inside of the form group or grid-related element.
Always add labels
Screen readers will have trouble with your forms if you don't include a label for every input. For these input groups, ensure that any additional label or functionality is conveyed to assistive technologies.
The exact technique to be used (visible <label>
elements, <label>
elements hidden using the .sr-only
class, or use of the aria-label
, aria-labelledby
, aria-describedby
, title
or placeholder
attribute) and what additional information will need to be conveyed will vary depending on the exact type of interface widget you're implementing. The examples in this section provide a few suggested, case-specific approaches.
Basic example
Place one add-on or button on either side of an input. You may also place one on both sides of an input.
We do not support multiple add-ons (.input-group-addon
or .input-group-btn
) on a single side.
We do not support multiple form-controls in a single input group.
Sizing
Add the relative form sizing classes to the .input-group
itself and contents within will automatically resize—no need for repeating the form control size classes on each element.
Checkboxes and radio addons
Place any checkbox or radio option within an input group's addon instead of text.
Button addons
Buttons in input groups are a bit different and require one extra level of nesting. Instead of .input-group-addon
, you'll need to use .input-group-btn
to wrap the buttons. This is required due to default browser styles that cannot be overridden.
Buttons with dropdowns
Segmented buttons
Multiple buttons
While you can only have one add-on per side, you can have multiple buttons inside a single .input-group-btn
.