Bootstrap Forms
Bootstrap Forms
COLLEGE,PALSANA
SUCHIKA D.PATEL
7/1/2023
SDJ INTERNATIONAL COLLEGE,PALSANA BOOTSTRAP FORMS
Bootstrap Forms
In this chapter we will study how to create forms with ease using Bootstrap.
Bootstrap makes it easy with the simple HTML markup and extended classes for
different styles of forms.
Form Layout
Bootstrap provides you with following types of form layouts:
• Inline form
• Horizontal form
The basic form structure comes with Bootstrap; individual form controls
automatically receive some global styling. To create a basic form do the following:
• Add a role form to the parent <form> element.
• Wrap labels and controls in a <div> with class .form-group. This is needed
for optimum spacing.
Add a class of .form-control to all textual <input>, <textarea>, and <select>
elements.
<form role="form">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name"
placeholder="Enter Name">
</div>
<div class="form-group">
<label for="inputfile">File input</label>
<input type="file" id="inputfile">
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label></div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
2 PREPARED BY: | PATEL SUCHIKA D.
SDJ INTERNATIONAL COLLEGE,PALSANA BOOTSTRAP FORMS
Output:
❖ INLINE FORM:
• To create a form where all of the elements are inline, left aligned and labels
are alongside, add the class .form- inline to the <form> tag.
• By default Inputs, selects, and textareas have 100% width in Bootstrap.
You need to set a width on the form controls when using inline form.
• Using the class .sr-only you can hide the labels of the inline forms.
Output:
❖ HORIZONTAL FORM:
Horizontal forms stand apart from the others not only in the amount of
markup, but also in the presentation of the form. To create a form that uses
the horizontal layout, do the following:
• Add a class of .form-horizontal to the parent <form> element.
• Wrap labels and controls in a <div> with class .form-group.
Add a class of .control-label to the labels.
➢ INPUTS:
o The most common form text field is the input—this is where users will
enter most of the essential form data. Bootstrap offers support for all
native HTML5 input types: text, password, datetime, datetime-local,
date, month, time, week, number, email, url, search, tel, and color. Proper
type declaration is required to make Inputs fully styled.
<form role="form">
<div class="form-group">
<label for="name">Label</label>
<input type="text" class="form-control" placeholder="Text input">
</div>
</form>
➢ TEXTAREA
The textarea is used when you need multiple lines of input. Change rows
attribute as necessary (fewer rows = smaller box, more rows = bigger box).
<form role="form">
<div class="form-group">
<label for="name">Text Area</label>
<textarea class="form-control" rows="3"></textarea>
</div>
</form>
Output:
➢ SELECTS
• A select is used when you want to allow the user to pick from multiple
options, but by default it only allows one.
• Use <select> for list options with which the user is familiar, such as states or
numbers.
• Use multiple="multiple" to allow the user to select more than one option.
The following example demonstrates both (select and multiple) types.
<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>
➢ Static control:
Use the class .form-control-static on a <p>, when you need to place plain text next
to a form label within a horizontal form.
<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 protected]</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="Password">
</div>
</div>
</form>
Output:
➢ DISABLED FIELDSETS
Add the disabled attribute to a <fieldset> to disable all the controls within the
<fieldset> at once.
➢ VALIDATION STATES
Bootstrap includes validation styles for error, warning, and success messages. To
use, simply add the appropriate class (.has-warning, .has-error, or .has-success) to
the parent element.
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="col-sm-2 control-label">Focused</label>
<div class="col-sm-10">
<input class="form-control"
id="focusedInput" type="text"
value="This is focused...">
</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="Disabled input here..."
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
10 PREPARED BY: | PATEL SUCHIKA (FieldsetD.disabled)
</label>
SDJ INTERNATIONAL COLLEGE,PALSANA BOOTSTRAP FORMS
<div class="col-sm-10">
<select id="disabledSelect" class="form-control">
<option>Disabled select</option>
</select></div></div>
</fieldset>
<div class="form-group has-success">
<label class="col-sm-2 control-label"
for="inputSuccess"> Input with 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 with 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 with error
</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputError">
</div>
</div>
</form>
<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>