Article 7. Form and Article 7 Form
Bootstrap provides three forms: vertical forms, inline forms, and horizontal forms.
Create a vertical or basic form:
- · Add to the parent <form> elementRole = "form".
- · Place tags and controls in a class. Form-groupIn <div>. This is required to obtain the optimal spacing.
- · Add a class to all text elements <input>, <textarea>, and <select>. Form-control
<Form role = "form">
<Div class = "form-group">
<Label for = "name"> name </label>
<Input type = "text" class = "form-control" id = "name"
Placeholder = "enter a name">
</Div>
<Div class = "form-group">
<Label for = "inputfile"> file input </label>
<Input type = "file" id = "inputfile">
<P class = "help-block"> here is an instance of block-level help text. </P>
</Div>
<Div class = "checkbox">
<Label>
<Input type = "checkbox"> check
</Label>
</Div>
<Button type = "submit" class = "btn-default"> submit </button>
</Form>
Inline form:
All the elements in the inline form are aligned to the left and labels are side by side. To create an inline form, you must add the class to the form label.
. Form-inline
<Form class = "form-inline" role = "form">
<Div class = "form-group">
<Label class = "sr-only" for = "name"> name </label>
<Input type = "text" class = "form-control" id = "name"
Placeholder = "enter a name">
</Div>
<Div class = "form-group">
<Label class = "sr-only" for = "inputfile"> file input </label>
<Input type = "file" id = "inputfile">
</Div>
<Div class = "checkbox">
<Label>
<Input type = "checkbox"> check
</Label>
</Div>
<Button type = "submit" class = "btn-default"> submit </button>
</Form>
The display effect is as follows:
Note: by default, the input, select, and textarea values in Bootstrap are 100% in width. When using an inline form, you must set a width on the Form Control.
Horizontal form: Unlike the other two forms, follow these steps to create a horizontal form.
- Add a class to the parent <form> element. Form-horizontal.
- Place tags and controls in a class. Form-groupIn <div>.
- Add class to label. Control-label.
For example:
<Form class = "form-horizontal" role = "form">
<Div class = "form-group">
<Label for = "firstname" class = "col-sm-2 control-label"> name </label>
<Div class = "col-sm-10">
<Input type = "text" class = "form-control" id = "firstname"
Placeholder = "enter a 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 = "Enter the 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-default"> log on </button>
</Div>
</Div>
</Form>