Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

3 Steps Simple Pure CSS Custom Checkbox

INTRODUCTION
THE MINOR ANNOYANCE

Welcome to a tutorial on how to create a simple pure CSS custom checkbox. Yep, the stone age of the Internet is long over, and CSS has advanced very much ever since… But one major bugbear still continues to haunt the developers till today – Creating a custom checkbox.

Yep, there is no “official easy way” to do it, and it requires quite a bit of roundabout work. Still, it actually isn’t really that difficult, and this guide will walk you through an easy “CSS only” solution. Read on!

I have included a zip file with all the source code at the end of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

CONFESSION
AN HONEST DISCLOSURE

Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!

NAVIGATION
TABLE OF CONTENTS

Step 1
The Basics

Step 2
Prettify Checkbox

Step 3
Animations

Extra
Download & More

Closing
What’s Next?

STEP 1
THE BASICS

First, let us start with the basic trick behind creating custom checkboxes using just pure CSS.

CHECKBOX WITH LABEL

1-basics.html

No rocket science is required behind the HTML part of custom checkboxes – This is just your usual . But please make sure that you attach a  to the checkbox, and wrap the entire thing in a 

 wrapper.

THE CSS TRICK

1-basics.css
.custom-1 input:checked ~ label:before {
  content: "\2605";
}

Yep, that is all to the “CSS magic” behind custom checkboxes.

  • We can use the input:checked pseudo class to specifically target when the checkbox is checked.
  • Then we add our own custom HTML symbol checkmark to the label using the pseudo class label:before.
  • Feel free to check out this reference for a comprehensive list of HTML symbols.

HIDING THE DEFAULT CHECKBOX

1-basics.css
.custom-1 input[type=checkbox] {
  display: none;
}
.custom-1 input:checked ~ label:before {
  content: "\2605";
}

With that, we can safely hide the default checkbox, and it will still work as usual – Since clicking on the label itself will still toggle the checkbox.

STEP 2
PRETTY CHECKBOXES

Having a custom checkmark is all cool, but that is still very spartan… Let us make it look a little elegant in this step.

THE HTML

2-pretty.html

Well, there are no changes in the HTML. But just to take note here that we will be creating a “normal custom square checkbox”, and also deal with the style for a disabled checkbox.

PRETTY CSS

2-pretty.css
/* Hide the default checkbox */
.custom-sq input[type=checkbox] {
  display: none;
}

/* Draw our custom check box */
.custom-sq {
  font-size: 16px;
  line-height: 20px;
}
.custom-sq label {
  cursor: pointer;
}
.custom-sq label:before {
  display: inline-block;
  width: 20px;
  margin-right: 5px;
  content: "\00a0";
  text-align: center;
  background: #eee;
}
.custom-sq label:hover::before{
  background: #aaa;
}

/* On check */
.custom-sq input:checked ~ label:before {
  content: "\2605";
  background: #3f6cb5;
  color: #fff;
}

What the heck happened here? It may look like quite a handful, but this is actually pretty straightforward.

  • We hide the default checkbox as usual.
  • We give .custom-sq is good font-size and line-height, so that we don’t get a “misaligned” checkbox later.
  • Set the label to show a pointer on mouse hover instead.
  • Create a nice gray empty box on the same pseudo class label:before.
  • Change the color of the gray empty box on mouse hover.
  • Finally, as with step 1, add a custom HTML symbol when the checkbox is checked.

DISABLED BOXES

2-pretty.css
/* Disabled */
.custom-sq input:disabled ~ label {
  color: #888;
}
.custom-sq input:disabled ~ label:before {
  background: #ccc;
}

To deal with the disabled checkboxes we simply target the :disabled pseudo class, and give it a “greyed-out” appearance.

STEP 3
ANIMATIONS

For you code ninjas who think that a custom checkbox is not cool enough, here is one last optional step to add animations

THE SPRING EFFECT

3-animation.css
.custom-asq input:checked ~ label:before {
  content: "\2605";
  background: #3f6cb5;
  color: #fff;
  animation: spring 1s; /* attach animation */
  animation-timing-function: ease;
}

/* Spring effect */
@keyframes spring { 
  0% {
    transform: scale(1) translateY(0); 
  }
  10% {
    transform: scale(1.2, 0.6); 
  }
  30% { 
    transform: scale(0.8, 1.1) translateY(-20px); 
  }
  50% { 
    transform: scale(1) translateY(0); 
  }
  100% { 
    transform: translateY(0); 
  }
}

Adding an animation to your checkbox is actually pretty easy. This is a bounce effect that I have created earlier, and all you have to do, is to create the keyframes, then attach it to the label. Done.

EXTRA
DOWNLOAD & MORE

That’s it for the custom checkboxes, and here is the download link as promised – Also a small extra that may be useful to you.

ROUND CHECKBOXES

4-circle.css
.custom-cir label:before {
  display: inline-block;
  width: 20px;
  border-radius: 20px; /* [THAT IS ALL YOU NEED!] */
  margin-right: 5px;
  content: "\00a0";
  text-align: center;
  background: #eee;
}

For you guys who want to create round checkboxes – All you need is to define the border-radius to half the width… Or just leave it the same as the width for simplicity sake.

DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

CLOSING
WHAT’S NEXT?

Thank you for reading, and we have come to the end of this short guide. I hope that it has helped you to create a better project, and if you have anything to share with this guide, please feel free to comment below. Good luck and happy coding!


The post 3 Steps Simple Pure Css Custom Checkbox appeared first on Code Boxx.



This post first appeared on Xxxxxxxxx, please read the originial post: here

Share the post

3 Steps Simple Pure CSS Custom Checkbox

×

Subscribe to Xxxxxxxxx

Get updates delivered right to your inbox!

Thank you for your subscription

×