FancyBoxes

Intro

FancyBoxes is a simple Prototype based script, which allows you to individually style your forms. If you ever wanted your checkboxes and radioboxes to look consistant in different browsers or just wanted them to look prettier - FancyBoxes is for you. It's unobtrusive and easy to use.

It's inspired by the jQuery-plugin prettyCheckboxes from Stephane Caron.

If you need help or have features requests feel free to comment to this blog post: Better looking radio buttons and checkboxes with FancyBoxes.

Demos

Just have a look and see what FancyBoxes can do for you.

FancyBoxes - Checkbox
FancyBoxes - Radio

FancyBoxes - Checkbox
FancyBoxes - Radio

FancyBoxes checkboxes as a "ticklist"
FancyBoxes - Radio

FancyBoxes - Checkbox
FancyBoxes - Radio

FancyBoxes - Checkbox
FancyBoxes - Radio

FancyBoxes - Checkbox
FancyBoxes - Radio

FancyBoxes - Checkbox
FancyBoxes - Radio

FancyBoxes - Checkbox
FancyBoxes - Checkbox

You can use the following two buttons to bind or unbind FancyBoxes. That means that you can switch off the FancyBoxes and display the radio and checkbox elements as normal.

Installation

FancyBoxes requires the JavaScript framework Prototype. You can grab it here or include it directly via the Google AJAX Libraries API. FancyBoxes uses CSS for styling the checkboxes and radio-buttons so you have to include the css-file which is included in the download. Upload the images for the boxes and your're almost done. All you have to do now is to include the JavaScript and CSS files. If you have other paths or want other images: check the image-paths in the css-file.

It's as simple as this:

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js'></script>
<script type='text/javascript' src='/fancyboxes/fancyboxes.js'></script>
<link rel="stylesheet" type="text/css" href="/fancyboxes/fancyboxes.css" />

If you don't want to load an exta Stylesheet you could simply copy the contents of /fancyboxes/fancyboxes.css to your default css-file.

How to use FancyBoxes

Usage is really simple. If you use the defaults provided by FancyBoxes all you have to do is this simple call:

new FancyBoxes();

This will convert all your radio-buttons and checkboxes into FancyBoxes. No other steps requried. If JavaScript is disabled the form will still work as normal.

Your input-elements need to be inside a label tag or must have a label tag associated, like so:

<label><input type="text" name="radio" type="radio" value="a" /> Choose A</label>
<label><input type="text" name="radio" type="radio" value="b" /> Choose B</label>
<label><input type="text" name="radio" type="radio" value="c" /> Choose C</label>
or
<input type="text" name="checkbox" type="checkbox" value="a" id="option_a"/> <label for="option_a">Choose A</label>

FancyBoxes options and API

FancyBoxes has some options, they are all listed below with their defaul values:

new FancyBoxes({
	elements: 'input[type=checkbox],input[type=radio]', 
	autoBind: true, // convert boxes automatically, if set to false you have to call FancyBoxes.bind()
	cssPrefix: 'fb_', // prefix for all css-classes used by FancyBoxes
	display: 'list', // display the box as inline or as list,
	images: false // here you can specify the images to use. It has to be a hash like: {radio: "image.png", checkbox: "image2.png"}
});

Options

elements

With this option you can specify which input-elements should be replaced with FancyBoxes. By default all checkboxes and radio-buttons are converted. This option can either take a string, which will be treated as a css-selector or an array of input elements. Here are some examples:
// only convert checkboxes
new FancyBoxes({elements: 'input[type=checkbox]'});

// only convert input-elements with the class "fb" 
new FancyBoxes({elements: 'input.fb'});
      
// only convert checkboxes and radio-buttons which are inside an element with the class "fancy"
new FancyBoxes({elements: '.fancy input[type=checkbox], .fancy input[type=radio]'});

// create a array of input-elements and use that:
var my_array = $$('input[type=checkbox]');
new FancyBoxes({elements: my_array}); 
	  

autoBind

By default FancyBoxes converts the selected elements when you create it with new FancyBoxes(). If you want to you can disable this automatic conversion and call bind() yourself:
var fb = new FancyBoxes({autoBind: false});
// later you can call      
fb.bind();

cssPrefix

FancyBoxes uses a couple of other css-classes to style the boxes. It is theoretically possible that you already use these classes. To not run into this problem you can tell FancyBoxes to use a prefix for all it's classnames:
new FancyBoxes({cssPrefix: 'my_fb'});
This will prefix all internal classes with the string "my_fb". By default "fb_" is used. If you change this settings you must also change the css-file to match this changes.

display

With this setting you can tell FancyBoxes if you want to render the boxes inline or as a list. The default is list: all boxes will be in a new line.
var fb = new FancyBoxes({display: 'list'});

images

The recommended way to customize the look of FancyBoxes is to use CSS. If you just want to replace the used images you can however do this with this option.
var fb = new FancyBoxes({images: {radio: "radio.png", checkbox: "checkbox.png"});

API

There are two public methods that you can call:

bind()

If you use the "autoBind: false" option you have to call bind() yourself, like so:
var fb = new FancyBoxes({autoBind: false});
fb.bind();

unbind()

To unbind FancyBoxes you can call unbind(). All FancyBoxes are then converted back to "normal" radio-buttons and checkboxes:
var fb = new FancyBoxes();
fb.unbind();

Customize FancyBoxes

If you want to customize the look of your radio-buttons and checkboxes you can create your own image and use this with FancyBoxes. FancyBoxes comes already with a bunch of different images out of the box:

If you want to create your own keep in mind: Every image contains all three states (checked, unchecked, hovered). If you need bigger or smaller images you have to edit the stylesheet accordingly.

To use other images is easy. You could either use the "images"-option described above or add a new css rule:

label.fb_checkbox span.fb_holder {
	background-image: url(fb/custom_checkbox.png); 
}
label.fb_radio span.fb_holder {
	background-image: url(fb/custom_radio.png); 
}
You can add additonal classes to your label-elements and use this in your CSS:
<label class="xy"><input type="checkbox" name="xy" value="xy" /> xy</label>
<label class="ab"><input type="checkbox" name="ab" value="ab" /> ab</label>
...  
<style type="text/css">    
label.fb_checkbox.xy span.fb_holder {
	background-image: url(fb/custom_checkbox_xy.png); 
}
label.fb_checkbox.ab span.fb_holder {
	background-image: url(fb/custom_checkbox_ab.png); 
}
</style>
Or you can simply wrap another element around your boxes and give them a class and use it in your css:
<div class="xy"><label class="xy"><input type="checkbox" name="xy" value="xy" /> xy</label>
<label class="ab"><input type="checkbox" name="ab" value="ab" /> ab</label></div>
...  
<style type="text/css">
.xy label.fb_checkbox span.fb_holder {
	background-image: url(fb/custom_checkbox_xy.png); 
}
</style>
In the fancyboxes.zip file you'll find a directory named "scalable". It contains a boxes.svg file which you can open and edit with the free software "inkscape". The file contains some sample images that you can use as a template for your own.

License

Just grab it and use it...free.

Download

Download fancyboxes.zip

Changelog

2008-03-07 - added focus state

I added a focus state. With this new feature FancyBoxes is usable if you only want to use you're keybord. Tab through the boxes and select them via space.

Contact

If you need help or have features requests feel free to comment to this blog post: Better looking radio buttons and checkboxes with FancyBoxes.