1
  1. We can use inline styles in HTML, and we can also use <script> to write JavaScript within the HTML document or we can inline it, too.
  2. In JavaScript, we can work with the DOM elements via selectors such as document.querySelectorAll(), and we can also modify their appearance through classes (e.g. className) or inline styles.
  3. In CSS, we can target the DOM elements via CSS selectors. No JavaScript. CSS preprocessors are helpful in making our CSS look a little bit more like JavaScript (variables, mixins, functions), but that code lives in its own sandbox and our scripts do not have access to it.

Were there ever any plans to support JavaScript within CSS? What are the arguments for and against this?

lmenus
  • 604
  • 9
  • 27
  • This reminds me of [http://webdesign.tutsplus.com/articles/css4-is-coming-what-you-need-to-know--webdesign-11521](http://webdesign.tutsplus.com/articles/css4-is-coming-what-you-need-to-know--webdesign-11521) – enifeder Apr 28 '16 at 01:56
  • I want you to imagine a world where your browser hangs, and after a long time, the automatic process handler pops up an alert. In that alert is the message: "Uncaught Out of Memory Error: Ran out of memory while attempting to allocate space for '$redButton' on line 7936 of styles.css. The stacktrace is below." – rockerest Apr 28 '16 at 02:23
  • Why would you introduce JS to CSS when you can operate on the DOM (do styling) with JS? – vol7ron Apr 28 '16 at 02:32
  • @rockerest There's no need to imagine, just fire up IE :) Although, you can actually use variables in CSS: https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables – wilsonzlin Apr 28 '16 at 02:40
  • @lerouche indeed. And variables are actually an exciting addition (despite the syntax). I'm thinking more of a calculation in JS that runs an infinite loop, or heavy interactions that causes the browser to run out of memory just trying to decide on the value to assign to a color in your stylesheet. Ew. CSS performs it's job admirably, given the requirements it has, and I'm sure that adding JS to it is not a good idea, and I'm confident we agree on that ☺ – rockerest Apr 28 '16 at 02:44
  • @rockerest Oh no, I definitely agree with you; in fact, I believe Chrome dropped CSS variables support for a while due to performance concerns (and still doesn't support scoped styling), so you can be rest assured CSS is extremely fast and will remain so as a top priority. CSS is probably the only thing you don't need to optimise for while programming in it... – wilsonzlin Apr 28 '16 at 03:09
  • @lerouche not exactly true. You should optimize your CSS. While the engine is generally lightweight and quick, poor styling can result in very slow pages. – vol7ron Apr 28 '16 at 03:35

1 Answers1

1

No. In very old versions of IE you could use CSS expressions, but the end result is that performance is slow, optimisations can't be done, and it's dangerous. Mixing two languages that are almost completely different is also bad.

Here's an answer that explains more about CSS expressions: https://stackoverflow.com/a/10910098/6249022

Community
  • 1
  • 1
wilsonzlin
  • 2,154
  • 1
  • 12
  • 22