Announcing Uniform
![]()
Update: View the new Uniform site
As a UI designer, I have to deal with form controls quite a bit. I hate how I can’t make selects, checkboxes, and radio buttons look consistent in all the browsers, so I wrote a plugin to solve just that. Introducing Uniform, a plugin for jQuery that lets you style select, radio, and checkboxes however you desire.
It works by hiding the original form elements using opacity, and wrapping div and span tags around the elements. Then, as the different events take place, the plugin applies certain classes to the elements. I’ve seen this method used a few places on the web, but never as an accessible, easy-to-use jQuery plugin.
Uniform supports the major browsers other than IE6, which has an issue due to the way they draw their form controls. In the case of IE6, it just ignores the form elements, so you can still use the form the same way. IE7 works perfectly.
There is a demo here that you can use to see what you could do. The demo utilizes Progressive Enhancement to style the form controls, which means that Safari and Firefox show a nicer version of the controls, but you could style them however you like. I suggest using Progressive Enhancement or utilizing CSS sprites of some type.
This plugin is very 1.0, so don’t expect it to be 100% perfect. If you find a bug, please email me and i’ll take a look at it.
The code generated looks like this:
Selects:
<div class="[selectClass]">
<span>Currently Selected Text</span>
<select>
[options]
</select>
</div>
Radios/Checkboxes:
<div class="[radio/checkboxClass]">
<span class="[checked]">
<input type="radio/checkbox" checked="checked" />
</span>
</div>
Usage is very simple:
$("select, :radio, :checkbox").uniform();
If you want to choose custom classes, the following parameters are available:
- selectClass: Class applied to the outer div of the select elements (default: selector)
- radioClass: Class applied to the outer div of the radio elements (default: radio)
- checkboxClass: Class applied to the outer div of the checkbox elements (default: checker)
- checkedClass: Class applied to the span of radio and checkbox elements if they are “checked” (default: checked)
- focusClass: Class applied to the outer div when the element is in focus (allowing for styling of the focus state for better keyboard accessbility) (default: focus)
Just input these parameters as you normally would with any other plugin:
$("select, :radio, :checkbox").uniform({selectClass: "mySelectClass", focusClass: "myFocusClass"});
I hope someone gets some use out of this plugin. Enjoy!
Much thanks to my great friends Thomas and Buck for all their support in creating this plugin.
ZIP Format, 132 kb



69 comments
Lachelle
Jun 8, 2009Looks great! I have a web application I’m working on that this will be useful for. I hate designing forms and this will take a lot of the work out while making the forms look great at the same time.
John Lascurettes
Jun 8, 2009Nice work, Josh. How does it handle the elusive type=“file” input? Safari is pretty different from the others in that regard.
(JavaScript must be enabled to view this email address)
Jun 8, 2009Lachelle: Glad to hear that. Please let me know what you think.
John: That’s a good one. I might add one of those in a future release if I can figure out how.
Peter
Jun 8, 2009Looks very interesting!
At first glance I noticed the rounded edges does not come out in Opera Version 9.63 on my PC; something we can live with. But with the radio buttons coming out square is a bit strange, I would probably use sprites instead of rounded corners in CSS.
Nice work
(JavaScript must be enabled to view this email address)
Jun 8, 2009Peter: Yeah, that’s the downside. I think in a production setting I would use sprites for radio buttons instead to keep up the illusion. The beauty is that you can style it however you’d like.
Quentin
Jun 8, 2009Lovely!
Wolf
Jun 8, 2009As much as I applaud the effort, I can’t help but cringe that this is the umpteenth attempt to this that completely breaks the defaults.
Try hitting the down, left or right arrows when focus is on the select (behavior is broken); try rapidly switching radio buttons (performance problems there)
(JavaScript must be enabled to view this email address)
Jun 8, 2009Wolf:
I think we can fix the arrow behavior. BTW, it works fine for me in Safari 4 to press the down arrow, but not the left or right.
The radio buttons could be improved as well I bet. I think the latency you’re noticing is because it’s checking on the mouseup event instead of mousedown.
Like I mentioned above, it’s a v1.0 effort at this point. I’ll just keep on making it better.
Driz
Jun 8, 2009One thing this plugin misses is the focus of elements. If you look at a native element when you push down on the mouse the state of the element will change. With this plugin nothing happens. WOULD be awesome if you could add this to the plugin. Thanks
(JavaScript must be enabled to view this email address)
Jun 8, 2009Driz: Yeah, I plan to add :active (mousedown) support (in the form of a class) in the next release. I completely agree with you and i’ve already heard a few people asking for this feature.
Dan Lewis
Jun 8, 2009That was quick! Thanks Josh :)
Driz
Jun 8, 2009@Josh Pyles
Sounds good. Look forward to it. Probably the best JS implementation of recreating form inputs yet. Have you noticed however that Apple’s MobileMe website doesn’t use JS for such things rather uses CSS alone to achieve a similar goal. Or am I off-base? Thanks
(JavaScript must be enabled to view this email address)
Jun 8, 2009Driz: I think MobileMe actually uses a similar method to this. It’s hard to tell. Also, I haven’t seen a <select> element in the app anywhere so I can’t really compare.
(JavaScript must be enabled to view this email address)
Jun 8, 2009Thanks for your work. I tried it with Firefox 3.0.10 but not working
(JavaScript must be enabled to view this email address)
Jun 8, 2009Hey George, can you email me the code you are using? Maybe I can help troubleshoot. Look on the contact page for my email.
Wolf
Jun 9, 2009MobileMe uses a Javascript framework called Sproutcore, see http://www.sproutcore.com/demos/ and click “Sample controls” then controls.
@Josh Pyles there’s so much to handle.
For example, you have no idea whether the user’s set his browser settings to skip radio buttons and check boxes (an option in Firefox and in the accessibility settings in OSX I believe). And when someone is on Linux their key mappings might be entirely different.
I bet the webkit devs are hard at work to do some similar as they did for scrollbars ( http://webkit.org/blog/363/styling-scrollbars/ ) for all input elements.
Some of the beauty of the web is that it mostly behaves as expected (first usability rule). The creators of the unobtrusive CSS framework ( http://unobtrusivecss.com/ ) definitely have a point somewhere.
Still, if you can get this thing perfect, I’m a fan.
George Wiscombe
Jun 9, 2009Looks brilliant, just what I was after!
rady
Jun 9, 2009It looks nice,thanks
(JavaScript must be enabled to view this email address)
Jun 9, 2009Wolf:
I agree with you. It’s a complicated thing. What i’m trying to do here is to use all the native functions off the original controls and just apply styles to the other elements. That’s what’s different here than a plugin to recreate a certain element.
The original inputs are actually hidden with opacity so they are still in action, and all the events still go to them. It’s just a matter of getting the correct hooks in.
I appreciate your feedback. Seriously, this is good. I definitely care about accessibility.
Thanks to everyone for your comments as well!
webmasterdubai
Jun 10, 2009great job will i works for text fields also or only for radios, checkbox and select box.
(JavaScript must be enabled to view this email address)
Jun 10, 2009webmaster: It doesn’t work for text fields because they are already highly customizable, therefore doesn’t need this plugin.
Nikolas
Jun 10, 2009Great!
Sébastien
Jun 10, 2009For sure I’ll use it in my next projects.
Good work! Tks!
Doug S.
Jun 10, 2009I concur on the sprites request. Having it as a single GIF, JPEG or PNG would be best.
Also, support for buttons would be extremely handy.
And, one last support request: Form Reset. If you add a reset button to the form and press it the values don’t change visually. I’d really like this functionality, no one else has it. You could likely add it by a simple listener for any input with a type of “reset”.
(JavaScript must be enabled to view this email address)
Jun 10, 2009Doug:
I might do a demo using sprites later. You can style it however you like though.
Support for buttons is something i’ve been thinking about. Taking <input type=“submit|button”/> or <button> elements and replacing them with <a> tags for backwards compatibility. I’ll consider it for the future.
That’s a good point. I’ll have to take care of that in the next version as well. Thanks!
(JavaScript must be enabled to view this email address)
Jun 11, 2009Is it me or…
...does not work in IE6
...ok on Firefox 3 & Chrome 2
(JavaScript must be enabled to view this email address)
Jun 11, 2009are you kidding me…
The idea of unifom forms is something i dream about. But either the name you have chosen is not apropriate or you can’t be bothered creating a demo that uses your own idea to its fullest.
Now this sounds like I’m dissing someone who is giving away their hard work for the benefit of all…but i will try this hoping that it really does work…and if it does I’ll thank you for creating this.
I just wonder why the article says it does something [uniform visual appearance]...but the demo is clearly different in ie7
(JavaScript must be enabled to view this email address)
Jun 11, 2009Alvin: Due to technical problems, it’s not possible in IE6. Works in IE7+ though.
Maak: I know the corners aren’t rounded in IE7. The point is that you can style them however you want. I chose a basic approach for this iteration, but the next demo I make will probably be more advanced. The idea isn’t that it has to look pixel perfect in every browser, it’s that they have a similar style in all the browsers. I’ll do a more advanced demo for 1.1
(JavaScript must be enabled to view this email address)
Jun 14, 2009Oh..So you CAN make it pixel perfect in the main browsers if you want using css?...cause thats what I’m after.
My dream is one sprite for all images [ala flash component] JS and transportable html that will work with forms in 1,2,or 3 columns with the real html form elements still used. currerntly I’m making custom dropdowns and checkboxexs which i dont like
(JavaScript must be enabled to view this email address)
Jun 15, 2009Maak: That’s the idea. Uniform lets you decide how you want to style your selects, radios, and checkboxes. You can use sprites, complicated styles, minimal styles, whatever you feel like.
Nick
Jun 16, 2009This works very well except in more complicated scenarios that include <optgroup> tags, which result in no text being shown, even when a selection has been made.
Nick
Jun 16, 2009Also, in case there are multiple radio selectors in your form would be better to use
div.radio span input[checked=“checked”]
instead of
div.radio span.checked
in the css to highlight them, as your js at the moment only adds the .checked class to the most recently clicked one.
(JavaScript must be enabled to view this email address)
Jun 16, 2009Nick: Yeah, I realized that issue late last week. I’m going to make sure it’s solved in the next version.
(JavaScript must be enabled to view this email address)
Jun 18, 2009nice work Josh!
really looks awesome!
In firefox, it is working perfectly.
And combo-box is not working in IE8 (may be IE8 is in beta) but in IE7 though the functionalities are working fine but the radio button’s shape is square not circular.
(JavaScript must be enabled to view this email address)
Jun 18, 2009The idea of degrading gracefully is implemented rather good; but since the web experience is about (download) speed, too, I would like to see the images reduced to 1 image in the next version.
Refael Ackermann
Jun 18, 2009Check out a multiple browser automatic tester:http://bit.ly/Or1ru
As you can see not rounded corner on IE 7
(JavaScript must be enabled to view this email address)
Jun 27, 2009Hi Josh
It is good .But In IE7 The Radio buttons are appearing as square.Strange
It should be round.
Eugene Bond
Jun 30, 2009Fast patch to resolve “radio buttons” issue:
//box was just checked, check span
$(”.”+options.radioClass+” span.”+options.checkedClass).each(function() {
var $e = $(this), $n = $e.find(“input”).attr(“name”), $sn = $(elem).attr(“name”);
if ($n == $sn) {
$e.removeClass(options.checkedClass);
}
});
spanTag.addClass(options.checkedClass);
David Kaneda
Jul 9, 2009Took the patch Eugene posted and modified to be part of the same selector as the line above, should be a little faster — might still be a bit messy, and just mildly tested, but appears to work.
//box was just checked, check span
$(”.”+options.radioClass + ” span.”+options.checkedClass + “:has([name=’” + $(elem).attr(‘name’) + “‘])”).removeClass(options.checkedClass);
Also found a few points for optimizing while digging around - is there a preferred way to send patches? Thanks-
(JavaScript must be enabled to view this email address)
Jul 9, 2009David & Eugene: Thanks a bunch guys! You could email me the patches as well. If you’re into GitHub you could also fork the project with your mods and let me know you fixed it.
New version will be coming soon and i’ll make sure to give credit where it’s due :)
(JavaScript must be enabled to view this email address)
Jul 15, 2009You know there’s a JQuery Uniform already out there right :-)
Funny you named yours the same. Nice Work!
(JavaScript must be enabled to view this email address)
Jul 29, 2009How about an API to get/set various values and options?
Something similar to Giva Labs’ Linkselect plugin: http://www.givainc.com/labs/linkselect_jquery_plugin.htm
(JavaScript must be enabled to view this email address)
Jul 29, 2009Aw, I came here to post a patch for the radio issue too but was beaten (twice)!
Anyway, not that it matters, but here is another version if the ones above don’t work for some reason:
$(“input[name=’”+$(elem).attr(‘name’)+”’]”).parent().removeClass(options.checkedClass);
Great plugin, much love to it and you Josh. Thanks!
balkan düğünü
Sep 16, 2009THANKS YOU.VERY GOOD ARTİCLE.
(JavaScript must be enabled to view this email address)
Sep 28, 2009Really great work. But, i wonder, for me the select boxes does not work in ie8 -> option will not open.
Medena
Oct 29, 2009Thx, thats looks great, because i have many ugly forms and no time to style.
Ming
Oct 30, 2009I really like your tutorial, thanks a lot
gagarine
Nov 8, 2009If you use display none or visibility hidden instead of opacity it would be work on IE6 no?
dr.emi
Nov 10, 2009awesome tricks! I like your comment form overlay too
great job!
gtraxx
Nov 10, 2009Hi bug with optgroup select a tag, the menu displays the data but once selected the div is empty
Jonas
Nov 18, 2009Great stuff my friend! Thanks a lot! this post saved my evening =:)
bigm75
Nov 23, 2009nice plugin
(JavaScript must be enabled to view this email address)
Nov 23, 2009Great Uniform plugin. I am however having problems with multi-radio groups. I can see somebody has come up with a fix but I can’t seem to get it to work. I have been adding the code snippets (all 3) but to no avail. Can you please help me with where I am going wrong?
I replace the code with the new code but the whole plugin breaks down.
I replaced:
//box was just checked, check span
$(”.”+options.radioClass+” span.”+options.checkedClass).removeClass(options.checkedClass);
spanTag.addClass(options.checkedClass);
}
with
//box was just checked, check span
$(”.”+options.radioClass + ” span.”+options.checkedClass + “:has([name=’” + $(elem).attr(‘name’) + “‘])”).removeClass(options.checkedClass);
Where am I going wrong.
Many Thanks
(JavaScript must be enabled to view this email address)
Dec 1, 2009Hello !
About problems with multi-radio groups.
$(”.”+options.radioClass+” span.”+options.checkedClass).removeClass(options.checkedClass);
is replaced with :
var nb_checked = $(”.”+options.radioClass+” span.”+options.checkedClass).size();
for (i=0; i< nb_checked; i++){
var x = $(”.”+options.radioClass+” span.”+options.checkedClass).find(“input”).eq(i).attr(“name”);
if(x==name){
$(”.”+options.radioClass+” span.”+options.checkedClass).eq(i).removeClass(options.checkedClass);
}
}
(JavaScript must be enabled to view this email address)
Dec 14, 2009If wanted to restrict the height of the drop down, how would I go about doing that? Currently, if I apply a height value to the select element, it adds a space in the value amount between the dropdown and the currently selected box.
(JavaScript must be enabled to view this email address)
Dec 14, 2009Hey Strife, that’s a good question. Do you mean change the height of the menu that comes down? I’m not sure how to solve that.
If you just mean the size of the select element you’d need to style the uniform class rather than the select itself.
(JavaScript must be enabled to view this email address)
Dec 14, 2009I was referring to the menu that drops down. I’ve tested it in various browsers and it seems as though it’s using the browsers default dropdown menu.
(JavaScript must be enabled to view this email address)
Dec 23, 2009Just got a pile of IE7 errors this morning ... not sure what changed, but I found that IE7 really wants the selectors to be very specific (wtf?)
So instead of the more general method—
$(“select, :radio, :checkbox”).uniform();
I found I could fix the errors like this—
$(“select, input:radio, input:checkbox”).uniform();
weird huh?
Tolga Kaprol
Jan 8, 2010@vince
It not worked since we added this:
name = $(this).attr(“name”)
(JavaScript must be enabled to view this email address)
Jan 11, 2010great plugin! ...but i need to change the select-elements and couldn’t find a way to change/remove the “<span>selected item text</span>” code… result: i can change the select-option but the selected-text still remains :-(
any ideas?
kastamonu
Feb 3, 2010Thanks.Son dakika kastamonu haberleri
kalp ağrısı
Feb 3, 2010Ooo very good.thank ederim
:joe.
Feb 23, 2010What about styling input fields and textareas?
Gavin
Feb 24, 2010:joe. - looks like styling for text & textarea fields is in the works! http://github.com/pixelmatrix/uniform/issues#issue/6
(JavaScript must be enabled to view this email address)
Feb 26, 2010Please help me, I will crazy about browser differences. Take a look the following screen shot :
IE8 - http://www.flickr.com/photos/43217118@N08/4388815505
FF3 - http://www.flickr.com/photos/43217118@N08/4388815531
The uniform works perfect in FF3 however doesn’t working in IE8.
So, what I am doing wrong?
(JavaScript must be enabled to view this email address)
Feb 26, 2010Puffff…
I solved the problem.
We should change the following css properties to fix IE8 issue :
div.radio {
position: relative;
float: left;
clear: left; <—I added this property
}
(JavaScript must be enabled to view this email address)
Feb 28, 2010Nice Plugin, But how to implement this plugin not working any demo or full html for download thanks
(JavaScript must be enabled to view this email address)
Mar 9, 2010maybe the file field should represented with a div for displaying the filename and a hidden field for the data. Right now when you click on it it displays the text cursor a bit over the field which doesn’t look too good.
The select element should have a bit air from the left. Haven’t tried this tho, there is a scrollbar on demand if the options get crowded right?
(JavaScript must be enabled to view this email address)
Mar 9, 2010doesn’t work under IE8, I followed exactly your site example. Could we just get a SIMPLE html file so to see what we do wrong?