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 | Step 2 | Step 3 |
Extra | Closing |
STEP 1
THE BASICS
First, let us start with the basic trick behind creating custom checkboxes using just pure CSS.
CHECKBOX WITH LABEL
No rocket science is required behind the HTML part of custom checkboxes – This is just your usual Yep, that is all to the “CSS magic” behind custom checkboxes. 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. Having a custom checkmark is all cool, but that is still very spartan… Let us make it look a little elegant in this step. 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. What the heck happened here? It may look like quite a handful, but this is actually pretty straightforward. To deal with the disabled checkboxes we simply target the For you code ninjas who think that a custom checkbox is not cool enough, here is one last optional step to add animations 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. 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. For you guys who want to create round checkboxes – All you need is to define the 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. 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..
But please make sure that you attach a to the checkbox, and wrap the entire thing in a
THE CSS TRICK
.custom-1 input:checked ~ label:before {
content: "\2605";
}
input:checked
pseudo class to specifically target when the checkbox is checked.label:before
.HIDING THE DEFAULT CHECKBOX
.custom-1 input[type=checkbox] {
display: none;
}
.custom-1 input:checked ~ label:before {
content: "\2605";
}
STEP 2
PRETTY CHECKBOXESTHE HTML
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;
}
.custom-sq
is good font-size
and line-height
, so that we don’t get a “misaligned” checkbox later.label
to show a pointer on mouse hover instead.label:before
.DISABLED BOXES
/* Disabled */
.custom-sq input:disabled ~ label {
color: #888;
}
.custom-sq input:disabled ~ label:before {
background: #ccc;
}
:disabled
pseudo class, and give it a “greyed-out” appearance.STEP 3
ANIMATIONSTHE SPRING EFFECT
.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);
}
}
EXTRA
DOWNLOAD & MOREROUND CHECKBOXES
.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;
}
border-radius
to half the width… Or just leave it the same as the width for simplicity sake.DOWNLOAD
CLOSING
WHAT’S NEXT?