Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a tick mark in a custom checkbox?

Tags:

html

css

I want to make a custom checkbox with CSS, but not sure how do it.

.custom-checkbox {
  border: 3px solid #7e8a94;
  float: right;
  height: 20px;
  width: 20px;
  border-radius: 5px;
  cursor: pointer;
  display: inline-block;
}

.custom-checkbox.hover {
  border: 3px solid #43D8B0;
}

.custom-checkbox.active {
  border: 3px solid #43D8B0;
}

.custom-checkbox.active.checkmark {}
<input type='checkbox' class='checkbox'>
<div class="custom-checkbox">
  <div class="checkmark"></div>
</div>

I added active class to custom-checkbox on checking, but not sure how to create a tick inside a box then?

like image 382
Leff Avatar asked Apr 20 '17 12:04

Leff


2 Answers

You have to manually customize your input & add a label in a way using pseudo elements to get the desired effect in the checkbox you want here.

Also, you can use content: '✔'; in your css to provide a tick on click.

I built you a demo to refer to, check the following code:

[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
  position: absolute;
  left: -9999px;
}

[type="checkbox"]:not(:checked)+label,
[type="checkbox"]:checked+label {
  position: relative;
  padding-left: 25px;
  cursor: pointer;
}

[type="checkbox"]:not(:checked)+label:before,
[type="checkbox"]:checked+label:before {
  content: '';
  position: absolute;
  left: 0;
  top: -5px;
  width: 40px;
  height: 40px;
  background: #fff;
  border-radius: 3px;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, .1);
  border-radius: 50%;
  background-color: #5cf1b3;
  outline: none;
}

[type="checkbox"]:not(:checked)+label:after,
[type="checkbox"]:checked+label:after {
  content: '✔';
  position: absolute;
  top: 8px;
  left: 10px;
  font-size: 24px;
  line-height: 0.8;
  color: #fff;
  transition: all .2s;
}

[type="checkbox"]:not(:checked)+label:after {
  opacity: 0;
  transform: scale(0);
}

[type="checkbox"]:checked+label:after {
  opacity: 1;
  transform: scale(1);
}
<p>
  <input type="checkbox" id="test1" />
  <label for="test1"></label>
</p>
like image 134
nashcheez Avatar answered Sep 22 '22 19:09

nashcheez


You will need to use :checked pseudo class for it. In the following example it's using a span tag to create the custom checkbox style, and using a pseudo element :before with a special character inside for the tick.

.custom-checkbox input {
  display: none;
}

.custom-checkbox span {
  border: 3px solid #7e8a94;
  float: right;
  height: 20px;
  width: 20px;
  border-radius: 5px;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
}

.custom-checkbox:hover span,
.custom-checkbox input:checked + span {
  border: 3px solid #43D8B0;
}

.custom-checkbox input:checked + span:before {
  content: "✔";
}
<label class="custom-checkbox">
  <input type="checkbox">
  <span></span>
</label>
like image 33
Stickers Avatar answered Sep 23 '22 19:09

Stickers