Custom Checkboxes, Custom Radio Buttons, Custom Select Lists

This JavaScript and CSS will allow you to use custom images for checkboxes, radio buttons and select lists.

Checkboxes, Radio Buttons, Select Lists, Custom HTML Form Elements

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:

  1. <input type="checkbox" class="styled" />

The example

Sexy

Boring

Sleek

Nice

Radical

Good

Fair

Poor

Usage

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.

How does it work?

In a nutshell, the JavaScript looks for any form element with class="styled" declared; hides the real 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 stages when it is clicked.

View a more comprehensive example

Download the full script

Custom Checkboxes Custom Radio Buttons

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.

  1. var checkboxHeight = "25";
  2. var radioHeight = "25";
  3. var selectWidth = "190";

The CSS

If you make your own images, you may need to change a few things in the cascading style sheet. In span.checkbox and span.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 span.select selector. You probably won't have to edit any other portions of the CSS, but regardless, this part is still straight forward.

  1. span.checkbox {
  2.   width: 19px;
  3.   height: 25px;
  4.   padding: 0 5px 0 0;
  5.   background: url(checkbox.gif) no-repeat;
  6.   display: block;
  7.   clear: left;
  8.   float: left;
  9. }
  10. span.radio {
  11.   width: 19px;
  12.   height: 25px;
  13.   padding: 0 5px 0 0;
  14.   background: url(radio.gif) no-repeat;
  15.   display: block;
  16.   clear: left;
  17.   float: left;
  18. }
  19. span.select {
  20.   position: absolute;
  21.   width: 158px; /* With the padding included, the width is 190 pixels: the actual width of the image. */
  22.   height: 21px;
  23.   padding: 0 24px 0 8px;
  24.   color: #fff;
  25.   font: 12px/21px arial,sans-serif;
  26.   background: url(select.gif) no-repeat;
  27.   overflow: hidden;
  28. }

The XHTML

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.

  1. <input type="checkbox" name="a" checked="checked" class="styled" /> Sexy
  2. <input type="checkbox" name="b" class="styled" /> Boring
  3. <input type="radio" name="c" checked="checked" class="styled" /> Radical
  4. <input type="radio" name="c" class="styled" /> Poor
  5. <select name="d" class="styled">
  6.   <option value="1">Option 1</option>
  7.   <option value="2">Option 2</option>
  8.   <option value="3">Option 3</option>
  9.   <option value="4">Option 4</option>
  10. </select>

Navigation

Web Design Resources and Articles