How to style a checkbox using CSS - Informatic Point

How to style a checkbox using CSS

How to style a checkbox using CSS. CSS checkbox is utilized to choose various options from a list of options at a time. Usually, the checkbox is square with a place inside the box. When the user clicks on the box place then it is said to use is selected. The checkbox is the most excellent option for when someone decides on more than one option from the given list of choices then this will be the most suitable option but in the case of a radio button, we can choose only one radio button at a time.

Read Also: CSS Inline Style

Example: Let suppose in an exercise, the specific question commonly has 4 options. In that more question if more than 2 options are correct. We can select them together. But If we take the usual radio button we can select only 1 option at a time but that is not the case. We have to select more than 2 options at a time so we need to utilize the checkbox to achieve our requirements.

Read Also: What Is CSS?

How to style a checkbox using CSS

Types of Checkboxes

There are two types of Checkboxes in CSS

  1. Default checkboxes
  2. Custom checkboxes

Default checkboxes

The default checkbox is not required to add any additional styles. CSS libraries by default provided with some styles for this checkbox.

Read Also: Introduction to CSS Controls

<form action="cart.php">
<label>Have a bike?<br></label>
<input type="checkbox" name="bike">
<input type="submit" value="Submit">
</form>

Custom Checkboxes

This Custom checkbox must require adding additional styles because these are user-required checkboxes so based on their requirement, they have to provide CSS styles.

<style>
.soft {
  display: block;
  position: relative;
  padding-left: 35px;
  margin-bottom: 12px;
  cursor: pointer;
  font-size: 22px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
.soft input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
}
.soft:hover input ~ .checkmark {
  background-color: hotpink;
}
.soft input:checked ~ .checkmark {
  background-color: #2ef6F3;
}
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}
.soft input:checked ~ .checkmark:after {
  display: block;
}
.soft .checkmark:after {
  left: 9px;
  top: 5px;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}
</style>
<h1>Custom Checkboxes</h1>
<p>CSS Stands for?</p>
<label class="soft">Cascading Style Sheet
  <input type="checkbox" checked="checked">
  <span class="checkmark"></span>
</label>

Conclusion

CSS checkboxes can be created by using default styles and custom styles. The default checkbox does not have a rich GUI whereas the custom checkbox has a rich GUI. We can select multiple items at a time in the checkbox. Initially, we can also auto-check any choice number of checkboxes.

Leave a Reply

Your email address will not be published. Required fields are marked *