Custom and fancy input type checkbox
Today I’ll introduce a tweak not as much popular as it deserve to be:
CUSTOM CHECKBOXES!
A simple css-only implementation of this element could be achieved with just 4 rules and 2 elements, generating an output like this:
that could be customized as much as you desire and feel creative.
The code is really simple and it consist in:
HTML
which contains a div.relative that could be any other parent of the checkbox, we just don’t care
<div class="relative">
<input type="checkbox"><label>Hello fancy checkbox :)</label>
</div>
CSS
which contains most of the the magic
body{
background: #333;
color: white;
}
.relative{
position: relative;
}
input[type=checkbox]{
position: absolute;
left: 0;
top: 2px;
z-index: 99;
cursor: pointer;
opacity: 0;
margin: 0;
}
input[type=checkbox] + label{
vertical-align: baseline;
display: inline-block;
position: relative;
padding-left: 20px;
}
input[type=checkbox]:checked + label:before{
content: "\2713";
text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
color: #333;
text-align: center;
}
input[type=checkbox] + label:before{
content: "";
display: inline-block;
width: 15px;
height: 15px;
line-height: 15px;
position: absolute;
left: 0;
top: 2px;
z-index: 1;
background-color: white;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
Remember that pseudo :after and :before are CSS3 compatible only. That means that on IE<9 you’ll see your normal checkbox,
but we’re starting caring less and less about IE8 and his parents 🙂
If you want to see a live preview, check out this JSFiddle!
No comments yet.