Styling jQuery plugins... you're doing it wrong!

Printer-friendly versionPrinter-friendly versionSend to friendSend to friend

I love jQuery, it's an amazingly powerful tool but there is one thing that always infuriates me whenever I come to installing a new plugin - inline styles!

Yesterday I spent the day adding a Simple Gallery photo gallery to a clients site. All went surprisingly well considering the site and the gallery was plugged in successfully, the last thing I had to fix was the fact that all the images appeared to be left aligned and I wanted them centered to avoid a nasty black border.

Simple you would think? text-align: center or margin: 0 auto; should do the trick, and it would have done if the position of the graphic wasn't set in an inline style. I have come across a few ways of dealing with inline styles, some argue that adding !important to a CSS rule should work but I have had limited success using this technique - but when it does work it works across all browsers. The other method I discovered recently was an article by Chris Coyier posted on css-tricks.com. This one seems to work more often, but doesn't work on IE6 (something I couldn't care less about for personal sites, but more corporate clients list IE6 compatibility as a requirement).

In my opinion the whole thing could be made much easier if a little more thought was put in when developing these jQuery plugins. I will use Simple Gallery as an example here (I'm not picking on Simple Gallery for any reason in particular, its just been my most recent experience of this problem!)

Take this line of code:

setting.$wrapperdiv=$('#'+setting.wrapperid).css({position:'relative', visibility:'visible', background:'black', overflow:'hidden', width:setting.dimensions[0], height:setting.dimensions[1]}).

Now heres the same thing, but much more customisable and developer friendly:

//jQuery
setting.$wrapper=$('#'+setting.wrapperid).addClass('class-name');

//CSS
.class-name {
  position:relative;
  visibility: visible;
  background: black;
  overflow: hidden;
  width: 200px;
  height: 200px;
}

Much nicer, the cleaned up version shows a much better separation of display and mark up, plus its easier to override and blend in with your site styles. The height and width settings for simple gallery are actually passed in when you create a new instance of it, I can't help thinking that this is wrong too hence the above CSS that sets it in (what I feel) is its proper place.

If jQuery plugin devs spent a little extra effort seperating their styles from their jQuery code and supplied a well commented base CSS file for their plugins, I for one would be a much happier dev!

End rant! :)

Comments

jCarousel doing it right

I added jCarousel to a site recently - and I'm pleased to say that they're doing it right!
A nice external CSS file and no inline CSS.
http://sorgalla.com/jcarousel/

manarth.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.
  • You may embed videos from the following providers . Just add the video URL to your textarea in the place where you would like the video to appear, i.e. http://www.youtube.com/watch?v=pw0jmvdh.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Insert Flickr images: [flickr-photo:id=230452326,size=s] or [flickr-photoset:id=72157594262419167,size=m].
  • You may use <swf file="song.mp3"> to display Flash files inline
  • Twitter-style @usersnames are linked to their Twitter account pages.
  • Twitter-style #hashtags are linked to search.twitter.com.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.