Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery plugin - inline CSS or external stylesheet?

I'm coding a jquery plugin and I'm thinking whether I should move all the CSS in the .js file, just to make it a little bit easier to setup.

Should I do this? Is it too bad, performance wise?

like image 507
Nikolay Dyankov Avatar asked Jan 11 '12 07:01

Nikolay Dyankov


5 Answers

What is most common is to use:

jquery.myplugin.js
jquery.myplugin.css

and deliver these together to anyone using the plugin. It is an extra request but it's practicing separation of concerns: styling and logic.

like image 76
c4urself Avatar answered Oct 21 '22 22:10

c4urself


Don't do this and use a CSS file for layout and feel purposes. Loose coupling is a great asset. This will ensure that any developer with CSS knowledge can change the look and layout in the future easily without fiddling with the javascript code.

Layout and looks are altogether a different concern. Merging it with .js file will always require a javascript coder just for mere change to the background or dimensions of your element.

Performance wise, there won't be any difference to the eye. But, development wise, you will have nightmare.

It's a pure no-no IMHO

like image 31
Pankaj Upadhyay Avatar answered Oct 21 '22 23:10

Pankaj Upadhyay


there is no performance degrade or anything like that, just whether it's more readable and maintainable for the person using your plugin.

like image 45
XepterX Avatar answered Oct 22 '22 00:10

XepterX


Why do you want to move the CSS to the JS file? It's fairly easy to link a CSS file in the HTML.

I usually prefer having the CSS and the JS separate from each other so as to make it easier to maintain and modify if required. It could be potentially messy to modify the CSS if the styles are located in the JS file itself.

Having the CSS loaded separately also allows the styles to get rendered across the HTML, even if there is a problem loading the JS.

To reduce the size, you could also minify the JS and the CSS files, if performance is a concern - http://code.google.com/p/minify/

More info on the YUI compressor - http://developer.yahoo.com/yui/compressor/

like image 1
Sagar Patil Avatar answered Oct 21 '22 22:10

Sagar Patil


just make css editable, the className and id are good for you, the css selector, you know, sample

style:

.graceful {width:100px;height:100px;background:#36c;}

script:

var div = document.createElement('div');
    div.className = 'graceful';
    div.innerHTML = ' ';
    document.getElementById('_images').appendChild(div)
like image 1
Wally Qiao Avatar answered Oct 22 '22 00:10

Wally Qiao