Back in 2015, I was experimenting with custom checkboxes using CSS only, but at the time I couldn’t figure out a cross-browser solution for checkboxes that I could easily use.

Alas! It is 2019 and I’ve redone the checkboxes with a pure CSS version using no background image to do the dirty work. You can simply drop this code into your webpage and the result will be nicely styled accessible checkboxes.

You can see a live example on Codepen here.

The output

Here’s what she’ll look like…

Custom Pure CSS Checkboxes using Sass

You’ll be able to customise the CSS to your hearts content, including the sizing, colours, spacing etc. I’ve provided this example as a boilerplate with nothing that should override your style-choices. You’ll need to:

  1. Copy the paste the HTML code.
  2. Added the Sass code and compile it into your processing tools.
  3. View the HTML file in a browser, woo!

The Markup

Copy and paste this example code into your document. You can leave the names and IDs as they are but you will need to modify these for your particular use-case when you’re ready.

HTML

<!-- Demo wrapper -->
<div class="demo">
  
  <!-- Checkboxes -->
  <div class="form-checkbox">
    <input class="checkbox" type="checkbox" id="checkbox1">
    <label for="checkbox1" tabindex="1">Shawn Carter</label>
  </div>
  <div class="form-checkbox">
    <input class="checkbox" type="checkbox" id="checkbox2" checked="checked">
    <label for="checkbox2" tabindex="2">Beyonce Knowles</label>
  </div>
  <div class="form-checkbox">
    <input class="checkbox" type="checkbox" id="checkbox3" checked="checked" disabled="disabled">
    <label for="checkbox3" tabindex="3">Christopher Wallace</label>
  </div>
  <div class="form-checkbox">
    <input class="checkbox" type="checkbox" id="checkbox4" disabled="disabled">
    <label for="checkbox4" tabindex="4">Tupac Shakur</label>
  </div>
  
</div>

The CSS (Sass)

Create a new .scss file (such as checkboxes.scss) and copy the Sass code into it from below. You can customise this to your requirements, but out of the box it should look like the above example.

// Variables
$checkbox-size: 20px;
$checkbox-border: #cecece;
$checkbox-selected: #02BB72; // Primary colour

.checkbox{
  position: absolute;
  opacity: 0;
  
  // Text
  & + label{
    display: inline-block;
    position: relative;
    cursor: pointer;
    line-height: $checkbox-size;
    padding-left: $checkbox-size + 10;
    font-size: $checkbox-size - 2;
    font-family: sans-serif;
    color: #666; // Label colour
    
    // Checkbox
    &:before{
      position: absolute;
      display: block;
      left: 0;
      top: 0;
      content: "";
      background: transparent;
      width: $checkbox-size;
      height: $checkbox-size;
      
      box-shadow: inset 0 0 0 1px $checkbox-border;
      border-radius: 4px;
      transition: 200ms ease-in-out all;
    }
    
    // Tick
    &:after{
      position: absolute;
      display: block;
      top: 5px;
      left: 8px;
      content: "";
      width: 3px;
      height: 7px;
      border-right: 1px solid transparent;
      border-bottom: 1px solid transparent;
      transform: rotate(45deg);
      
      transition: 200ms ease-in-out all;
    }
  }
  
  // Hover
  & + label:hover{
    color: #333;
  }
  
  // Focus
  & + label:focus{
    outline: none;
  }
  
  // Checked
  &:checked{
    & + label{
      &:before{
        background: $checkbox-selected;
        box-shadow: none;
      }
      &:after{
        border-color: white;
      }
    }
  }
  
  // Disabled
  &:disabled{
    & + label{
      &:before{
        background: #f2f2f2; // Disabled colour
        box-shadow: none;
      }
      &:after{
        border-color: transparent;
      }
    }
  }
  
  // Disabled Checked
  &:checked:disabled{
    & + label{
      &:before{
        background: #f2f2f2;
        box-shadow: none;
      }
      &:after{
        border-color: #cecece;
      }
    }
  }
}

// Additional demo styling
.form-checkbox{
  position: relative;
  display: block;
  margin: 0 0 15px;
}
.demo{
  display: block;
  width: 240px;
  margin: 50px auto;
  padding: 100px;
  border: 1px solid $checkbox-border;
  border-radius: 10px;
}

That’s it! Easy peasy, lemon-squeazy!? If you enjoyed this article, give it a share and feel free to get in touch if you want to suggest improvements.

Thanks,
Andy