Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
Good point that no matter what, styles are always configurable. –  ryanzec Jan 1 '12 at 1:03
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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