Custom Checkbox with CSS3 Borders and Transitions

Alt Text

Introduction

Making your own custom checkbox has always been a good way to standardize your designs. I recently saw a checkbox like this and I thought it was implemented with only borders so I tried to recreate it here. Basically the checkbox is a regular box with a border that transforms into a rectangle with only the bottom and right edge opaque creating the checkmark look without any extra elements or graphics needed.

Custom Checkbox Structure

There is no special HTML structure as I applied the styles to the input tag directly. The only requirement is for each input to be tagged with a label because the custom styles are applied onto the label element.

<form action="#">
  <p>
    <input type="checkbox" id="test1" />
    <label for="test1">Red</label>
  </p>
  <p>
    <input type="checkbox" id="test2" checked="checked" />
    <label for="test2">Yellow</label>
  </p>
  <p>
    <input type="checkbox" id="test3" checked="checked" disabled="disabled" />
    <label for="test3">Green</label>
  </p>
    <p>
      <input type="checkbox" id="test4" disabled="disabled" />
      <label for="test4">Brown</label>
  </p>
</form>

Custom Checkbox Styles

First you remove the browser’s default checkbox. Then you add before and after pseudo classes to the label which defines your custom checkbox. The checkmark is just a transform on the border property making the top and the left border transparent while also rotating and stretching it to give it a checkmark look.

/* Remove default checkbox */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
  position: absolute;
  left: -9999px;
}
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {  
  position: relative;
  overflow: hidden;
  padding-left: 35px;
  cursor: pointer;
  display: inline-block;
  height: 25px;
  line-height: 25px;

  -webkit-user-select: none; /* webkit (safari, chrome) browsers */
  -moz-user-select: none; /* mozilla browsers */
  -khtml-user-select: none; /* webkit (konqueror) browsers */
  -ms-user-select: none; /* IE10+ */
}

/* checkbox aspect */
[type="checkbox"] + label:before,
[type="checkbox"] + label:after {
  content: '';
  position: absolute;
  left: 0;
  z-index: 1;

  -webkit-transition: .2s;
  transition: .2s;
}
/* Unchecked styles */
[type="checkbox"]:not(:checked) + label:before {
  top: 0px;
  width: 19px; height: 19px;
  border: 3px solid #ddd;
}
[type="checkbox"]:not(:checked) + label:after {
  top: 0px;
  width: 19px; height: 19px;
  border: 3px solid #eee;
  z-index: 0;
}
/* Checked styles */
[type="checkbox"]:checked + label:before {
  top: 2px;
  width: 6px; height: 12px;
  border-top: 3px solid transparent;
  border-left: 3px solid transparent;
  border-right: 3px solid #7fc900;
  border-bottom: 3px solid #7fc900;
  -webkit-transform: rotateZ(37deg);
  transform: rotateZ(37deg);

  -webkit-transform-origin: 20% 40%;
  transform-origin: 100% 100%;
}
[type="checkbox"]:checked + label:after {
  top: 0px;
  width: 19px; height: 19px;
  border: 3px solid #eee;
  background-color: #eee;
  z-index: 0;
}
/* disabled checkbox */
[type="checkbox"]:disabled:not(:checked) + label:before,
[type="checkbox"]:disabled:checked + label:before {
  top: 0;
  box-shadow: none;
  background-color: #444;
  width: 19px; height: 19px;
  border: 3px solid #444;
  -webkit-transform: rotateZ(0deg);
  transform: rotateZ(0deg);
}
[type="checkbox"]:disabled + label {
  color: #555;
}
[type="checkbox"]:disabled:not(:checked) + label:hover:before {
  border-color: #444;
}

/* hover style just for information */
[type="checkbox"]:not(:checked) + label:hover:before {
  border-color: #fff;
}