Bootstrap 4 form provides styling and alignment of form controls elements by providing predefined form classes. Labels, input fields, select box, text area, and buttons are the form control elements.
Bootstrap provides three type of form layout.
<form>
base class. <form>
container having .form-group
i.e( <div>
having .form-group
) while as put checkbox and radio button to the container having class .checkbox
and .radio
correspondingly. <form>
container having .form-group
i.e(<div>
having .form-group
). .form-control
to input fields, selectbox, textarea. Note: Bootstrap creates alignment and styling of form controls. These form controls are like Labels, input fields, select boxes, text area, and buttons are the form control elements.
Source Code
<form action="" method="post">
<div class="form-group">
<label for="User Name">User Name</label>
<input type="TEXT" class="form-control" id="username" name="user'" placeholder="User name" required />
</div>
<div class="radio">
<label><input name="gender" type="radio" value="Male" />Male</label>
</div>
<div class="radio">
<label><input name="gender" type="radio" value="Female" />Female</label>
</div>
<div class="form-group">
<label for="userEmail">Email</label>
<input type="email" class="form-control" id="userEmail" placeholder="User Email" required />
</div>
<div class="form-group">
<label for="userPassword">Password</label>
<input type="password" class="form-control" id="userPassword" placeholder="User Password" required />
</div>
<button type="button" class="btn btn-info disabled">Login</button>
</form>
Try it yourself
Note:
The form controls input fields
,selectbox
, textrea
are placed inside the element having .form-group
while as checkbox
& radio
are places inside the element having .checkbox
&
.radio
respectively.
<form>
base class.
<form>
container having .form-group
& .row
i.e( <div>
having .form-group .row
).Use col-*-*
grid classes to specify the width of your labels and controls.
<div>
having class .form-group and .row) having labels and form controls i.e(input fields
, textrea
, selectbox
), provide .col-form-label
to labels and .form-control
to form controls( input fields
, selectbox
,textrea
).
.form-check-label
to label base class.
Source Code
<form action="" method="post">
<div class="form-group row">
<label for="inputUserName" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputUserName" placeholder="User Name" required />
</div>
</div>
<div class="form-group row">
<label for="inputUserName" class="col-sm-2 col-form-label">Gender</label>
<div class="col-sm-10">
<label class="form-check-label"> <input type="radio" value="male" name="gender" /> male</label>
<label class="form-check-label"><input type="radio" name="gender" value="Female" /> Female</label>
</div>
</div>
<div class="form-group row">
<label for="inputEmail" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail" placeholder="Email" required />
</div>
</div>
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword" placeholder="Password" required />
</div>
</div>
<div class="form-group row">
<div class="col-sm-10 offset-sm-2">
<button type="button" class="btn btn-primary">Sign in</button>
</div>
</div>
</form>
Try it yourself
Note: In the above example horizontal form layout concept is mentioned.Follow the goven step and design horizontal form layout.
Bootstrap inline form display labels,form controls and button on a single horrizontal row.
.form-inline
to
element base class for making inline form.
input fields
, select box
, textarea
,checkbox
, radio
) inside the child class of
container having .form-group
i.e(<div>
having .form-group
).
input fields
, textarea
,select box
),provide .form-control
to form controls(input fields
,selectbox
,textarea
).
Source Code
<form method="post" action="" class="form-inline">
<div class="form-group mr-2">
<label class="sr-only" for="inputEmail">Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="Email" />
</div>
<div class="form-group mr-2">
<label class="sr-only" for="inputPassword">Password</label>
<input type="password" class="form-control" id="inputPassword" placeholder="Password" />
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
Try it yourself
Note: In the above example bootstrap inline form concept is used.It displays labels, form controls, and buttons on a single horizontal row.