Aritra Hazra
Aritra Hazra

Reputation: 524

Difference between defining css via jquery and using external css file

I'm planning to make web GUI components which will be using jquery. so for modulity purpose it is safe to define component's styles inside jquery?

Upvotes: 1

Views: 33

Answers (2)

Apoorv
Apoorv

Reputation: 383

I recommend that you use external stylesheets because css and javascript are loaded simultaneously. Defining styles in the script will make both the operations slower as the operations will take place one by one rather than the same time.

Upvotes: 0

Lix
Lix

Reputation: 47966

Safe? Yes - recommended - probably not.

I would say that it is always better to use a modular approach and separate "functionality" appropriately.

Your JavaScript code should be inside .js files and your style definitions should be in .css files (or some other css preprocessor format). This way you know exactly where to find everything.

The instances where it would make sense to handle CSS within your JS would be if all the CSS styles of the elements change dynamically at run time or if you are required to only have one file per component in which case you should make sure to keep all of your CSS manipulations in one central location within the code - for example in a single function.


To summarize: It's probably not "safe" to handle all style definitions within your JavaScript purely because it might cause confusion for other/future developers.

Upvotes: 2

Related Questions