This really depends on the amount of styling involved. If, for example, your plugin creates a widget with lots of content areas and buttons then yes, you should use a separate StyleSheet (see below). But if it's just a basic plugin with a couple of styles then there's really no point. E.g.
jQuery.fn.doSomething = function() {
// Only a couple of styles.. no point in a separate StyleSheet
return this.css({
color: 'red',
fontSize: '1.5em'
});
};
Also, if you are using a StyleSheet, then you might consider making the classes and IDs configurable, then you can load the StyleSheet via XHR and add the hooks. E.g.
plugin-styles.css
#[[pluginID]] {
color: red;
background: white;
border: 1px solid #000;
}
#[[pluginID]] a.[[pluginCLASS]] {
display: block;
border: 1px solid green;
}
/* etc. */
Your plugin:
jQuery.fn.myPlugin = (function(){
var pluginID = 'foo',
pluginCLASS = 'bar',
cssRequest = jQuery.ajax({
url: 'css.css',
async: false
});
jQuery('head').append(
'<style type="text/css">' +
cssRequest.responseText
.replace(/\[\[pluginID\]\]/g, pluginID)
.replace(/\[\[pluginCLASS\]\]/g, pluginCLASS)
+ '</style>'
);
return function() {
// Plugin functionality...
};
})();
The resulting CSS (which is added to the <style/>
element):
#foo {
color: red;
background: white;
border: 1px solid #000;
}
#foo a.bar {
display: block;
border: 1px solid green;
}
/* etc. */