Bootstrap is a popular front-end development framework that makes it easy to create responsive and attractive web pages. One of the key features of Bootstrap is the built-in support for various form elements, including checkboxes. In this article, we will explore how to create and customize checkboxes using Bootstrap, with several code examples to illustrate the different options available.
Creating a basic checkbox in Bootstrap is simple. You can use the .form-check
class on a <div>
element to create a container for the checkbox, and the .form-check-input
class on the <input>
element to style it as a checkbox. Here's an example:
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">
Default checkbox
</label>
</div>
This will create a basic checkbox that looks like this:
You can also use the .form-check-inline
class on the <div>
element to create an inline checkbox, like this:
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" value="" id="exampleCheck2">
<label class="form-check-label" for="exampleCheck2">
Inline checkbox
</label>
</div>
You can also customize the appearance of checkboxes using Bootstrap's built-in classes. For example, you can use the .form-check-input:checked
class to change the appearance of a checkbox when it's selected, like this:
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="exampleCheck3" checked>
<label class="form-check-label" for="exampleCheck3">
Custom checkbox
</label>
</div>
.form-check-input:checked + .form-check-label::before {
color: green;
}
You can also use Bootstrap's built-in classes to create a switch-style toggle. To do this, you can use the .custom-switch
class on the <div>
element and the .custom-control-input
class on the <input>
element. Here's an example:
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="customSwitch1">
<label class="custom-control-label" for="customSwitch1">Toggle this switch element</label>
</div>
![Bootstrap switch example](https
Sure, in addition to creating and customizing checkboxes, Bootstrap also provides a number of other form controls and options for creating responsive and user-friendly forms. Some of the other form elements that you can use with Bootstrap include:
- Radio buttons: You can use the
.form-check
and.form-check-input
classes, along with thetype="radio"
attribute on the<input>
element, to create radio buttons. - Select menus: You can use the
.form-control
class on a<select>
element to style it as a select menu. You can also use the.custom-select
class to create a custom-styled select menu. - Text inputs: You can use the
.form-control
class on a<input>
element with thetype="text"
attribute to create a text input. - Textareas: You can use the
.form-control
class on a<textarea>
element to create a textarea. - File inputs: You can use the
.form-control-file
class on a<input>
element with thetype="file"
attribute to create a file input.
Bootstrap also provides several other classes and options for customizing the layout and appearance of forms, such as:
- Form groups: You can use the
.form-group
class on a<div>
element to create a container for a group of form controls. - Inline forms: You can use the
.form-inline
class on a<form>
element to create an inline form. - Horizontal forms: You can use the
.form-horizontal
class on a<form>
element to create a horizontal form, where the labels are aligned next to the form controls. - Form validation: You can use Bootstrap's built-in validation classes, such as
.is-valid
and.is-invalid
, to provide visual feedback to users when a form is submitted with errors.
Here's an example of a form created using Bootstrap classes:
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
I hope this gives
Popular questions
- What is the class used to create a container for a checkbox in Bootstrap?
- The
.form-check
class is used to create a container for a checkbox in Bootstrap.
- How can you create an inline checkbox in Bootstrap?
- To create an inline checkbox in Bootstrap, you can use the
.form-check-inline
class on the<div>
element that contains the checkbox.
- What is the class used to style an
<input>
element as a checkbox in Bootstrap?
- The
.form-check-input
class is used to style an<input>
element as a checkbox in Bootstrap.
- How can you customize the appearance of a checkbox when it is selected in Bootstrap?
- To customize the appearance of a checkbox when it is selected in Bootstrap, you can use the
.form-check-input:checked
class, and use CSS to change the appearance of the checkbox.
- How can you create a switch-style toggle in Bootstrap?
- To create a switch-style toggle in Bootstrap, you can use the
.custom-switch
class on the<div>
element that contains the toggle, and the.custom-control-input
class on the<input>
element.
Tag
Bootstrap