I am trying to style a checkbox using the following:
<input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" />
_x000D_
But the style is not applied. The checkbox still displays its default style. How do I give it the specified style?
Quick fix to add icon in front of text:
< asp:CheckBox... Text="< img src='/link/to/img.png' />My Text" />
You can avoid adding extra markup. This works everywhere except IE for Desktop (but works in IE for Windows Phone and Microsoft Edge) via setting CSS appearance
:
input[type="checkbox"] {_x000D_
-webkit-appearance: none;_x000D_
-moz-appearance: none;_x000D_
appearance: none;_x000D_
_x000D_
/* Styling checkbox */_x000D_
width: 16px;_x000D_
height: 16px;_x000D_
background-color: red;_x000D_
}_x000D_
_x000D_
input[type="checkbox"]:checked {_x000D_
background-color: green;_x000D_
}
_x000D_
<input type="checkbox" />
_x000D_
Here is a simple CSS solution without any jQuery or JavaScript code.
I am using FontAwseome icons but you can use any image
input[type=checkbox] {
display: inline-block;
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
visibility: hidden;
font-size: 14px;
}
input[type=checkbox]:before {
content: @fa-var-square-o;
visibility: visible;
/*font-size: 12px;*/
}
input[type=checkbox]:checked:before {
content: @fa-var-check-square-o;
}
Warning: the following was true at the time of writing, but in the meantime things have progressed.
AFAIK modern browsers display checkboxes using the native OS control, so there's no way to style them.
With pure CSS, nothing fancy with :before
and :after
, no transforms, you can turn off the default appearance and then style it with an inline background image like the following example. This works in Chrome, Firefox, Safari, and now Edge (Chromium Edge).
INPUT[type=checkbox]:focus
{
outline: 1px solid rgba(0, 0, 0, 0.2);
}
INPUT[type=checkbox]
{
background-color: #DDD;
border-radius: 2px;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
width: 17px;
height: 17px;
cursor: pointer;
position: relative;
top: 5px;
}
INPUT[type=checkbox]:checked
{
background-color: #409fd6;
background: #409fd6 url("data:image/gif;base64,R0lGODlhCwAKAIABAP////3cnSH5BAEKAAEALAAAAAALAAoAAAIUjH+AC73WHIsw0UCjglraO20PNhYAOw==") 3px 3px no-repeat;
}
_x000D_
<form>
<label><input type="checkbox"> I Agree To Terms & Conditions</label>
</form>
_x000D_
Since browsers like Edge and Firefox do not support :before :after on checkbox input tags, here is an alternative purely with HTML and CSS. Of course you should edit CSS according to your requirements.
Make the HTML for checkbox like this:
<div class='custom-checkbox'>
<input type='checkbox' />
<label>
<span></span>
Checkbox label
</label>
</div>
Apply this style for the checkbox to change the color label
<style>
.custom-checkbox {
position: relative;
}
.custom-checkbox input{
position: absolute;
left: 0;
top: 0;
height:15px;
width: 50px; /* Expand the checkbox so that it covers */
z-index : 1; /* the label and span, increase z-index to bring it over */
opacity: 0; /* the label and set opacity to 0 to hide it. */
}
.custom-checkbox input+label {
position: relative;
left: 0;
top: 0;
padding-left: 25px;
color: black;
}
.custom-checkbox input+label span {
position: absolute; /* a small box to display as checkbox */
left: 0;
top: 0;
height: 15px;
width: 15px;
border-radius: 2px;
border: 1px solid black;
background-color: white;
}
.custom-checkbox input:checked+label { /* change label color when checked */
color: orange;
}
.custom-checkbox input:checked+label span{ /* change span box color when checked */
background-color: orange;
border: 1px solid orange;
}
</style>
I'd follow the advice of SW4's answer – to hide the checkbox and to cover it with a custom span, suggesting this HTML:
<label>
<input type="checkbox">
<span>send newsletter</span>
</label>
The wrap in label neatly allows clicking the text without the need of "for-id" attribute linking. However,
visibility: hidden
or display: none
It works by clicking or tapping, but that is a lame way to use checkboxes. Some people still use much more effective Tab to move focus, Space to activate, and hiding with that method disables it. If the form is long, one will save someone's wrists to use tabindex
or accesskey
attributes. And if you observe the system checkbox behavior, there is a decent shadow on hover. The well styled checkbox should follow this behavior.
cobberboy's answer recommends Font Awesome which is usually better than bitmap since fonts are scalable vectors. Working with the HTML above, I'd suggest these CSS rules:
Hide checkboxes
input[type="checkbox"] {
position: absolute;
opacity: 0;
z-index: -1;
}
I use just negative z-index
since my example uses big enough checkbox skin to cover it fully. I don't recommend left: -999px
since it is not reusable in every layout. Bushan wagh's answer provides a bulletproof way to hide it and convince the browser to use tabindex, so it is a good alternative. Anyway, both is just a hack. The proper way today is appearance: none
, see Joost's answer:
input[type="checkbox"] {
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
}
Style checkbox label
input[type="checkbox"] + span {
font: 16pt sans-serif;
color: #000;
}
Add checkbox skin
input[type="checkbox"] + span:before {
font: 16pt FontAwesome;
content: '\00f096';
display: inline-block;
width: 16pt;
padding: 2px 0 0 3px;
margin-right: 0.5em;
}
\00f096
is Font Awesome's square-o
, padding is adjusted to provide even dotted outline on focus (see below).
Add checkbox checked skin
input[type="checkbox"]:checked + span:before {
content: '\00f046';
}
\00f046
is Font Awesome's check-square-o
, which is not the same width as square-o
, which is the reason for the width style above.
Add focus outline
input[type="checkbox"]:focus + span:before {
outline: 1px dotted #aaa;
}
Safari doesn't provide this feature (see @Jason Sankey's comment), see this answer for Safari-only CSS
Set gray color for disabled checkbox
input[type="checkbox"]:disabled + span {
color: #999;
}
Set hover shadow on non-disabled checkbox
input[type="checkbox"]:not(:disabled) + span:hover:before {
text-shadow: 0 1px 2px #77F;
}
Try to hover the mouse over the checkboxes and use Tab and Shift+Tab to move and Space to toggle.
This is simplest way and you can choose which checkboxes to give this style.
CSS:
.check-box input {
display: none;
}
.check-box span:before {
content: ' ';
width: 20px;
height: 20px;
display: inline-block;
background: url("unchecked.png");
}
.check-box input:checked + span:before {
background: url("checked.png");
}
HTML:
<label class="check-box">
<input type="checkbox">
<span>Check box Text</span>
</label>
For those using SCSS (or easily converted to SASS), the following will be helpful. Effectively, make an element next to the checkbox, which is the one that you will style. When the checkbox is clicked, the CSS restyles the sister element (to your new, checked style). Code is below:
label.checkbox {_x000D_
input[type="checkbox"] {_x000D_
visibility: hidden;_x000D_
display: block;_x000D_
height: 0;_x000D_
width: 0;_x000D_
position: absolute;_x000D_
overflow: hidden;_x000D_
_x000D_
&:checked + span {_x000D_
background: $accent;_x000D_
}_x000D_
}_x000D_
_x000D_
span {_x000D_
cursor: pointer;_x000D_
height: 15px;_x000D_
width: 15px;_x000D_
border: 1px solid $accent;_x000D_
border-radius: 2px;_x000D_
display: inline-block;_x000D_
transition: all 0.2s $interpol;_x000D_
}_x000D_
}
_x000D_
<label class="checkbox">_x000D_
<input type="checkbox" />_x000D_
<span></span>_x000D_
Label text_x000D_
</label>
_x000D_
**Custom checkbox with css** (WebKit browser solution only Chrome, Safari, Mobile browsers)
<input type="checkbox" id="cardAccptance" name="cardAccptance" value="Yes">
<label for="cardAccptance" class="bold"> Save Card for Future Use</label>
/* The checkbox-cu */
.checkbox-cu {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 0;
cursor: pointer;
font-size: 16px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default checkbox-cu */
.checkbox-cu input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox-cu */
.checkmark {
position: absolute;
top: 4px;
left: 0;
height: 20px;
width: 20px;
background-color: #eee;
border: 1px solid #999;
border-radius: 0;
box-shadow: none;
}
/* On mouse-over, add a grey background color */
.checkbox-cu:hover input~.checkmark {
background-color: #ccc;
}
/* When the checkbox-cu is checked, add a blue background */
.checkbox-cu input:checked~.checkmark {
background-color: transparent;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.checkbox-cu input:checked~.checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.checkbox-cu .checkmark::after {
left: 7px;
top: 3px;
width: 6px;
height: 9px;
border: solid #28a745;
border-width: 0 2px 2px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
z-index: 100;
}
After a lot of search and testing I got this solution which is simple to implement and easier to customize. In this solution:
Simple put the flowing CSS at the top of your page and all checkboxes style will change like this:
input[type=checkbox] {
transform: scale(1.5);
}
input[type=checkbox] {
width: 30px;
height: 30px;
margin-right: 8px;
cursor: pointer;
font-size: 17px;
visibility: hidden;
}
input[type=checkbox]:after {
content: " ";
background-color: #fff;
display: inline-block;
margin-left: 10px;
padding-bottom: 5px;
color: #00BFF0;
width: 22px;
height: 25px;
visibility: visible;
border: 1px solid #00BFF0;
padding-left: 3px;
border-radius: 5px;
}
input[type=checkbox]:checked:after {
content: "\2714";
padding: -5px;
font-weight: bold;
}
_x000D_
<input type="checkbox" id="checkbox1" />
<label for="checkbox1">Checkbox</label>
_x000D_
Recently I found a quite interesting solution to the problem.
You could use appearance: none;
to turn off the checkbox's default style and then write your own over it like described here (Example 4).
input[type=checkbox] {_x000D_
width: 23px;_x000D_
height: 23px;_x000D_
-webkit-appearance: none;_x000D_
-moz-appearance: none;_x000D_
appearance: none;_x000D_
margin-right: 10px;_x000D_
background-color: #878787;_x000D_
outline: 0;_x000D_
border: 0;_x000D_
display: inline-block;_x000D_
-webkit-box-shadow: none !important;_x000D_
-moz-box-shadow: none !important;_x000D_
box-shadow: none !important;_x000D_
}_x000D_
_x000D_
input[type=checkbox]:focus {_x000D_
outline: none;_x000D_
border: none !important;_x000D_
-webkit-box-shadow: none !important;_x000D_
-moz-box-shadow: none !important;_x000D_
box-shadow: none !important;_x000D_
}_x000D_
_x000D_
input[type=checkbox]:checked {_x000D_
background-color: green;_x000D_
text-align: center;_x000D_
line-height: 15px;_x000D_
}
_x000D_
<input type="checkbox">
_x000D_
Unfortunately browser support is quite bad for the appearance
option. From my personal testing I only got Opera and Chrome working correctly. But this would be the way to go to keep it simple when better support comes or you only want to use Chrome/Opera.
You can simply use appearance: none
on modern browsers, so that there is no default styling and all your styles are applied properly:
input[type=checkbox] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
display: inline-block;
width: 2em;
height: 2em;
border: 1px solid gray;
outline: none;
vertical-align: middle;
}
input[type=checkbox]:checked {
background-color: blue;
}
From my googling, this is the easiest way for checkbox styling. Just add :after
and :checked:after
CSS based on your design.
body{_x000D_
background: #DDD;_x000D_
}_x000D_
span{_x000D_
margin-left: 30px;_x000D_
}_x000D_
input[type=checkbox] {_x000D_
cursor: pointer;_x000D_
font-size: 17px;_x000D_
visibility: hidden;_x000D_
position: absolute;_x000D_
top: 0;_x000D_
left: 0;_x000D_
transform: scale(1.5);_x000D_
}_x000D_
_x000D_
input[type=checkbox]:after {_x000D_
content: " ";_x000D_
background-color: #fff;_x000D_
display: inline-block;_x000D_
color: #00BFF0;_x000D_
width: 14px;_x000D_
height: 19px;_x000D_
visibility: visible;_x000D_
border: 1px solid #FFF;_x000D_
padding: 0 3px;_x000D_
margin: 2px 0;_x000D_
border-radius: 8px;_x000D_
box-shadow: 0 0 15px 0 rgba(0,0,0,0.08), 0 0 2px 0 rgba(0,0,0,0.16);_x000D_
}_x000D_
_x000D_
input[type=checkbox]:checked:after {_x000D_
content: "\2714";_x000D_
display: unset;_x000D_
font-weight: bold;_x000D_
}
_x000D_
<input type="checkbox"> <span>Select Text</span>
_x000D_
I have been scrolling and scrolling and tons of these answers simply throw accessibility out the door and violate WCAG in more than one way. I threw in radio buttons since most of the time when you're using custom checkboxes you want custom radio buttons too.
Fiddles:
Late to the party but somehow this is still difficult in 2019, 2020, 2021 so I have added my three solutions which are accessible and easy to drop in.
These are all JavaScript free, accessible, and external library free*...
If you want to plug-n-play with any of these just copy the style sheet from the fiddles, edit the color codes in the CSS to fit your needs, and be on your way. You can add a custom svg checkmark icon if you want for the checkboxes. I've added lots of comments for those non-CSS'y folks.
If you have long text or a small container and are encountering text wrapping underneath the checkbox or radio button input then just convert to divs like this.
Longer explanation:
I needed a solution that does not violate WCAG, doesn't rely on JavaScript or external libraries, and that does not break keyboard navigation like tabbing or spacebar to select, that allows focus events, a solution that allows for disabled checkboxes that are both checked and unchecked, and finally a solution where I can customize the look of the checkbox however I want with different background-color
's, border-radius
, svg
backgrounds, etc.
I used some combination of this answer from @Jan Turon to come up with my own solution which seems to work quite well. I've done a radio button fiddle that uses a lot of the same code from the checkboxes in order to make this work with radio buttons too.
I am still learning accessibility so if I missed something please drop a comment and I will try to correct it here is a code example of my checkboxes:
input[type="checkbox"] {
position: absolute;
opacity: 0;
z-index: -1;
}
/* Text color for the label */
input[type="checkbox"]+span {
cursor: pointer;
font: 16px sans-serif;
color: black;
}
/* Checkbox un-checked style */
input[type="checkbox"]+span:before {
content: '';
border: 1px solid grey;
border-radius: 3px;
display: inline-block;
width: 16px;
height: 16px;
margin-right: 0.5em;
margin-top: 0.5em;
vertical-align: -2px;
}
/* Checked checkbox style (in this case the background is green #e7ffba, change this to change the color) */
input[type="checkbox"]:checked+span:before {
/* NOTE: Replace the url with a path to an SVG of a checkmark to get a checkmark icon */
background-image: url('https://cdnjs.cloudflare.com/ajax/libs/ionicons/4.5.6/collection/build/ionicons/svg/ios-checkmark.svg');
background-repeat: no-repeat;
background-position: center;
/* The size of the checkmark icon, you may/may not need this */
background-size: 25px;
border-radius: 2px;
background-color: #e7ffba;
color: white;
}
/* Adding a dotted border around the active tabbed-into checkbox */
input[type="checkbox"]:focus+span:before,
input[type="checkbox"]:not(:disabled)+span:hover:before {
/* Visible in the full-color space */
box-shadow: 0px 0px 0px 2px rgba(0, 150, 255, 1);
/* Visible in Windows high-contrast themes
box-shadow will be hidden in these modes and
transparency will not be hidden in high-contrast
thus box-shadow will not show but the outline will
providing accessibility */
outline-color: transparent; /*switch to transparent*/
outline-width: 2px;
outline-style: dotted;
}
/* Disabled checkbox styles */
input[type="checkbox"]:disabled+span {
cursor: default;
color: black;
opacity: 0.5;
}
/* Styles specific to this fiddle that you do not need */
body {
padding: 1em;
}
h1 {
font-size: 18px;
}
_x000D_
<h1>
NOTE: Replace the url for the background-image in CSS with a path to an SVG in your solution or CDN. This one was found from a quick google search for a checkmark icon cdn
</h1>
<p>You can easily change the background color, checkbox symbol, border-radius, etc.</p>
<label>
<input type="checkbox">
<span>Try using tab and space</span>
</label>
<br>
<label>
<input type="checkbox" checked disabled>
<span>Disabled Checked Checkbox</span>
</label>
<br>
<label>
<input type="checkbox" disabled>
<span>Disabled Checkbox</span>
</label>
<br>
<label>
<input type="checkbox">
<span>Normal Checkbox</span>
</label>
<br>
<label>
<input type="checkbox">
<span>Another Normal Checkbox</span>
</label>
_x000D_
You can achieve quite a cool custom checkbox effect by using the new abilities that come with the :after
and :before
pseudo classes. The advantage to this, is: You don't need to add anything more to the DOM, just the standard checkbox.
Note this will only work for compatible browsers. I believe this is related to the fact that some browsers do not allow you to set :after
and :before
on input elements. Which unfortunately means for the moment only WebKit browsers are supported. Firefox + Internet Explorer will still allow the checkboxes to function, just unstyled, and this will hopefully change in the future (the code does not use vendor prefixes).
This is a WebKit browser solution only (Chrome, Safari, Mobile browsers)
$(function() {_x000D_
$('input').change(function() {_x000D_
$('div').html(Math.random());_x000D_
});_x000D_
});
_x000D_
/* Main Classes */_x000D_
.myinput[type="checkbox"]:before {_x000D_
position: relative;_x000D_
display: block;_x000D_
width: 11px;_x000D_
height: 11px;_x000D_
border: 1px solid #808080;_x000D_
content: "";_x000D_
background: #FFF;_x000D_
}_x000D_
_x000D_
.myinput[type="checkbox"]:after {_x000D_
position: relative;_x000D_
display: block;_x000D_
left: 2px;_x000D_
top: -11px;_x000D_
width: 7px;_x000D_
height: 7px;_x000D_
border-width: 1px;_x000D_
border-style: solid;_x000D_
border-color: #B3B3B3 #dcddde #dcddde #B3B3B3;_x000D_
content: "";_x000D_
background-image: linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);_x000D_
background-repeat: no-repeat;_x000D_
background-position: center;_x000D_
}_x000D_
_x000D_
.myinput[type="checkbox"]:checked:after {_x000D_
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAQAAABuW59YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAB2SURBVHjaAGkAlv8A3QDyAP0A/QD+Dam3W+kCAAD8APYAAgTVZaZCGwwA5wr0AvcA+Dh+7UX/x24AqK3Wg/8nt6w4/5q71wAAVP9g/7rTXf9n/+9N+AAAtpJa/zf/S//DhP8H/wAA4gzWj2P4lsf0JP0A/wADAHB0Ngka6UmKAAAAAElFTkSuQmCC'), linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);_x000D_
}_x000D_
_x000D_
.myinput[type="checkbox"]:disabled:after {_x000D_
-webkit-filter: opacity(0.4);_x000D_
}_x000D_
_x000D_
.myinput[type="checkbox"]:not(:disabled):checked:hover:after {_x000D_
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAQAAABuW59YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAB2SURBVHjaAGkAlv8A3QDyAP0A/QD+Dam3W+kCAAD8APYAAgTVZaZCGwwA5wr0AvcA+Dh+7UX/x24AqK3Wg/8nt6w4/5q71wAAVP9g/7rTXf9n/+9N+AAAtpJa/zf/S//DhP8H/wAA4gzWj2P4lsf0JP0A/wADAHB0Ngka6UmKAAAAAElFTkSuQmCC'), linear-gradient(135deg, #8BB0C2 0%, #FFF 100%);_x000D_
}_x000D_
_x000D_
.myinput[type="checkbox"]:not(:disabled):hover:after {_x000D_
background-image: linear-gradient(135deg, #8BB0C2 0%, #FFF 100%);_x000D_
border-color: #85A9BB #92C2DA #92C2DA #85A9BB;_x000D_
}_x000D_
_x000D_
.myinput[type="checkbox"]:not(:disabled):hover:before {_x000D_
border-color: #3D7591;_x000D_
}_x000D_
_x000D_
/* Large checkboxes */_x000D_
.myinput.large {_x000D_
height: 22px;_x000D_
width: 22px;_x000D_
}_x000D_
_x000D_
.myinput.large[type="checkbox"]:before {_x000D_
width: 20px;_x000D_
height: 20px;_x000D_
}_x000D_
_x000D_
.myinput.large[type="checkbox"]:after {_x000D_
top: -20px;_x000D_
width: 16px;_x000D_
height: 16px;_x000D_
}_x000D_
_x000D_
/* Custom checkbox */_x000D_
.myinput.large.custom[type="checkbox"]:checked:after {_x000D_
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGHRFWHRBdXRob3IAbWluZWNyYWZ0aW5mby5jb23fZidLAAAAk0lEQVQ4y2P4//8/AyUYwcAD+OzN/oMwshjRBoA0Gr8+DcbIhhBlAEyz+qZZ/7WPryHNAGTNMOxpJvo/w0/uP0kGgGwGaZbrKgfTGnLc/0nyAgiDbEY2BCRGdCDCnA2yGeYVog0Aae5MV4c7Gzk6CRqAbDM2w/EaQEgzXgPQnU2SAcTYjNMAYm3GaQCxNuM0gFwMAPUKd8XyBVDcAAAAAElFTkSuQmCC'), linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);_x000D_
}_x000D_
_x000D_
.myinput.large.custom[type="checkbox"]:not(:disabled):checked:hover:after {_x000D_
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGHRFWHRBdXRob3IAbWluZWNyYWZ0aW5mby5jb23fZidLAAAAk0lEQVQ4y2P4//8/AyUYwcAD+OzN/oMwshjRBoA0Gr8+DcbIhhBlAEyz+qZZ/7WPryHNAGTNMOxpJvo/w0/uP0kGgGwGaZbrKgfTGnLc/0nyAgiDbEY2BCRGdCDCnA2yGeYVog0Aae5MV4c7Gzk6CRqAbDM2w/EaQEgzXgPQnU2SAcTYjNMAYm3GaQCxNuM0gFwMAPUKd8XyBVDcAAAAAElFTkSuQmCC'), linear-gradient(135deg, #8BB0C2 0%, #FFF 100%);_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
_x000D_
<table style="width:100%">_x000D_
<tr>_x000D_
<td>Normal:</td>_x000D_
<td><input type="checkbox" /></td>_x000D_
<td><input type="checkbox" checked="checked" /></td>_x000D_
<td><input type="checkbox" disabled="disabled" /></td>_x000D_
<td><input type="checkbox" disabled="disabled" checked="checked" /></td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Small:</td>_x000D_
<td><input type="checkbox" class="myinput" /></td>_x000D_
<td><input type="checkbox" checked="checked" class="myinput" /></td>_x000D_
<td><input type="checkbox" disabled="disabled" class="myinput" /></td>_x000D_
<td><input type="checkbox" disabled="disabled" checked="checked" class="myinput" /></td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Large:</td>_x000D_
<td><input type="checkbox" class="myinput large" /></td>_x000D_
<td><input type="checkbox" checked="checked" class="myinput large" /></td>_x000D_
<td><input type="checkbox" disabled="disabled" class="myinput large" /></td>_x000D_
<td><input type="checkbox" disabled="disabled" checked="checked" class="myinput large" /></td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Custom icon:</td>_x000D_
<td><input type="checkbox" class="myinput large custom" /></td>_x000D_
<td><input type="checkbox" checked="checked" class="myinput large custom" /></td>_x000D_
<td><input type="checkbox" disabled="disabled" class="myinput large custom" /></td>_x000D_
<td><input type="checkbox" disabled="disabled" checked="checked" class="myinput large custom" /></td>_x000D_
</tr>_x000D_
</table>
_x000D_
Bonus Webkit style flipswitch fiddle
$(function() {_x000D_
var f = function() {_x000D_
$(this).next().text($(this).is(':checked') ? ':checked' : ':not(:checked)');_x000D_
};_x000D_
$('input').change(f).trigger('change');_x000D_
});
_x000D_
body {_x000D_
font-family: arial;_x000D_
}_x000D_
_x000D_
.flipswitch {_x000D_
position: relative;_x000D_
background: white;_x000D_
width: 120px;_x000D_
height: 40px;_x000D_
-webkit-appearance: initial;_x000D_
border-radius: 3px;_x000D_
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);_x000D_
outline: none;_x000D_
font-size: 14px;_x000D_
font-family: Trebuchet, Arial, sans-serif;_x000D_
font-weight: bold;_x000D_
cursor: pointer;_x000D_
border: 1px solid #ddd;_x000D_
}_x000D_
_x000D_
.flipswitch:after {_x000D_
position: absolute;_x000D_
top: 5%;_x000D_
display: block;_x000D_
line-height: 32px;_x000D_
width: 45%;_x000D_
height: 90%;_x000D_
background: #fff;_x000D_
box-sizing: border-box;_x000D_
text-align: center;_x000D_
transition: all 0.3s ease-in 0s;_x000D_
color: black;_x000D_
border: #888 1px solid;_x000D_
border-radius: 3px;_x000D_
}_x000D_
_x000D_
.flipswitch:after {_x000D_
left: 2%;_x000D_
content: "OFF";_x000D_
}_x000D_
_x000D_
.flipswitch:checked:after {_x000D_
left: 53%;_x000D_
content: "ON";_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>_x000D_
_x000D_
<h2>Webkit friendly mobile-style checkbox/flipswitch</h2>_x000D_
<input type="checkbox" class="flipswitch" /> _x000D_
<span></span>_x000D_
<br>_x000D_
<input type="checkbox" checked="checked" class="flipswitch" /> _x000D_
<span></span>
_x000D_
input[type=checkbox].css-checkbox {
position: absolute;
overflow: hidden;
clip: rect(0 0 0 0);
height: 1px;
width: 1px;
margin: -1px;
padding: 0;
border: 0;
}
input[type=checkbox].css-checkbox + label.css-label {
padding-left: 20px;
height: 15px;
display: inline-block;
line-height: 15px;
background-repeat: no-repeat;
background-position: 0 0;
font-size: 15px;
vertical-align: middle;
cursor: pointer;
}
input[type=checkbox].css-checkbox:checked + label.css-label {
background-position: 0 -15px;
}
.css-label{
background-image:url(http://csscheckbox.com/checkboxes/dark-check-green.png);
}
There is a way to do this using just CSS. We can (ab)use the label
element and style that element instead. The caveat is that this will not work for Internet Explorer 8 and lower versions.
.myCheckbox input {_x000D_
position: relative;_x000D_
z-index: -9999;_x000D_
}_x000D_
_x000D_
.myCheckbox span {_x000D_
width: 20px;_x000D_
height: 20px;_x000D_
display: block;_x000D_
background: url("link_to_image");_x000D_
}_x000D_
_x000D_
.myCheckbox input:checked + span {_x000D_
background: url("link_to_another_image");_x000D_
}
_x000D_
<label for="test">Label for my styled "checkbox"</label>_x000D_
<label class="myCheckbox">_x000D_
<input type="checkbox" name="test" />_x000D_
<span></span>_x000D_
</label>
_x000D_
input[type="checkbox"] {_x000D_
cursor: pointer;_x000D_
-webkit-appearance: none;_x000D_
-moz-appearance: none;_x000D_
appearance: none;_x000D_
outline: 0;_x000D_
background: lightgray;_x000D_
height: 16px;_x000D_
width: 16px;_x000D_
border: 1px solid white;_x000D_
}_x000D_
_x000D_
input[type="checkbox"]:checked {_x000D_
background: #2aa1c0;_x000D_
}_x000D_
_x000D_
input[type="checkbox"]:hover {_x000D_
filter: brightness(90%);_x000D_
}_x000D_
_x000D_
input[type="checkbox"]:disabled {_x000D_
background: #e6e6e6;_x000D_
opacity: 0.6;_x000D_
pointer-events: none;_x000D_
}_x000D_
_x000D_
input[type="checkbox"]:after {_x000D_
content: '';_x000D_
position: relative;_x000D_
left: 40%;_x000D_
top: 20%;_x000D_
width: 15%;_x000D_
height: 40%;_x000D_
border: solid #fff;_x000D_
border-width: 0 2px 2px 0;_x000D_
transform: rotate(45deg);_x000D_
display: none;_x000D_
}_x000D_
_x000D_
input[type="checkbox"]:checked:after {_x000D_
display: block;_x000D_
}_x000D_
_x000D_
input[type="checkbox"]:disabled:after {_x000D_
border-color: #7b7b7b;_x000D_
}
_x000D_
<input type="checkbox"><br>_x000D_
<input type="checkbox" checked><br>_x000D_
<input type="checkbox" disabled><br>_x000D_
<input type="checkbox" disabled checked><br>
_x000D_
Here is an example, with theme support. It is a modern approach with CSS transitions. There is absolutely no JavaScript required.
I derived the following code linked in this comment; on a related question.
Edit: I added radio buttons, as maxshuty suggests.
const selector = '.grid-container > .grid-row > .grid-col[data-theme="retro"]';_x000D_
const main = () => {_x000D_
new CheckboxStyler().run(selector);_x000D_
new RadioStyler().run(selector);_x000D_
};_x000D_
_x000D_
/*_x000D_
* This is only used to add the wrapper class and checkmark span to an existing DOM,_x000D_
* to make this CSS work._x000D_
*/_x000D_
class AbstractInputStyler {_x000D_
constructor(options) {_x000D_
this.opts = options;_x000D_
}_x000D_
run(parentSelector) {_x000D_
let wrapperClass = this.opts.wrapperClass;_x000D_
let parent = document.querySelector(parentSelector) || document.getElementsByTagName('body')[0];_x000D_
let labels = parent.querySelectorAll('label');_x000D_
if (labels.length) labels.forEach(label => {_x000D_
if (label.querySelector(`input[type="${this.opts._inputType}"]`)) {_x000D_
if (!label.classList.contains(wrapperClass)) {_x000D_
label.classList.add(wrapperClass);_x000D_
label.appendChild(this.__createDefaultNode());_x000D_
}_x000D_
}_x000D_
});_x000D_
return this;_x000D_
}_x000D_
/* @protected */_x000D_
__createDefaultNode() {_x000D_
let checkmark = document.createElement('span');_x000D_
checkmark.className = this.opts._activeClass;_x000D_
return checkmark;_x000D_
}_x000D_
}_x000D_
_x000D_
class CheckboxStyler extends AbstractInputStyler {_x000D_
constructor(options) {_x000D_
super(Object.assign({_x000D_
_inputType: 'checkbox',_x000D_
_activeClass: 'checkmark'_x000D_
}, CheckboxStyler.defaultOptions, options));_x000D_
}_x000D_
}_x000D_
CheckboxStyler.defaultOptions = {_x000D_
wrapperClass: 'checkbox-wrapper'_x000D_
};_x000D_
_x000D_
class RadioStyler extends AbstractInputStyler {_x000D_
constructor(options) {_x000D_
super(Object.assign({_x000D_
_inputType: 'radio',_x000D_
_activeClass: 'pip'_x000D_
}, RadioStyler.defaultOptions, options));_x000D_
}_x000D_
}_x000D_
RadioStyler.defaultOptions = {_x000D_
wrapperClass: 'radio-wrapper'_x000D_
};_x000D_
_x000D_
main();
_x000D_
/* Theming */_x000D_
_x000D_
:root {_x000D_
--background-color: #FFF;_x000D_
--font-color: #000;_x000D_
--checkbox-default-background: #EEE;_x000D_
--checkbox-hover-background: #CCC;_x000D_
--checkbox-disabled-background: #AAA;_x000D_
--checkbox-selected-background: #1A74BA;_x000D_
--checkbox-selected-disabled-background: #6694B7;_x000D_
--checkbox-checkmark-color: #FFF;_x000D_
--checkbox-checkmark-disabled-color: #DDD;_x000D_
}_x000D_
_x000D_
[data-theme="dark"] {_x000D_
--background-color: #111;_x000D_
--font-color: #EEE;_x000D_
--checkbox-default-background: #222;_x000D_
--checkbox-hover-background: #444;_x000D_
--checkbox-disabled-background: #555;_x000D_
--checkbox-selected-background: #2196F3;_x000D_
--checkbox-selected-disabled-background: #125487;_x000D_
--checkbox-checkmark-color: #EEE;_x000D_
--checkbox-checkmark-disabled-color: #999;_x000D_
}_x000D_
_x000D_
[data-theme="retro"] {_x000D_
--background-color: #FFA;_x000D_
--font-color: #000;_x000D_
--checkbox-default-background: #EEE;_x000D_
--checkbox-hover-background: #FFF;_x000D_
--checkbox-disabled-background: #BBB;_x000D_
--checkbox-selected-background: #EEE;_x000D_
--checkbox-selected-disabled-background: #BBB;_x000D_
--checkbox-checkmark-color: #F44;_x000D_
--checkbox-checkmark-disabled-color: #D00;_x000D_
}_x000D_
_x000D_
_x000D_
/* General styles */_x000D_
_x000D_
html {_x000D_
width: 100%;_x000D_
height: 100%;_x000D_
}_x000D_
_x000D_
body {_x000D_
/*background: var(--background-color); -- For demo, moved to column. */_x000D_
/*color: var(--font-color); -- For demo, moved to column. */_x000D_
background: #777;_x000D_
width: calc(100% - 0.5em);_x000D_
height: calc(100% - 0.5em);_x000D_
padding: 0.25em;_x000D_
}_x000D_
_x000D_
h1 {_x000D_
font-size: 1.33em !important;_x000D_
}_x000D_
_x000D_
h2 {_x000D_
font-size: 1.15em !important;_x000D_
margin-top: 1em;_x000D_
}_x000D_
_x000D_
_x000D_
/* Grid style - using flex */_x000D_
_x000D_
.grid-container {_x000D_
display: flex;_x000D_
height: 100%;_x000D_
flex-direction: column;_x000D_
flex: 1;_x000D_
}_x000D_
_x000D_
.grid-row {_x000D_
display: flex;_x000D_
flex-direction: row;_x000D_
flex: 1;_x000D_
margin: 0.25em 0;_x000D_
}_x000D_
_x000D_
.grid-col {_x000D_
display: flex;_x000D_
flex-direction: column;_x000D_
height: 100%;_x000D_
padding: 0 1em;_x000D_
flex: 1;_x000D_
margin: 0 0.25em;_x000D_
/* If not demo, remove and uncomment the body color rules */_x000D_
background: var(--background-color);_x000D_
color: var(--font-color);_x000D_
}_x000D_
_x000D_
.grid-cell {_x000D_
width: 100%;_x000D_
height: 100%;_x000D_
}_x000D_
_x000D_
_x000D_
/* The checkbox wrapper */_x000D_
_x000D_
.checkbox-wrapper,_x000D_
.radio-wrapper {_x000D_
display: block;_x000D_
position: relative;_x000D_
padding-left: 1.5em;_x000D_
margin-bottom: 0.5em;_x000D_
cursor: pointer;_x000D_
-webkit-user-select: none;_x000D_
-moz-user-select: none;_x000D_
-ms-user-select: none;_x000D_
user-select: none;_x000D_
}_x000D_
_x000D_
_x000D_
/* Hide the browser's default checkbox and radio buttons */_x000D_
_x000D_
.checkbox-wrapper input[type="checkbox"] {_x000D_
position: absolute;_x000D_
opacity: 0;_x000D_
cursor: pointer;_x000D_
height: 0;_x000D_
width: 0;_x000D_
}_x000D_
_x000D_
.radio-wrapper input[type="radio"] {_x000D_
position: absolute;_x000D_
opacity: 0;_x000D_
cursor: pointer;_x000D_
height: 0;_x000D_
width: 0;_x000D_
}_x000D_
_x000D_
_x000D_
/* Create a custom checkbox */_x000D_
_x000D_
.checkbox-wrapper .checkmark {_x000D_
position: absolute;_x000D_
top: 0;_x000D_
left: 0;_x000D_
height: 1em;_x000D_
width: 1em;_x000D_
background-color: var(--checkbox-default-background);_x000D_
transition: all 0.2s ease-in;_x000D_
}_x000D_
_x000D_
.radio-wrapper .pip {_x000D_
position: absolute;_x000D_
top: 0;_x000D_
left: 0;_x000D_
height: 1em;_x000D_
width: 1em;_x000D_
border-radius: 50%;_x000D_
background-color: var(--checkbox-default-background);_x000D_
transition: all 0.2s ease-in;_x000D_
}_x000D_
_x000D_
_x000D_
/* Disabled style */_x000D_
_x000D_
.checkbox-wrapper input[type="checkbox"]:disabled~.checkmark,_x000D_
.radio-wrapper input[type="radio"]:disabled~.pip {_x000D_
cursor: not-allowed;_x000D_
background-color: var(--checkbox-disabled-background);_x000D_
color: var(--checkbox-checkmark-disabled-color);_x000D_
}_x000D_
_x000D_
.checkbox-wrapper input[type="checkbox"]:disabled~.checkmark:after,_x000D_
.radio-wrapper input[type="radio"]:disabled~.pip:after {_x000D_
color: var(--checkbox-checkmark-disabled-color);_x000D_
}_x000D_
_x000D_
.checkbox-wrapper input[type="checkbox"]:disabled:checked~.checkmark,_x000D_
.radio-wrapper input[type="radio"]:disabled:checked~.pip {_x000D_
background-color: var(--checkbox-selected-disabled-background);_x000D_
}_x000D_
_x000D_
_x000D_
/* On mouse-over, add a grey background color */_x000D_
_x000D_
.checkbox-wrapper:hover input[type="checkbox"]:not(:disabled):not(:checked)~.checkmark,_x000D_
.radio-wrapper:hover input[type="radio"]:not(:disabled):not(:checked)~.pip {_x000D_
background-color: var(--checkbox-hover-background);_x000D_
}_x000D_
_x000D_
_x000D_
/* When the checkbox is checked, add a blue background */_x000D_
_x000D_
.checkbox-wrapper input[type="checkbox"]:checked~.checkmark,_x000D_
.radio-wrapper input[type="radio"]:checked~.pip {_x000D_
background-color: var(--checkbox-selected-background);_x000D_
}_x000D_
_x000D_
_x000D_
/* Create the checkmark/indicator (hidden when not checked) */_x000D_
_x000D_
.checkbox-wrapper .checkmark:after {_x000D_
display: none;_x000D_
width: 100%;_x000D_
position: absolute;_x000D_
text-align: center;_x000D_
content: "\2713";_x000D_
color: var(--checkbox-checkmark-color);_x000D_
line-height: 1.1em;_x000D_
}_x000D_
_x000D_
.radio-wrapper .pip:after {_x000D_
display: none;_x000D_
width: 100%;_x000D_
position: absolute;_x000D_
text-align: center;_x000D_
content: "\2022";_x000D_
color: var(--checkbox-checkmark-color);_x000D_
font-size: 1.5em;_x000D_
top: -0.2em;_x000D_
}_x000D_
_x000D_
_x000D_
/* Show the checkmark when checked */_x000D_
_x000D_
.checkbox-wrapper input[type="checkbox"]:checked~.checkmark:after {_x000D_
display: block;_x000D_
line-height: 1.1em;_x000D_
}_x000D_
_x000D_
.radio-wrapper input[type="radio"]:checked~.pip:after {_x000D_
display: block;_x000D_
line-height: 1.1em;_x000D_
}
_x000D_
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet" />_x000D_
<div class="grid-container">_x000D_
<div class="grid-row">_x000D_
<div class="grid-col">_x000D_
<div class="grid-cell">_x000D_
<h1>Pure CSS</h1>_x000D_
<h2>Checkbox</h2>_x000D_
<label class="checkbox-wrapper">One_x000D_
<input type="checkbox" checked="checked">_x000D_
<span class="checkmark"></span>_x000D_
</label>_x000D_
<label class="checkbox-wrapper">Two_x000D_
<input type="checkbox">_x000D_
<span class="checkmark"></span>_x000D_
</label>_x000D_
<label class="checkbox-wrapper">Three_x000D_
<input type="checkbox" checked disabled>_x000D_
<span class="checkmark"></span>_x000D_
</label>_x000D_
<label class="checkbox-wrapper">Four_x000D_
<input type="checkbox" disabled>_x000D_
<span class="checkmark"></span>_x000D_
</label>_x000D_
<h2>Radio</h2>_x000D_
<label class="radio-wrapper">One_x000D_
<input type="radio" name="group-x">_x000D_
<span class="pip"></span>_x000D_
</label>_x000D_
<label class="radio-wrapper">Two_x000D_
<input type="radio" name="group-x">_x000D_
<span class="pip"></span>_x000D_
</label>_x000D_
<label class="radio-wrapper">Three_x000D_
<input type="radio" name="group-x" checked disabled>_x000D_
<span class="pip"></span>_x000D_
</label>_x000D_
<label class="radio-wrapper">Four_x000D_
<input type="radio" name="group-x" disabled>_x000D_
<span class="pip"></span>_x000D_
</label>_x000D_
</div>_x000D_
</div>_x000D_
<div class="grid-col" data-theme="dark">_x000D_
<div class="grid-cell">_x000D_
<h1>Pure CSS</h1>_x000D_
<h2>Checkbox</h2>_x000D_
<label class="checkbox-wrapper">One_x000D_
<input type="checkbox" checked="checked">_x000D_
<span class="checkmark"></span>_x000D_
</label>_x000D_
<label class="checkbox-wrapper">Two_x000D_
<input type="checkbox">_x000D_
<span class="checkmark"></span>_x000D_
</label>_x000D_
<label class="checkbox-wrapper">Three_x000D_
<input type="checkbox" checked disabled>_x000D_
<span class="checkmark"></span>_x000D_
</label>_x000D_
<label class="checkbox-wrapper">Four_x000D_
<input type="checkbox" disabled>_x000D_
<span class="checkmark"></span>_x000D_
</label>_x000D_
<h2>Radio</h2>_x000D_
<label class="radio-wrapper">One_x000D_
<input type="radio" name="group-y">_x000D_
<span class="pip"></span>_x000D_
</label>_x000D_
<label class="radio-wrapper">Two_x000D_
<input type="radio" name="group-y">_x000D_
<span class="pip"></span>_x000D_
</label>_x000D_
<label class="radio-wrapper">Three_x000D_
<input type="radio" name="group-y" checked disabled>_x000D_
<span class="pip"></span>_x000D_
</label>_x000D_
<label class="radio-wrapper">Four_x000D_
<input type="radio" name="group-y" disabled>_x000D_
<span class="pip"></span>_x000D_
</label>_x000D_
</div>_x000D_
</div>_x000D_
<div class="grid-col" data-theme="retro">_x000D_
<div class="grid-cell">_x000D_
<h1>JS + CSS</h1>_x000D_
<h2>Checkbox</h2>_x000D_
<label>One <input type="checkbox" checked="checked"></label>_x000D_
<label>Two <input type="checkbox"></label>_x000D_
<label>Three <input type="checkbox" checked disabled></label>_x000D_
<label>Four <input type="checkbox" disabled></label>_x000D_
<h2>Radio</h2>_x000D_
<label>One <input type="radio" name="group-z" checked="checked"></label>_x000D_
<label>Two <input type="radio" name="group-z"></label>_x000D_
<label>Three <input type="radio" name="group-z" checked disabled></label>_x000D_
<label>Four <input type="radio" name="group-z" disabled></label>_x000D_
</div>_x000D_
</div>_x000D_
</div>_x000D_
</div>
_x000D_
Yikes! All these workarounds have led me to the conclusion that the HTML checkbox kind of sucks if you want to style it.
As a forewarning, this isn't a CSS implementation. I just thought I'd share the workaround I came up with in case anyone else might find it useful.
I used the HTML5 canvas
element.
The upside to this is that you don't have to use external images and can probably save some bandwidth.
The downside is that if a browser for some reason can't render it correctly, then there's no fallback. Though whether this remains an issue in 2017 is debatable.
I found the old code quite ugly, so I decided to give it a rewrite.
Object.prototype.create = function(args){
var retobj = Object.create(this);
retobj.constructor(args || null);
return retobj;
}
var Checkbox = Object.seal({
width: 0,
height: 0,
state: 0,
document: null,
parent: null,
canvas: null,
ctx: null,
/*
* args:
* name default desc.
*
* width 15 width
* height 15 height
* document window.document explicit document reference
* target this.document.body target element to insert checkbox into
*/
constructor: function(args){
if(args === null)
args = {};
this.width = args.width || 15;
this.height = args.height || 15;
this.document = args.document || window.document;
this.parent = args.target || this.document.body;
this.canvas = this.document.createElement("canvas");
this.ctx = this.canvas.getContext('2d');
this.canvas.width = this.width;
this.canvas.height = this.height;
this.canvas.addEventListener("click", this.ev_click(this), false);
this.parent.appendChild(this.canvas);
this.draw();
},
ev_click: function(self){
return function(unused){
self.state = !self.state;
self.draw();
}
},
draw_rect: function(color, offset){
this.ctx.fillStyle = color;
this.ctx.fillRect(offset, offset,
this.width - offset * 2, this.height - offset * 2);
},
draw: function(){
this.draw_rect("#CCCCCC", 0);
this.draw_rect("#FFFFFF", 1);
if(this.is_checked())
this.draw_rect("#000000", 2);
},
is_checked: function(){
return !!this.state;
}
});
Here's a working demo.
The new version uses prototypes and differential inheritance to create an efficient system for creating checkboxes. To create a checkbox:
var my_checkbox = Checkbox.create();
This will immediately add the checkbox to the DOM and hook up the events. To query whether a checkbox is checked:
my_checkbox.is_checked(); // True if checked, else false
Also important to note is that I got rid of the loop.
Something I neglected to mention in the last update is that using the canvas has more advantages than just making a checkbox that looks however you want it to look. You could also create multi-state checkboxes, if you wanted to.
Object.prototype.create = function(args){
var retobj = Object.create(this);
retobj.constructor(args || null);
return retobj;
}
Object.prototype.extend = function(newobj){
var oldobj = Object.create(this);
for(prop in newobj)
oldobj[prop] = newobj[prop];
return Object.seal(oldobj);
}
var Checkbox = Object.seal({
width: 0,
height: 0,
state: 0,
document: null,
parent: null,
canvas: null,
ctx: null,
/*
* args:
* name default desc.
*
* width 15 width
* height 15 height
* document window.document explicit document reference
* target this.document.body target element to insert checkbox into
*/
constructor: function(args){
if(args === null)
args = {};
this.width = args.width || 15;
this.height = args.height || 15;
this.document = args.document || window.document;
this.parent = args.target || this.document.body;
this.canvas = this.document.createElement("canvas");
this.ctx = this.canvas.getContext('2d');
this.canvas.width = this.width;
this.canvas.height = this.height;
this.canvas.addEventListener("click", this.ev_click(this), false);
this.parent.appendChild(this.canvas);
this.draw();
},
ev_click: function(self){
return function(unused){
self.state = !self.state;
self.draw();
}
},
draw_rect: function(color, offsetx, offsety){
this.ctx.fillStyle = color;
this.ctx.fillRect(offsetx, offsety,
this.width - offsetx * 2, this.height - offsety * 2);
},
draw: function(){
this.draw_rect("#CCCCCC", 0, 0);
this.draw_rect("#FFFFFF", 1, 1);
this.draw_state();
},
draw_state: function(){
if(this.is_checked())
this.draw_rect("#000000", 2, 2);
},
is_checked: function(){
return this.state == 1;
}
});
var Checkbox3 = Checkbox.extend({
ev_click: function(self){
return function(unused){
self.state = (self.state + 1) % 3;
self.draw();
}
},
draw_state: function(){
if(this.is_checked())
this.draw_rect("#000000", 2, 2);
if(this.is_partial())
this.draw_rect("#000000", 2, (this.height - 2) / 2);
},
is_partial: function(){
return this.state == 2;
}
});
I modified slightly the Checkbox
used in the last snippet so that it is more generic, making it possible to "extend" it with a checkbox that has 3 states. Here's a demo. As you can see, it already has more functionality than the built-in checkbox.
Something to consider when you're choosing between JavaScript and CSS.
First, set up a canvas
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
checked = 0; // The state of the checkbox
canvas.width = canvas.height = 15; // Set the width and height of the canvas
document.body.appendChild(canvas);
document.body.appendChild(document.createTextNode(' Togglable Option'));
Next, devise a way to have the canvas update itself.
(function loop(){
// Draws a border
ctx.fillStyle = '#ccc';
ctx.fillRect(0,0,15,15);
ctx.fillStyle = '#fff';
ctx.fillRect(1, 1, 13, 13);
// Fills in canvas if checked
if(checked){
ctx.fillStyle = '#000';
ctx.fillRect(2, 2, 11, 11);
}
setTimeout(loop, 1000/10); // Refresh 10 times per second
})();
The last part is to make it interactive. Luckily, it's pretty simple:
canvas.onclick = function(){
checked = !checked;
}
This is where you might have problems in IE, due to their weird event handling model in JavaScript.
I hope this helps someone; it definitely suited my needs.
The original question and answer are now ~5 years old. As such, this is a little bit of an update.
Firstly, there are a number of approaches when it comes to styling checkboxes. the basic tenet is:
You will need to hide the default checkbox control which is styled by your browser, and cannot be overridden in any meaningful way using CSS.
With the control hidden, you will still need to be able to detect and toggle its checked state
The checked state of the checkbox will need to be reflected by styling a new element
The above can be accomplished by a number of means - and you will often hear using CSS3 pseudo-elements is the right way. Actually, there is no real right or wrong way, it depends on the approach most suitable for the context you will be using it in. That said, I have a preferred one.
Wrap your checkbox in a label
element. This will mean that even when it is hidden, you can still toggle its checked state on clicking anywhere within the label.
Hide your checkbox
Add a new element after the checkbox which you will style accordingly. It must appear after the checkbox so it can be selected using CSS and styled dependent on the :checked
state. CSS cannot select 'backwards'.
label input {_x000D_
visibility: hidden;/* <-- Hide the default checkbox. The rest is to hide and allow tabbing, which display:none prevents */_x000D_
display: block;_x000D_
height: 0;_x000D_
width: 0;_x000D_
position: absolute;_x000D_
overflow: hidden;_x000D_
}_x000D_
label span {/* <-- Style the artificial checkbox */_x000D_
height: 10px;_x000D_
width: 10px;_x000D_
border: 1px solid grey;_x000D_
display: inline-block;_x000D_
}_x000D_
[type=checkbox]:checked + span {/* <-- Style its checked state */_x000D_
background: black;_x000D_
}
_x000D_
<label>_x000D_
<input type='checkbox'>_x000D_
<span></span>_x000D_
Checkbox label text_x000D_
</label>
_x000D_
But hey! I hear you shout. What about if I want to show a nice little tick or cross in the box? And I don't want to use background images!
Well, this is where CSS3's pseudo-elements can come into play. These support the content
property which allows you to inject Unicode icons representing either state. Alternatively, you could use a third party font icon source such as font awesome (though make sure you also set the relevant font-family
, e.g. to FontAwesome
)
label input {_x000D_
display: none; /* Hide the default checkbox */_x000D_
}_x000D_
_x000D_
/* Style the artificial checkbox */_x000D_
label span {_x000D_
height: 10px;_x000D_
width: 10px;_x000D_
border: 1px solid grey;_x000D_
display: inline-block;_x000D_
position: relative;_x000D_
}_x000D_
_x000D_
/* Style its checked state...with a ticked icon */_x000D_
[type=checkbox]:checked + span:before {_x000D_
content: '\2714';_x000D_
position: absolute;_x000D_
top: -5px;_x000D_
left: 0;_x000D_
}
_x000D_
<label>_x000D_
<input type='checkbox'>_x000D_
<span></span>_x000D_
Checkbox label text_x000D_
</label>
_x000D_
Here is a CSS/HTML-only version, no jQuery or JavaScript needed at all, Simple and clean HTML and really simple and short CSS.
Here is the JSFiddle
Here is the HTML:
<div id="myContainer">
<input type="checkbox" name="myCheckbox" id="myCheckbox_01_item" value="red" />
<label for="myCheckbox_01_item" class="box"></label>
<label for="myCheckbox_01_item" class="text">I accept the Terms of Use.</label>
</div>
Here is the CSS
#myContainer {
outline: black dashed 1px;
width: 200px;
}
#myContainer input[type="checkbox"][name="myCheckbox"] {
display: none;
}
#myContainer input[type="checkbox"][name="myCheckbox"]:not(:checked) + label.box {
display: inline-block;
width: 25px;
height: 25px;
border: black solid 1px;
background: #FFF ;
margin: 5px 5px;
}
#myContainer input[type="checkbox"][name="myCheckbox"]:checked + label.box {
display: inline-block;
width: 25px;
height: 25px;
border: black solid 1px;
background: #F00;
margin: 5px 5px;
}
#myContainer input[type="checkbox"][name="myCheckbox"] + label + label.text {
font: normal 12px arial;
display: inline-block;
line-height: 27px;
vertical-align: top;
margin: 5px 0px;
}
This can be adapted to be able to have individual radio or checkboxes, grooups of checkboxes and groups of radio buttons as well.
This html/css, will allow you to also capture click on the label, so the checkbox will be checked and unchecked even if you click just on the label.
This type of checkbox/radio button works perfectly with any form, no problem at all. Have been tested using PHP, ASP.NET (.aspx), JavaServer Faces, and ColdFusion too.
A simple and lightweight template as well:
input[type=checkbox] {_x000D_
cursor: pointer;_x000D_
}_x000D_
_x000D_
input[type=checkbox]:checked:before {_x000D_
content: "\2713";_x000D_
background: #fffed5;_x000D_
text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);_x000D_
font-size: 20px;_x000D_
text-align: center;_x000D_
line-height: 8px;_x000D_
display: inline-block;_x000D_
width: 13px;_x000D_
height: 15px;_x000D_
color: #00904f;_x000D_
border: 1px solid #cdcdcd;_x000D_
border-radius: 4px;_x000D_
margin: -3px -3px;_x000D_
text-indent: 1px;_x000D_
}_x000D_
_x000D_
input[type=checkbox]:before {_x000D_
content: "\202A";_x000D_
background: #ffffff;_x000D_
text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);_x000D_
font-size: 20px;_x000D_
text-align: center;_x000D_
line-height: 8px;_x000D_
display: inline-block;_x000D_
width: 13px;_x000D_
height: 15px;_x000D_
color: #00904f;_x000D_
border: 1px solid #cdcdcd;_x000D_
border-radius: 4px;_x000D_
margin: -3px -3px;_x000D_
text-indent: 1px;_x000D_
}
_x000D_
<input type="checkbox" checked="checked">checked1<br>_x000D_
<input type="checkbox">unchecked2<br>_x000D_
<input type="checkbox" checked="checked" id="id1">_x000D_
<label for="id1">checked2+label</label><br>_x000D_
<label for="id2">unchecked2+label+rtl</label>_x000D_
<input type="checkbox" id="id2">_x000D_
<br>
_x000D_
You can style checkboxes with a little trickery using the label
element an example is below:
.checkbox > input[type=checkbox] {_x000D_
visibility: hidden;_x000D_
}_x000D_
_x000D_
.checkbox {_x000D_
position: relative;_x000D_
display: block;_x000D_
width: 80px;_x000D_
height: 26px;_x000D_
margin: 0 auto;_x000D_
background: #FFF;_x000D_
border: 1px solid #2E2E2E;_x000D_
border-radius: 2px;_x000D_
-webkit-border-radius: 2px;_x000D_
-moz-border-radius: 2px;_x000D_
}_x000D_
_x000D_
.checkbox:after {_x000D_
position: absolute;_x000D_
display: inline;_x000D_
right: 10px;_x000D_
content: 'no';_x000D_
color: #E53935;_x000D_
font: 12px/26px Arial, sans-serif;_x000D_
font-weight: bold;_x000D_
text-transform: capitalize;_x000D_
z-index: 0;_x000D_
}_x000D_
_x000D_
.checkbox:before {_x000D_
position: absolute;_x000D_
display: inline;_x000D_
left: 10px;_x000D_
content: 'yes';_x000D_
color: #43A047;_x000D_
font: 12px/26px Arial, sans-serif;_x000D_
font-weight: bold;_x000D_
text-transform: capitalize;_x000D_
z-index: 0;_x000D_
}_x000D_
_x000D_
.checkbox label {_x000D_
position: absolute;_x000D_
display: block;_x000D_
top: 3px;_x000D_
left: 3px;_x000D_
width: 34px;_x000D_
height: 20px;_x000D_
background: #2E2E2E;_x000D_
cursor: pointer;_x000D_
transition: all 0.5s linear;_x000D_
-webkit-transition: all 0.5s linear;_x000D_
-moz-transition: all 0.5s linear;_x000D_
border-radius: 2px;_x000D_
-webkit-border-radius: 2px;_x000D_
-moz-border-radius: 2px;_x000D_
z-index: 1;_x000D_
}_x000D_
_x000D_
.checkbox input[type=checkbox]:checked + label {_x000D_
left: 43px;_x000D_
}
_x000D_
<div class="checkbox">_x000D_
<input id="checkbox1" type="checkbox" value="1" />_x000D_
<label for="checkbox1"></label>_x000D_
</div>
_x000D_
And a FIDDLE for the above code. Note that some CSS doesn't work in older versions of browsers, but I'm sure there are some fancy JavaScript examples out there!
You can use iCheck. It is customized checkboxes and radio buttons for jQuery & Zepto, and maybe it will help you.
Make sure jQuery v1.7+ is loaded before the icheck.js
Insert before in your HTML (replace your-path and color-scheme):
<link href="your-path/minimal/color-scheme.css" rel="stylesheet">
<script src="your-path/icheck.js"></script>
Example for a Red color scheme:
<link href="your-path/minimal/red.css" rel="stylesheet">
<script src="your-path/icheck.js"></script>
Add some checkboxes and radio buttons to your HTML:
<input type="checkbox">
<input type="checkbox" checked>
<input type="radio" name="iCheck">
<input type="radio" name="iCheck" checked>
Add JavaScript to your HTML to launch iCheck plugin:
<script>
$(document).ready(function(){
$('input').iCheck({
checkboxClass: 'icheckbox_minimal',
radioClass: 'iradio_minimal',
increaseArea: '20%' // Optional
});
});
</script>
For different from black color schemes use this code (example for Red):
<script>
$(document).ready(function(){
$('input').iCheck({
checkboxClass: 'icheckbox_minimal-red',
radioClass: 'iradio_minimal-red',
increaseArea: '20%' // Optional
});
});
</script>
Done
It seems you can change the colour of the checkbox in grayscale by using CSS only.
The following converts the checkboxes from black to gray (which was about what I wanted):
input[type="checkbox"] {
opacity: .5;
}
No, you still can't style the checkbox itself, but I (finally) figured out how to style an illusion while keeping the functionality of clicking a checkbox. It means that you can toggle it even if the cursor isn't perfectly still without risking selecting text or triggering drag-and-drop!
This solution probably also fits radio buttons.
The following works in Internet Explorer 9, Firefox 30.0 and Chrome 40.0.2214.91 and is just a basic example. You can still use it in combination with background images and pseudo-elements.
http://jsfiddle.net/o0xo13yL/1/
label {
display: inline-block;
position: relative; /* Needed for checkbox absolute positioning */
background-color: #eee;
padding: .5rem;
border: 1px solid #000;
border-radius: .375rem;
font-family: "Courier New";
font-size: 1rem;
line-height: 1rem;
}
label > input[type="checkbox"] {
display: block;
position: absolute; /* Remove it from the flow */
width: 100%;
height: 100%;
margin: -.5rem; /* Negative the padding of label to cover the "button" */
cursor: pointer;
opacity: 0; /* Make it transparent */
z-index: 666; /* Place it on top of everything else */
}
label > input[type="checkbox"] + span {
display: inline-block;
width: 1rem;
height: 1rem;
border: 1px solid #000;
margin-right: .5rem;
}
label > input[type="checkbox"]:checked + span {
background-color: #666;
}
<label>
<input type="checkbox" />
<span> </span>Label text
</label>
I prefer to use icon fonts (such as FontAwesome) since it's easy to modify their colours with CSS, and they scale really well on high pixel-density devices. So here's another pure CSS variant, using similar techniques to those above.
(Below is a static image so you can visualize the result; see the JSFiddle for an interactive version.)
As with other solutions, it uses the label
element. An adjacent span
holds our checkbox character.
span.bigcheck-target {_x000D_
font-family: FontAwesome; /* Use an icon font for the checkbox */_x000D_
}_x000D_
_x000D_
input[type='checkbox'].bigcheck {_x000D_
position: relative;_x000D_
left: -999em; /* Hide the real checkbox */_x000D_
}_x000D_
_x000D_
input[type='checkbox'].bigcheck + span.bigcheck-target:after {_x000D_
content: "\f096"; /* In fontawesome, is an open square (fa-square-o) */_x000D_
}_x000D_
_x000D_
input[type='checkbox'].bigcheck:checked + span.bigcheck-target:after {_x000D_
content: "\f046"; /* fontawesome checked box (fa-check-square-o) */_x000D_
}_x000D_
_x000D_
/* ==== Optional - colors and padding to make it look nice === */_x000D_
body {_x000D_
background-color: #2C3E50;_x000D_
color: #D35400;_x000D_
font-family: sans-serif;_x000D_
font-weight: 500;_x000D_
font-size: 4em; /* Set this to whatever size you want */_x000D_
}_x000D_
_x000D_
span.bigcheck {_x000D_
display: block;_x000D_
padding: 0.5em;_x000D_
}
_x000D_
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />_x000D_
_x000D_
<span class="bigcheck">_x000D_
<label class="bigcheck">_x000D_
Cheese_x000D_
<input type="checkbox" class="bigcheck" name="cheese" value="yes" />_x000D_
<span class="bigcheck-target"></span>_x000D_
</label>_x000D_
</span>
_x000D_
Here's the JSFiddle for it.
No JavaScript or jQuery required.
Change your checkbox style simple way.
input[type="checkbox"] {_x000D_
display: none;_x000D_
border: none !important;_x000D_
box-shadow: none !important;_x000D_
}_x000D_
_x000D_
input[type="checkbox"] + label span {_x000D_
background: url(http://imgh.us/uncheck.png);_x000D_
width: 49px;_x000D_
height: 49px;_x000D_
display: inline-block;_x000D_
vertical-align: middle;_x000D_
}_x000D_
_x000D_
input[type="checkbox"]:checked + label span {_x000D_
background: url(http://imgh.us/check_2.png);_x000D_
width: 49px;_x000D_
height: 49px;_x000D_
vertical-align: middle;_x000D_
}
_x000D_
<input type="checkbox" id="option" />_x000D_
<label for="option"> <span></span> Click me </label>
_x000D_
I think the easiest way to do it is by styling a label
and making the checkbox
invisible.
HTML
<input type="checkbox" id="first" />
<label for="first"> </label>
CSS
checkbox {
display: none;
}
checkbox + label {
/* Style for checkbox normal */
width: 16px;
height: 16px;
}
checkbox::checked + label,
label.checked {
/* Style for checkbox checked */
}
The checkbox
, even though it is hidden, will still be accessible, and its value will be sent when a form is submitted. For old browsers you might have to change the class of the label
to checked using JavaScript because I don't think old versions of Internet Explorer understand ::checked
on the checkbox
.
Modify checkbox style with plain CSS3, don't required any JS&HTML manipulation.
.form input[type="checkbox"]:before {_x000D_
display: inline-block;_x000D_
font: normal normal normal 14px/1 FontAwesome;_x000D_
font-size: inherit;_x000D_
text-rendering: auto;_x000D_
-webkit-font-smoothing: antialiased;_x000D_
content: "\f096";_x000D_
opacity: 1 !important;_x000D_
margin-top: -25px;_x000D_
appearance: none;_x000D_
background: #fff;_x000D_
}_x000D_
_x000D_
.form input[type="checkbox"]:checked:before {_x000D_
content: "\f046";_x000D_
}_x000D_
_x000D_
.form input[type="checkbox"] {_x000D_
font-size: 22px;_x000D_
appearance: none;_x000D_
-webkit-appearance: none;_x000D_
-moz-appearance: none;_x000D_
}
_x000D_
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />_x000D_
_x000D_
<form class="form">_x000D_
<input type="checkbox" />_x000D_
</form>
_x000D_
Source: Stackoverflow.com