Checkbox in Bootstrap
Posted by Superadmin on April 30 2023 15:50:14

Checkbox in Bootstrap

By Mrunali PangeMrunali Pange
  

Checkbox in Bootstrap

Introduction to Checkbox in Bootstrap

Bootstrap used many buttons with one click, but sometimes we need to choose an option. Choosing an option has two ways one is a radio button, and another is a checkbox. The radio button has only one option to choose from others. In any condition, we need to choose more than one option that is the time checkbox working. Checkbox has multiple options to choose from the many options. The checkbox is used to choose options in multiple choices with a click on the checkbox. The use of checkbox buttons is multiple choice questions in the exam when the question has multiple answers to one question. Bootstrap has own class of checkbox button, you will see below.

Examples to Implement Checkbox in Bootstrap

Users can understand how to implement a checkbox and the working of these buttons. Below are the examples to implement checkbox in Boostrap:

  1. Vertical Checkbox
  2. Inline Checkbox

1. Vertical Checkbox

Example of Vertical Checkbox is given in the following.

Code:

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Example vertical checkbox</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2> Checkbox Example 1 </h2>
<form>
<div class="form-group">
<p> Which are seven wonders in following list?  </p>
</div>
<div class="checkbox">
<label><input type="checkbox" value=""> Taj Mahal </label>
</div>
<div class="checkbox">
<label><input type="checkbox" value=""> Egypt Pyramid </label>
</div>
<div class="checkbox">
<label><input type="checkbox" value=""> London Bridge </label>
</div>
<div class="checkbox">
<label><input type="checkbox" value=""> Eiffel Tower </label>
</div>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>

Output:

Checkbox in Bootstrap 1

Description:

2. Inline Checkbox

Example of Inline Checkbox is given in the following.

Code:

<!DOCTYPE html>
<html >
<head>
<title>Bootstrap Example inline checkbox</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2> Checkbox Example 2</h2>
<form class="form-inline">
<label> hobbies: </label>
<label class="checkbox-inline">
<input type="checkbox" value=""> painting
</label>
<label class="checkbox-inline">
<input type="checkbox" value=""> dancing
</label>
<label class="checkbox-inline">
<input type="checkbox" value=""> reading
</label>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>

Output:

Checkbox in Bootstrap 2

Description:

Checkbox Using Buttons

Checkbox in bootstrap also worked out with the buttons with some classes to look more stylish and elegant in form. Class button-group-toggle, button class has to used in < div > and < label > respectively. With the button-group-toggle class, data-toggle=’buttons’ class also necessary in the form. In the < input >, the type must be a checkbox for working as the checkbox. Let’s see some examples to understand more about toggle in checkbox using bootstrap.

Code:

<!DOCTYPE html>
<html >
<head>
<title>Bootstrap Example checkbox</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form>
<label > Languages </label>
<div class="btn-group-toggle" data-toggle = "buttons">
<label class="btn btn-primary ">
<input type="checkbox" checked autocomplete = "off"> HTML
</label>
<label class="btn btn-primary">
<input type="checkbox" checked autocomplete = "off"> CSS
</label>
<label class="btn btn-primary">
<input type="checkbox" checked autocomplete = "off"> JavaScript
</label>
<label class="btn btn-primary">
<input type="checkbox" checked autocomplete = "off"> Bootstrap
</label>
</div>
</form>
</div>
</body>
</html>

Output:

Languages1

Description:

Using the Active Button of Checkbox in Bootstrap

If the user active class adds in button then this button automatically is checked and users have remaining options to choose. This button changes the color to get the activated sign.

Let’s see the following example:

Code:

<div class="container">
<form>
<label > Languages </label>
<div class="btn-group-toggle"data-toggle= "buttons">
<label class="btn btn-primary active ">
<input type="checkbox" checked autocomplete = "off"> HTML
</label>
<label class="btn btn-primary">
<input type="checkbox" checked autocomplete = "off"> CSS
</label>
<label class="btn btn-primary">
<input type="checkbox" checked autocomplete = "off"> JavaScript
</label>
<label class="btn btn-primary">
<input type="checkbox" checked autocomplete = "off"> Bootstrap
</label>
</div>
</form>
</div>

Output:

Languages2

Description:

Conclusion

Checkbox is useful If the job has multiple options to choose for one condition. User can select more than one option which is required for the task. Checkbox Check means Yes or Not Checked means no. Minimum two mutual conditions can select using a checkbox.