tags:

views:

109

answers:

5

I'm relatively new to client-side programming coming from the PHP/MySQL environment. I understand the roles both CSS and JavaScript can play in the browser environment, however, it appears CSS is irreversibly stagnant without JavaScript. I by no means want to create a debate but this is what the situation looks like to me, the "novice." So why not just use only JavaScript to set element attributes/properties? And if so, is this a common practice? (I'm sure CSS is much faster...)

+5  A: 

Css is for page layout and style.

Javascript is for behavior.

Even if it is possible to completely replace css with javascript, it's not considered standard practice and would be frowned upon severely by most web developers.

Good web development always assumes that a client may have javascript turned off and will provide for a graceful default setting. Replacing css with javascript may make this impossible.

Terry Donaghe
+4  A: 

It is far from a common practice, in fact it would probably be viewed as a bad practice!

CSS is used to set the styles of the page and it is rendered when the page loads. Javascript comes into play after the page loads and can manipulate the existing styles and page.

If you were to put it into all JS it would be hugely inefficient! DOM manipulation gets expensive if you do it a lot in JS, and doing all styles in javascript instead of CSS would be lots of manipulation. The load on the client would be ridiculous and there would probably be a noticeable lag in the client as it tried to make all those changes. Plus what happens if a client has javascript disabled? There goes your entire site's look and feel.

Think of CSS as defining how the page looks and should be rendered, and then think of JS as changing that page after it's done rendering. You shouldn't push anything into a javascript that you can do with a simple CSS style up-front.

Parrots
+2  A: 

The only time you should use JavaScript for setting style properties is when you need to change the style at runtime. You should always use CSS for styling where possible because:

  • CSS tends to be implemented a lot more consistently across browsers than JS
  • A user may have JS disabled
  • The "code" to do styling in CSS is a lot terser, and easier to read/write than the equivalent JS code
Don
+3  A: 

The real problems with changing out your CSS for javascript would be maintainability and performance. CSS makes it very simple to find a single elements styling, and change it, without effecting the rest of your page. Javascript would become cumbersome at best for even a simple page, and completely unmanageable for a more complex page.

On the performance side, any time javascript is executing, your page will "freeze". Considering a page with 1000 elements needing laid out, your execution time might easily grow to a minute or more, depending on the complexity of the layout. CSS would handle this natively, and without any "freezing" of your browser. This would cause your page to be unusable for the first bit of time that a person visits, and (according to statistics) you only have 10 seconds to capture the attention of your viewer before they become disinterested in your page and leave, meaning you drive away all your new visitors.

md5sum
+8  A: 

Some general points:

CPU Cost

Running Javascript to apply styles to individual elements will incredibly slow. Javascript is synchronous, so it'll have to update one style at a time. Plus, as mentioned elsewhere, traversing the DOM is computationally expensive. More so if you're applying styles since you're forcing the browser to re-render the site each time you apply a change.

Brain Cost

It's also mentally expensive to try to write and maintain styles in Javascript. It's a functional language never intended to contains the rules of layouts. CSS is just a lot easier to read.

They Cascade!

CSS stands for Cascading Style Sheets. One of the great benefits styles can inherit properties from eachother. Consider the following:

a.nav { font-weight: bold; }

Now all your links with a class of "nav" are bold. But should you wish to further modify a link you'll still be able to:

li a.nav { color: red; }

Now all your a.nav links contained within a list item will be red and bold. It's possible to do this is javascript, but you'd have to force it and maintenance would be horrible.

If you use Javascript for styles your coworkers will beat you to death!

I think this one kind of speaks for itself

Mike Robinson