Poo123
Poo123

Reputation: 49

Custom checkbox tick

I want to know how to make a check inside of my custom checkbox after click. I try to hide the normal checkbox below my custom one. How can I get a black and simple tick, after user ticks the checkbox?

// Custom Checkbox
.xCheckbox {
  width: 25px;
  margin-left: 40px;
  position: relative;
}


/**
    * The box
    */

.xCheckbox label {
  margin-top: 5px;
  position: absolute;
  cursor: pointer;
  width: 20px;
  height: 20px;
  border-radius: 3px;
  top: 0;
  left: 0;
  background-color: blue;
  border: 1px solid #ddd;
}


/**
    * The tick
    */

.xCheckbox input[type=checkbox]:checked+label {
  transform: rotate(-45deg);
  /* Testing purpose */
}
<div class="xCheckbox">
  <input type="checkbox" value="1" id="xCheckboxInput" name="" />
  <label for="xCheckboxInput"></label>
</div>

http://codepen.io/Maartinshh/pen/dvByge

Upvotes: 1

Views: 18673

Answers (1)

sol
sol

Reputation: 22959

Create the tick using a pseudoelement on the label and hide it by default. When the input is checked, show the tick.

Also, to hide the default input box you can set it's opacity to 0:

#customCheckboxInput {
  opacity: 0;
}

codepen

// Custom Checkbox
.customCheckbox {
  width: 25px;
  margin-left: 40px;
  position: relative;
}

#customCheckboxInput {
  opacity: 0;
}


/**
 * The box
 */

.customCheckbox label {
  margin-top: 5px;
  position: absolute;
  cursor: pointer;
  width: 20px;
  height: 20px;
  border-radius: 3px;
  top: 0;
  left: 0;
  background-color: blue;
  border: 1px solid black;
}


/**
 * The tick
 */

.customCheckbox input[type=checkbox]+label::after {
  content: "\2713";
  color: #000;
  text-align: center;
  opacity: 0;
  position: absolute;
  left: 2px;
  transform: rotate(45deg);
  font-weight: bold;
}

.customCheckbox input[type=checkbox]:checked+label {
  transform: rotate(-45deg);
  /* Testing purpose */
  cursor: pointer;
  /* doesnt work like supposed to */
}

.customCheckbox input[type=checkbox]:checked+label::after {
  opacity: 1;
}
<div class="customCheckbox">
  <input type="checkbox" value="1" id="customCheckboxInput" name="" />
  <label for="customCheckboxInput"></label>
</div>

update

As requested, to create this effect with multiple checkboxes requires reworking of HTML and CSS.

Basically...wrap input and label in a container div. This solves the problem of position: absolute overlapping labels.

I've tried to simplify the initial CSS in this example, most notably creating the custom checkbox entirely from a pseudoelement, and not styling the label. Transform property removed from the demo for simplicity.

codepen

.input-container {
  margin-bottom: 10px;
}

/* use :not selector here to support older broswers 
   more info: http://code.stephenmorley.org/html-and-css/styling-checkboxes-and-radio-buttons/
*/

.input-container input[type=checkbox]:not(old) {
  opacity: 0;
  /* match the dimentsions of the custom label */
  width: 20px;
  height: 20px;
  padding: 0;
  margin: 0;
}

/**
 * The box
 */

.input-container label {
  position: relative;
}

.input-container label::after {
  content: "";
  position: absolute;
  /*cover the default input */
  top: -5px;
  left: -20px;
  width: 20px;
  height: 20px;
  border-radius: 3px;
  background-color: blue;
  border: 1px solid black;
  transform: rotate(0);
  pointer-events: none;
  text-align: center;
  color: #FFF; /* easier to see in demo */
}

/**
 * The tick
 */

.input-container input[type=checkbox]:checked+label:after {
  content: "\2713";
}
<div class="customCheckbox">
  <div class="input-container">
    <input type="checkbox" value="1" name="" />
    <label for="customCheckboxInput"></label>
  </div>
  <div class="input-container">
    <input type="checkbox" value="1" name="" />
    <label for="customCheckboxInput"></label>
  </div>
  <div class="input-container">
    <input type="checkbox" value="1" name="" />
    <label for="customCheckboxInput"></label>
  </div>
  <div class="input-container">
    <input type="checkbox" value="1" name="" />
    <label for="customCheckboxInput"></label>
  </div>
</div>

Upvotes: 3

Related Questions