1

JavaScript is more dynamic than CSS. But how is JavaScript more dynamic than CSS? How does JavaScript coding work in a better way than CSS?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
luxi
  • 317
  • 1
  • 4
  • 17
  • 2
    JavaScript can manipulate the information/content of your document. CSS can only manipulate the rendering of your document. – Zeta Apr 14 '13 at 10:37
  • Javascript can manipulate de DOM tree at any time (insert/delete/update nodes) and CSS only apply rules to selectors/tags on rendering. – tiagojpdias Apr 14 '13 at 10:42
  • @Zeta, @ tiagojpdias - That was true some years ago, now we can content manipulation from CSS. Also a popin menu is more about layout then content so i think CSS-solutions are neater. – krs Apr 14 '13 at 11:00

5 Answers5

3

It's two different things. CSS is a stylesheet language while JavaScript is a programming language. It's really like comparing apples and oranges. (Actually, the distinction isn't all that obvious on a technical level, as apparently CSS is even Turing complete, but as most people practically use the term "programming language", they're different.)

But I guess you can argue that JavaScript is more dynamic, in that it is a programming language that can dynamically change both the content and the presentation of your page. You can actually add and remove CSS rules dynamically, in run-time, with JavaScript. You can also alter the attributes of the DOM elements in your page, like classes, IDs etc.

Community
  • 1
  • 1
Hannes Johansson
  • 1,794
  • 2
  • 15
  • 28
2

Yeah, CSS is not as dynamic as JavaScript, and the new dynamic selectors do not work in older browers. Then again people won't upgrade if nothing breaks, so go with a pure CSS solution =)

Toggleable menus done with pure CSS usually uses the :selected dynamic selector and styles it with some visible menu element to have it only visible when a hidden checkbox is checked.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
krs
  • 4,096
  • 19
  • 22
1

While JavaScript can be more flexible because it's a programming language, CSS can perform better because it's built in to the browser. JavaScript can emulate many of the same CSS properties, but CSS will have better performance. It can do things like animation smoother for example.

Many times people will write JavaScript code that sets CSS properties to let CSS do the rendering for that reason.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rob
  • 14,746
  • 28
  • 47
  • 65
0

JavaScript not only stops at making cool design, it does a lot of other things directly to HTML and CSS will only help you with page design things and CSS can't replace your website's action maker, Javascript. I mean... imagine a CSS drop-down menu, haha.

Good question though.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mr. X
  • 26
  • 4
0

There's significant overlap between their capacities.

Pure CSS3 can be used to build an attractive and responsive menu bar with drop-downs. And you can do the same in pure Javascript. But you may find that HTML+CSS is more expressive in creating layout+style.

Javascript will come in handy when you have to dynamically generate content on the client side (e.g. editor and simulators that run in-browser). You then may find it easier to do (practically) everything in Javascript.