umerata
8/30/2017 - 12:43 PM

Custom Checkbox Input | label-css

Custom Checkbox Input

<ul class="unstyled centered">
    <li>
        <div class="checkbox">
            <input class="custom-checkbox" id="cbid1" type="checkbox" value="value1">
            <label for="cbid1">Checkbox</label>
        </div>
    </li>
    <li>
        <div class="checkbox">
            <input class="custom-checkbox" id="cbid2" type="checkbox" value="value2" checked>
            <label for="cbid2">CSS Only</label>
        </div>
    </li>
    <li>
        <div class="checkbox">
            <input class="custom-checkbox" id="cbid3" type="checkbox" value="value3" disabled>
            <label for="cbid3">A disabled checkbox</label>
        </div>
    </li>
    <li>
        <div class="checkbox">
            <input class="custom-checkbox" id="cbid4" type="checkbox" value="value4">
            <label for="cbid4">Fourth option</label>
        </div>
    </li>
</ul>
.custom-checkbox {
  position: absolute; // take it out of document flow
  opacity: 0; // hide it

  & + label {
    position: relative;
    cursor: pointer;
    padding: 0;
  }

  // Box.
  & + label:before {
    content: '';
    margin-right: 10px;
    display: inline-block;
    vertical-align: text-top;
    width: 20px;
    height: 20px;
    background: white;
    border: 1px solid #000;
  }

  // Box hover
  &:hover + label:before {
    background: #5591dc;
  }

  // Box focus
  &:focus + label:before {
    //box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.12);
  }

  // Box checked
  &:checked + label:before {
    background: #5591dc;
  }

  // Disabled state label.
  &:disabled + label {
    color: #b8b8b8;
    cursor: auto;
  }

  // Disabled box.
  &:disabled + label:before {
    box-shadow: none;
    background: #ddd;
  }

  // Checkmark. Could be replaced with an image
  &:checked + label:after {
    content: '';
    position: absolute;
    left: 5px;
    top: 9px;
    background: white;
    width: 2px;
    height: 2px;
    box-shadow:
    2px 0 0 white,
    4px 0 0 white,
    4px -2px 0 white,
    4px -4px 0 white,
    4px -6px 0 white,
    4px -8px 0 white;
    transform: rotate(45deg);
  }
}