1

Lets say I am building a jQuery plugin that has a number of non-configurable css attributes it needs to apply to DOM elements the plugin itself creates. Is it consider better practice to apply the css directly in the javascript code or is it better to create a css file and just include both the css and javascript files?

I have always been under the impression that is would be better to have the css file just because it seems a bit cleaner and you don't really gain much from having everthing in the javascript code. I mean the size of the css + javascript files would probably be about the same as if you had applied the css through javascript so the only thing you gain is that you only need to request 1 resource file instead of 2 if you apply the css in javascript. I am just curious as I an building a number of jQuery plugins and I have noticed some other plugins that are embedding a lot of css in the javascript instead of a css file.

2 Answers 2

2

Nothing style-wise is actually non-configurable. Someone using your plugin can always use a selector for the plugin and use !important when setting the style attribute.

Having said that, knowing that your CSS can be overridden, there's no reason to design around it and make it as difficult as possible for someone to alter it. Therefore, I'd say that separating your styles out into a .css file makes ultimate sense as it's easier for someone to alter your styles and it separates style from implementation.

1
  • Good point that no matter what, styles are always configurable.
    – ryanzec
    Commented Jan 1, 2012 at 1:03
0

As a user, I find it easier to manage jQuery plugins that are a single JavaScript file, rather than those that require additional CSS files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.