This JavaScript and CSS will allow you to use custom images to style checkboxes, radio buttons and select lists.
Have you ever wanted to use your own images for checkboxes, radio buttons or select lists? This script will let you do that. Easily. The unobtrusive script gracefully degrades, so if JavaScript is disabled, normal form input objects appear instead of your customized elements. This works flawlessly in Firefox, Safari, Internet Explorer 7, Opera and others. In Internet Explorer, the select lists are unstyled, but the checkboxes and radio buttons still look and function beautifully. It's this easy:
View a more comprehensive example
In a nutshell, the JavaScript looks for every form element with class="styled"
on it; hides the real form element; sticks a span
tag with a CSS class on it next to the element; and, finally, mouse events are added to the span
that handles the visual stages form inputs go through when they are clicked.
To get the checkboxes, radio buttons and select boxes to work properly, you'll need to change three variables in the script: checkboxHeight, radioHeight and selectWidth on lines 21-23. If you use the images I created, you won't have to change the variables, but if you make your own, chances are you'll have to. The checkboxes and radio buttons to the right are linked to transparent PNG images for you to use freely if you'd like. The frist two variables are the height of a single stage of the checkbox and radio button, and the third is the width of the select box. You may need to spend a little time tinkering with the checkbox and radio button images so they don't twitch during different stages.
If you make your own images, you may need to change a few things in the cascading style sheet. In .checkbox
and .radio
, the height
property should be one fourth of the height of the full size images. You also might have to change the width
property in .select
selector. You probably won't have to edit any other portions of the CSS, but regardless, this part is still straight forward.
The script won't customize checkboxes, radio buttons or select lists unless you declare the styled class. Simply add class="styled"
to any checkbox, radio button or option list and the JavaScript and CSS will take over from there.
You are free to share, alter and use this script commercially. Just leave the title, my name and website in tact in the JavaScript script. This script is licensed under a Creative Commons license.
from: http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/