Is it possible to style the HTML checkbox without using javascripts?
This code for example will work fine with IE, but not on Firefox or Chrome.
http://jsfiddle.net/5wJxF/
Any suggestions?
Is it possible to style the HTML checkbox without using javascripts? This code for
Share
YES. It is possible.
You can’t directly style the checkbox element, but the effect you’re looking for can be achieved if you use a
<label>element in conjunction with the checkbox, and style that.And then your CSS would look like this:
Here is a working example: http://jsfiddle.net/TVaPX/ (tested with Firefox 5)
The trouble with this approach is that it only works in modern browsers. Older browsers may not support the
:checkedor+CSS selectors. But if you’re okay with not supporting older versions of IE, then this will work. The example above does not work in IE8 (it supports+but not:checked).If you’re not comfortable with that, then you’ll have to stick with a Javascript solution.
However, with an approach similar to this, you can still do it with very minimal amounts of Javascript code: simply have a one-line JS that toggles the class of the checkbox when it’s checked, and you can use all the above code, but with the alternate classname instead of the
:checkedselector. That will work in IE7 and IE8.Hope that helps.