Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to style checkbox using the following:

<input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" />

But, it is still displaying the default checkbox. How to do it?

share|improve this question
2  
2  
Here is an article with no-js solution: moodevs.com/blog/ui/1.html – Taras Neporozhniy Sep 26 '12 at 21:08
BTW, useful jQuery plugin to style checkboxes alt-checkbox.starikovs.com – infous Mar 10 at 18:17
i think you are looking for something like this: asimishaq.com/myfiles/image-checkbox See this post: stackoverflow.com/questions/16352864/… – asim-ishaq May 3 at 12:11

6 Answers

up vote 85 down vote accepted

UPDATE: The below answer references the state of things before widespread availability of CSS3. In modern browsers it is more possible to create checkbox replacements with your preferred styling.

Here are some useful links:

It is worth noting that the fundamental issue has not changed. You still can't apply styles (borders, etc.) directly to the checkbox element and have those styles affect the display of the HTML checkbox. What has changed, however, is that it's now possible to hide the actual checkbox and replace it with a styled element of your own, using nothing but CSS. In particular, because CSS now has a :checked selector, you can make your replacement correctly reflect the checked status of the box.


OLDER ANSWER

Here's a useful article about styling checkboxes. Basically what that writer found was that it varies tremendously from browser to browser, and that many browsers always display the default checkbox no matter how you style it. So there really isn't an easy way.

It's not hard to imagine a workaround where you would use javascript to overlay an image on the checkbox and have clicks on that image cause the real checkbox to be checked. Users without javascript would see the default checkbox.

Edited to add: here's a nice script that does this for you; it hides the real checkbox element, replaces it with a styled span, and redirects the click events.

share|improve this answer
This answer is getting old! The primary link leads to a site comparing IE6 and IE7 styles... – Jonatan Littke Dec 14 '12 at 13:15
Fair point -- and the basic point isn't really true anymore, in modern browsers. I've updated with some newer links, but left the original as a resource for older browsers. – JacobM Dec 14 '12 at 19:04
5  
+1 for the updated answer – Venemo Feb 8 at 13:09

There is a way to do this using just css. We can (ab)use the label element and style that instead. The caveat is that this will not work for IE8 and lower versions.

CSS:

.myCheckbox input {
    display: none;
}

.myCheckbox span {
    width: 20px;
    height: 20px;
    display: block;
    background: url("link_to_image");
}

.myCheckbox input:checked + span {
    background: url("link_to_another_image");
}

HTML:

<label for="test">Label for my styled "checkbox"</label>
<label class="myCheckbox">
    <input type="checkbox" name="test"/>
    <span></span>
</label>
share|improve this answer
Just what the doctor ordered! – Joachim H. Skeie Oct 4 '12 at 14:59
@Blake Pettersson will this work in IE 8+? – Gandalf StormCrow Oct 5 '12 at 1:14
@GandalfStormCrow this will work for any browser that supports the :checked pseudo-class, which IE8 does NOT support. You can check if this works with selectivizr.com - which adds support for :checked and friends. – Blake Pettersson Oct 5 '12 at 12:08
In other words, IE9 and later versions supports :checked. – Blake Pettersson Oct 8 '12 at 13:13
very nice thank you – Constanta Feb 6 at 15:32

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.

share|improve this answer
1  
It may have been true in '10, but not now, you should delete this answer. – MichaƂ K Feb 21 at 8:40

I think the easiest way to do it is by styling a label and making the check-box invisible.

HTML

<input type="checkbox" id="first" />
<label for="first">&nbsp;</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 it's 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 IE understand ::checked on the checkbox

share|improve this answer
3  
The difference between your answer and mine is what exactly? – Blake Pettersson Dec 5 '12 at 17:50
@BlakePettersson yours is correct, ::checked is just wrong (I even tried just in case it's also allowed) ;) – esp Feb 24 at 0:31

this the checkbox like with iPhone, it it used the pure css3 to create,pls view the tutorial:http://www.w3cplus.com/css3/css3-style-iphone-checkbox

share|improve this answer

You can achieve quite a cool custom checkbox effect by using the new abilities that come with the :after and :before psuedo 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, which unfortunately at the moment appears to be webkit only, but I am sure FF + IE, will work in the future (as no prefixes used) :)

See example: http://jsfiddle.net/YMuPw/15/

.myinput[type="checkbox"]:before{
    position: relative;
    display: block;
    width: 11px;
    height: 11px;
    border: 1px solid #808080;
    content: "";
    background: #FFF;
}
.myinput[type="checkbox"]:after{
    position: relative;
    display: block;
    left: 2px;
    top: -11px;
    width: 7px;
    height: 7px;
    border-width: 1px;
    border-style: solid;
    border-color: #B3B3B3 #dcddde #dcddde #B3B3B3;
    content: "";
    background-image: linear-gradient(135deg, #B1B6BE 0%,#FFF 100%);
    background-repeat: no-repeat;
    background-position:center;
}
.myinput[type="checkbox"]:checked:after{
    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%);
}
.myinput[type="checkbox"]:disabled:after{
    -webkit-filter: opacity(0.4);
}
.myinput[type="checkbox"]:not(:disabled):checked:hover:after{
    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%);
}
.myinput[type="checkbox"]:not(:disabled):hover:after{
    background-image: linear-gradient(135deg, #8BB0C2 0%,#FFF 100%);  
    border-color: #85A9BB #92C2DA #92C2DA #85A9BB;  
}
.myinput[type="checkbox"]:not(:disabled):hover:before{
    border-color: #3D7591;
}

.myinput.large{
    height:22px;
    width:22px;
}

.myinput.large[type="checkbox"]:before{
    width: 20px;
    height: 20px;
}
.myinput.large[type="checkbox"]:after{
    top: -20px;
    width: 16px;
    height: 16px;
}
.myinput.large.custom[type="checkbox"]:checked:after{
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%);
}
.myinput.large.custom[type="checkbox"]:not(:disabled):checked:hover:after{
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%);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.