The default checkbox supplied by most browsers is not that visually appealing. Although you can increase, or decrease the size, there is very little you can do with it in terms of styling.

So, should you wish to have something a little more stylish, what do you do? Let me show you how to create a replacement checkbox to add that extra bit of flair to your mundane forms.

The answer is to create your own display. What we’ll do in this example is essentially link up a label to the checkbox, hide the checkbox so that we can use it’s functionality, and display a replacement using pseudo elements. This will all be done in pure HTML and CSS.

Animated checkbox basics

The first thing we need to do is create our display, which is very basic.

<input type="checkbox" name="model" id="model" />
<label for="model">
Model
</label>

Hide checkbox

Now we need to hide the actual checkbox. We still need it there because we still want to use it’s functionality. To hide the Checkbox, we add the following styling.

[type="checkbox"] {
display: none;
}

Adding in alt

So if there isn’t a checkbox, how can you tell what state everything is? Well, the answer is you create your own checkbox. The next part of the code will create a basic border for the new checkbox using the first pseudo element, while adding some style to the attached label.

[type="checkbox"]+label {
position: relative;
display: block;
padding-left: 25px;
cursor: pointer;
}

[type="checkbox"]+label:before {
content: '';
position: absolute;
left: 0;
top: 4px;
width: 14px;
height: 14px;
border: 1px solid #0880bc;
background: #transparent;
}

Adding checked status

We’ll use the second pseudo element for the actual checked status. This first code will display the checked status regardless of the checkboxes actual state. We’ll handle that in the second part.

[type="checkbox"]+label:after {
content: '';
position: absolute;
display: block;
top: 8px;
left: 4px;
height: 8px;
width: 8px;
font-size: 18px;
line-height: 0.8;
background: #00adef;
-webkit-transition: all .2s;
transition: all .2s;
}

Alternating states

The final part of the code allows for us the toggle the checked pseudo element’s opacity to indicate the checked state. The two blocks of code are for both the checked and non-checked status.

[type="checkbox"]:not(:checked)+label:after {
opacity: 0;
-webkit-transform: scale(0);
transform: scale(0);
}

[type="checkbox"]:checked+label:after {
opacity: 1;
-webkit-transform: scale(1);
transform: scale(1);
}

Demo

See the Pen Checkbox replacement by Dean Pugh (@wezlar) on CodePen.0

Conclusion

And that is everything you need. You can modify the :before element of the label to be whatever size, shape and colour you require that can be created in CSS. The second :after element can also be modified in the same way, allowing you to create whatever checked status you desire. You can even go beyond that with the animation between each status at your mercy.

Having the checkbox set up this way even makes it easier to create the same identical look between browsers. Something that is much harder to do if you use the browser supplied version.

Tags: , , ,

Join the conversation...

Your email address will not be published. Required fields are marked *