A newer and better crossbrowser solution for custom checkbox and radio button styling is available on this page.
The full example for this blogpost is available for download at this link. You can play with the online demo on this page.
WebKit browsers (Safari, Chrome, Konqueror) have vendor-specific implementation of the CSS appearance property, as well as full support for the :checked and :disabled pseudo classes. The property is called -khtml-appearance and it’s default value is visual. When set to none, it turns the specified control(s) to a blank slate, and this, combined with :checked, :hover and :disabled facilitates any styling of checkboxes and radiobuttons without JavaScript or complex layout tricks. All we need is a sprite that contains all possible states (checked, unchecked, hover) of checkboxes and radiobuttons and as few as 50 lines of CSS:
1. The image sprite
![]()
2. The HTML:
<ul>
<li>
<input type="checkbox" id="CheckBox1" checked="checked" />
<label for="CheckBox1">Checkbox 1</label>
</li>
<li>
<input type="checkbox" id="CheckBox2" />
<label for="CheckBox2">Checkbox 2</label>
</li>
<li>
<input type="checkbox" id="CheckBox3" disabled="disabled" />
<label for="CheckBox3">Checkbox 2</label>
</li>
</ul>
<ul>
<li>
<input type="radio" id="Radio1" checked="checked" name="radio" />
<label for="Radio1">Radio 1</label>
</li>
<li>
<input type="radio" id="Radio2" name="radio" />
<label for="Radio2">Radio 2</label>
</li>
<li>
<input type="radio" id="Radio3" name="radio" disabled="disabled" />
<label for="Radio3">Radio 2</label>
</li>
</ul>
3. And the CSS, where we make use of attribute selectors, pseudo classes, and of course -khtml-appearance set to none:
label
{
font: normal 12px/20px Arial, Sans-serif;
color: black;
vertical-align: middle;
}
/* common settings checkbox and radiobutton */
input[type="checkbox"],
input[type="radio"]
{
-khtml-appearance: none;
background: url('RadioAndCheckBoxSprite.gif') no-repeat;
width: 20px;
height: 20px;
vertical-align: middle;
}
input[type="checkbox"]
{
background-position-x: left;
}
input[type="radio"]
{
background-position-x: right;
}
input[type="checkbox"]:hover,
input[type="radio"]:hover
{
background-position-y: -40px;
}
input[type="checkbox"]:checked,
input[type="radio"]:checked
{
background-position-y: -20px;
}
input[type="checkbox"]:checked:hover,
input[type="radio"]:checked:hover
{
background-position-y: -60px;
}
/* disabled settings for checkbox and radiobutton */
input[type="radio"]:disabled,
input[type="checkbox"]:disabled
{
opacity: .3;
}
FireFox has a similar implementation of the property called -moz-appearance, however its default value is none, and at present the above approach cannot be used for any browser but Safari and Google Chrome.
The full example is available for download at this link. You can play with the online demo on this page.
You may also be interested in the following scripts that take control on the visual appearance of other form elements.
Find more experiments here.
[...] – Input.JS – Enhance the textboxes, passwords and textarea elements of your website forms with rounded corners with four lines of code. - SelectBoxReplacement.JS – Unobtrusive and almost codeless way to give the ordinary HTML combobox form controls (<select>…</select>) cool looks and appeal. - ButtonReplacement.JS – Unobtrusive and almost codeless way to give the ordinary HTML buttons (<input type="button | submit | reset" /> and <button type="button | submit | reset">…</button>) cool looks and appeal. - CheckBox.XML - Skin the checkboxes in your forms. - RadioButton.XML - Skin the radiobuttons in your forms. - SpinBox.JS – Codelessly turn the ordinary text inputs (<input type="text" />) into skinnable and multifunctional spinboxes. - Upload.JS – Quick and dirty way to add style to the file input boxes (<input type="file" />) of your forms. - Upload.XML - Create stylish file upload (<input type="file" />) boxes with this AJAX script. - SearchBox.XML – Integrate any number of web search engines in your websites with this cool skinnable AJAX script. - ColorPicker.JS – Multifunctional, fast and skinnable color picker. - Skinnable Browser Dialogs – replace the boring and impossible to style default browser alert, prompt and confirm with this cool script. – Using CSS to Style Radiobuttons and Checkboxes in Safari and Chrome. [...]
[...] 01. Better Image Scaling and Resampling in Internet Explorer 02. Setting Opacity and Other Filters in Internet Explorer 8 03. The 32 External CSS Files Limitation of Internet Explorer and More 04. Unobtrusive CSS Loading Indicator for Images 05. CSS: Filtering and Distinguishing Google Chrome and Safari 06. Controlling the CSS Opacity Rate of Child Elements in Transparent Parents in Internet Explorer 07. $style – Get Any CSS Property Value of an Object 08. Emulating border-color: transparent in Internet Explorer 6 09. An Exotic CSS Hack for Internet Explorer 10. CSS Watermark 11. Using CSS to Style Radiobuttons and Checkboxes in Safari and Chrome [...]
[...] DIRECT LINK » [...]
[...] It was actually Dave Arthur from the Royal London Society for the Blind who came up with the solution, or more accurately came up with a link to the solution. [...]
Thanks for the great info!
I used this while optimizing a page for iPhone browsing.
One adjustment I had to make was to set “border: none;” in the “input[type="checkbox"],input[type="radio"]” selector.
Otherwise the unchecked state would be padded with the standard Safari checkbox border.
Thanks,
Noah
http://WebMobilize.mobi
[...] Using CSS to Style Radiobuttons and Checkboxes in Safari and Chrome July 2009 5 comments [...]
[...] Using CSS to Style Radiobuttons and Checkboxes in Safari and Chrome [...]