in.
Add class to the label .control-label 。
Example
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="firstname" class=">col-sm-2 control-label
"
First name
</
label
<
div
class
=
"
col-sm-10
"
<
input
type
=
"
text
"
class
=
"
form-control
"
id
=
"
firstname
"
placeholder
=
"Please enter first name
"
</
div
</
div
<
div
class
=
"
form-group
"
<
label
for
=
"
lastname
"
class
=
"col-sm-2 control-label
"
Last name
</
label
<
div
class
=
"
col-sm-10
"
<
input
type
=
"
text
"
class
=
"
form-control
"
id
=
"
lastname
"
placeholder
=
"Please enter last name
"
</
div
</
div
<
div
class
=
"
form-group
"
<
div
class
=
"col-sm-offset-2 col-sm-10
"
<div class="checkbox">
<label>
<input type="checkbox">Remember me
</
label
</
div
</
div
</
div
<
div
class
=
"
form-group
"
<
div
class
=
"col-sm-offset-2 col-sm-10
"
<button type="submit" class=">btn btn-default
"
Sign in
</
button
</
div
</
div
</
formTry it >>
The result is as follows:
Supported form controls
Bootstrap supports the most common form controls, mainly input, textarea, checkbox, radio, and select 。
Input field (Input)
The most common form text field is the input field. Users can enter most of the necessary form data into it. Bootstrap provides support for all native HTML5 input types, including:text、password、datetime、datetime-local、date、month、time、week、number、email、url、search、tel and color . Appropriate type declaration is required, so that input gets complete styling.
Example
<form role="form">
<div class="form-group">
<label for="name" Label>
</label>
<input type="text" class="form-control" placeholder=">Text input
"
</
div
</
formTry it >>
The result is as follows:
Text area (Textarea)
When you need multi-line input, you can use the textarea text box. If necessary, you can change the rows attribute (fewer rows = smaller box, more rows = larger box).
Example
<form role="form">
<div class="form-group">
<label for="name">Text box
</
label
<
textarea
class
=
"
form-control
"
rows
=
"
3
"
</
textarea
</
div
</
formTry it >>
The result is as follows:
Check box (Checkbox) and Radio button (Radio)
Checkboxes and radio buttons are used to let users select from a set of predefined options.
When creating a form, if you want users to select several options from a list, use checkbox . If you restrict users to only one option, use radio 。
For a series of checkboxes and radio buttons, use .checkbox-inline or .radio-inline class, controlling them to be displayed on the same line.
The following examples demonstrate these two types (default and inline):
Example
<label for="name">Example of default checkboxes and radio buttons
</
label
<
div
class
=
"
checkbox
"
<
label
<
input
type
=
"
checkbox
"
value
=
"
"Option 1
</
label
</
div
<
div
class
=
"
checkbox
"
<
label
<
input
type
=
"
checkbox
"
value
=
"
"Option 2
</
label
</
div
<
div
class
=
"
radio
"
<
label
<
input
type
=
"
radio
"
name
=
"
optionsRadios
"
id
=
"
optionsRadios1
"
value
=
"
option1
"
checkedOption 1
</
label
</
div
<
div
class
=
"
radio
"
<
label
<
input
type
=
"
radio
"
name
=
"
optionsRadios
"
id
=
"
optionsRadios2
"
value
=
"
option2
"Option 2 - selecting it will deselect option 1
</
label
</
div
<
label
for
=
"
name
"Example of inline checkboxes and radio buttons
</
label
<
div
<
label
class
=
"
checkbox-inline
"
<
input
type
=
"
checkbox
"
id
=
"
inlineCheckbox1
"
value
=
"
option1
"Option 1
</
label
<
label
class
=
"
checkbox-inline
"
<
input
type
=
"
checkbox
"
id
=
"
inlineCheckbox2
"
value
=
"
option2
"Option 2
</
label
<
label
class
=
"
checkbox-inline
"
<
input
type
=
"
checkbox
"
id
=
"
inlineCheckbox3
"
value
=
"
option3
"Option 3
</
label
<
label
class
=
"
radio-inline
"
<
input
type
=
"
radio
"
name
=
"
optionsRadiosinline
"
id
=
"
optionsRadios3
"
value
=
"
option1
"
checkedOption 1
</
label
<
label
class
=
"
radio-inline
"
<
input
type
=
"
radio
"
name
=
"
optionsRadiosinline
"
id
=
"
optionsRadios4
"
value
=
"
option2
"Option 2
</
label
</
divTry it >>
The result is as follows:
Select box (Select)
When you want users to select from multiple options but only one by default, use a select box.
Use to display list options, usually those selection lists that users are familiar with, such as states or numbers.
Use multiple="multiple" Allow users to select multiple options.
The following examples demonstrate these two types (select and multiple):
Example
<form role="form">
<div class="form-group">
<label for="name">Select list
</
label
<
select
class
=
"
form-control
"
<
option
1
</
option
<
option
2
</
option
<
option
3
</
option
<
option
4
</
option
<
option
5
</
option
</
select
<
label
for
=
"
name
"Multiple-select list
</
label
<
select
multiple
class
=
"
form-control
"
<
option
1
</
option
<
option
2
</
option
<
option
3
</
option
<
option
4
</
option
<
option
5
</
option
</
select
</
div
</
formTry it >>
The result is as follows:
Static controls
When you need to place plain text after a form label in a horizontal form, on the
use class .form-control-static 。
Example
<form class="form-horizontal" role="form">
<div class="form-group">
<label class=">col-sm-2 control-label
"
Email
</
label
<
div
class
=
"
col-sm-10
"
<
p
class
=
"
form-control-static
"
email@example.com
</
p
</
div
</
div
<
div
class
=
"
form-group
"
<
label
for
=
"
inputPassword
"
class
=
"col-sm-2 control-label
"
Password
</
label
<
div
class
=
"
col-sm-10
"
<
input
type
=
"
password
"
class
=
"
form-control
"
id
=
"
inputPassword
"
placeholder
=
"Please enter password
"
</
div
</
div
</
formTry it >>
The result is as follows:
Form control states
In addition to :focus state (i.e., the user clicks the input or uses the tab key to focus on the input), Bootstrap also defines styles for disabled input fields and provides form validation classes.
Input focus
When the input field receives :focus , the outline of the input field is removed, and also applies box-shadow 。
Disabled input field
If you want to disable an input field, simply add disabled attribute, which not only disables the input field but also changes its style and the cursor style when the mouse pointer hovers over the element.
Disabled fieldset
To the
add the disabled attribute to disable all controls inside it.
Validation states
Bootstrap includes validation styles for error, warning, and success messages. Simply add the appropriate class (.has-warning, .has-error, or .has-success ) to use validation states.
The following example demonstrates all control states:
Example
<form class="form-horizontal" role="form">
<div class="form-group">
<label class=">col-sm-2 control-label
"
Focus
</
label
<
div
class
=
"
col-sm-10
"
<
input
class
=
"
form-control
"
id
=
"
focusedInput
"
type
=
"
text
"
value
=
"This input field has focus...
"
</
div
</
div
<
div
class
=
"
form-group
"
<
label
for
=
"
inputPassword
"
class
=
"col-sm-2 control-label
"
Disabled
</
label
<
div
class
=
"
col-sm-10
"
<
input
class
=
"
form-control
"
id
=
"
disabledInput
"
type
=
"
text
"
placeholder
=
"This input field is disabled...
"
disabled
</
div
</
div
<
fieldset
disabled
<
div
class
=
"
form-group
"
<
label
for
=
"
disabledTextInput
"
class
=
"col-sm-2 control-label
"
Disabled input (Fieldset disabled)
</
label
<
div
class
=
"
col-sm-10
"
<
input
type
=
"
text
"
id
=
"
disabledTextInput
"
class
=
"
form-control
"
placeholder
=
"Disabled input
"
</
div
</
div
<
div
class
=
"
form-group
"
<
label
for
=
"
disabledSelect
"
class
=
"col-sm-2 control-label
"
Disabled select menu (Fieldset disabled)
</
label
<
div
class
=
"
col-sm-10
"
<
select
id
=
"
disabledSelect
"
class
=
"
form-control
"
<
optionSelection disabled
</
option
</
select
</
div
</
div
</
fieldset
<
div
class
=
"form-group has-success
"
<label class=">col-sm-2 control-label
"
for
=
"
inputSuccess
"
Input success
</
label
<
div
class
=
"
col-sm-10
"
<
input
type
=
"
text
"
class
=
"
form-control
"
id
=
"
inputSuccess
"
</
div
</
div
<
div
class
=
"form-group has-warning
"
<label class=">col-sm-2 control-label
"
for
=
"
inputWarning
"
Input warning
</
label
<
div
class
=
"
col-sm-10
"
<
input
type
=
"
text
"
class
=
"
form-control
"
id
=
"
inputWarning
"
</
div
</
div
<
div
class
=
"form-group has-error
"
<label class=">col-sm-2 control-label
"
for
=
"
inputError
"
Input error
</
label
<
div
class
=
"
col-sm-10
"
<
input
type
=
"
text
"
class
=
"
form-control
"
id
=
"
inputError
"
</
div
</
div
</
formTry it >>
The result is as follows:
Form control sizing
You can use the class .input-lg and .col-lg- * to set the height and width of the form. The following example demonstrates this:
Example
<form role="form">
<div class="form-group">
<input class=">form-control input-lg
"
type
=
"
text
"
placeholder
=
"
.input-lg
"
</
div
<
div
class
=
"
form-group
"
<
input
class
=
"
form-control
"
type
=
"
text
"
placeholder
=
"Default input
"
</
div
<
div
class
=
"
form-group
"
<
input
class
=
"form-control input-sm
"
type
=
"
text
"
placeholder
=
"
.input-sm
"
</
div
<
div
class
=
"
form-group
"
</
div
<
div
class
=
"
form-group
"
<
select
class
=
"form-control input-lg
"
<option value="" .input-lg>
</option>
</select>
</div>
<div class="form-group">
<select class="form-control">
<option value="">Default select
</
option
</
select
</
div
<
div
class
=
"
form-group
"
<
select
class
=
"form-control input-sm
"
<option value="" .input-sm>
</option>
</select>
</div>
<div class="row">
<div class="col-lg-2">
<input type="text" class="form-control" placeholder=".col-lg-2" div>
<div class="col-lg-3">
<input type="text" class="form-control" placeholder=".col-lg-3" div>
<div class="col-lg-4">
<input type="text" class="form-control" placeholder=".col-lg-4" div div form>Try it »
The result is as follows:
Form Help Text
Bootstrap form controls can have block-level help text on an input. To add a block of content that spans the full width, use after .help-block . The following example demonstrates this:
Example
<form role="form">
<span>Help Text Example
span
<input class="form-control" type="text" placeholder="">
<span class="help-block">A longer block of help text that exceeds one line needs to extend to the next line. The help text in this example has two lines in total.
span
form
Try it »
The result is as follows:
Source: Runoob Tutorial · Original modified: 2017-07-26T14:12:11 · This copy is for personal offline learning only; public distribution is prohibited.