CSS-Mania vs. The JavaScript Frenzy

Nick

April 30, 2020

CSS-Mania Vs. The JavaScript Frenzy

We could all acknowledge the intimacy that exists between HTML and CSS. However, there is JavaScript — to enhance our overall frontend functionality. Though CSS (considered more a frontend paradigm than a language) and JavaScript, both complement each other in one way or another. Still, performance is of the utmost importance when comparing such functionalities as animations across these two technologies (especially responsive-wise), among other things. Notwithstanding, CSS leads the race by a considerable margin over JavaScript in almost all areas (a debatable fact though). Mainly because CSS offloads animation logic into the browser itself, and as a result, has a heightened performance level. Since it uses the GPU to improve performance by letting the browser optimise DOM interaction and memory consumption.

Nevertheless, when we talk about using CSS or JavaScript for layouts, we get two significant schools of thought. And according to Christian Heilmann, these include;

·       Those who see CSS as a thing of the past (even though its invention was historically more recent than JavaScript). Thus, in a module-driven world such as the one we find ourselves in, we should do all things in scripting space.

·       And then some see CSS and its preprocessors & build scripts as more than enough to do everything.

These two philosophies bring us back to the memories of the good old days when we had backlash —  "the CSS-centric solution" that allowed us to simulate sophisticated interactivity through checkboxes and completely eradicated the usage of JavaScript. As well as the DHTML, which was an absolute replacement to the HTML-CSS-JavaScript chain with a much more subtle approach that eliminated the CSS layer. Yet, the question one would often ask is; Why pay so much assiduity to JavaScript programming as a frontend tool if CSS was naturally this good? Well, the answer to that question solely depends on several things that I have carefully highlighted in the following sections of this article.

Performance & Optimisation

First and foremost, CSS parses and renders faster, while most JavaScript libraries are notoriously slow and highly unreliable if not handled by an adept programmer. Whereas a broken CSS rule doesn’t affect the overall performance of your web app. Even though a missing tag could break the layout, it seldom stops your page from loading (except in some extreme cases). The CSS parser uses an approach of ignoring any property; it doesn’t seem to understand. Hence it provides developers with one of the most important tools for code optimisation — which is backward compatibility. That said, with CSS, things do not look perfect, but they work.

And because CSS is declarative enough, there is far more room for optimisation since its operability is enhanced by hardware acceleration. But with JavaScript, you may have to optimise performance, which is completely dependent on the library being used.

Browser Compatibility

JavaScript is not fault-tolerant, which can be disastrous in most cases. Because of being much more in control when using JavaScript, you become even more responsible for a number of things. Considering you are expected to create a specific rule to handle all the supported browser types across a wide range of devices. We could take an example of setting the background-color and gradient on a CSS element (e.g., a button). While the gradient is slated for modern browser versions, the background-color shows up on older versions as a replacement to the gradient. This gives CSS a more extensive outreach with nearly zero effort in terms of browser compatibility when compared to JavaScript.

With CSS, you don’t necessarily care about the environment, and thus the coding techniques relative to a specific environment become less of your responsibility. The OS, browser, and proxies involved make these decisions for you.

External Libraries

Unlike CSS, JavaScript is a scripting language; therefore, its extensive use of libraries. Hence, its performance depends on the library in use, as certain libraries improve performance, while others impede its effectiveness. But most JavaScript libraries such as jQuery, add an extra overhead which increases page load-time, especially for mobile devices.

Although JavaScript proffers lightweight libraries with a wide range of browser compatibility to handle load-time overheads. Yet, these featherlight libraries are fraught with reduced utilities (with less serviceability compared to CSS) than are usually inherent in core JavaScript libraries. Also, because using JavaScript requires the selection of a well-suited library that does not dispute other libraries you already have in place. This makes the performance of JavaScript, and it’s libraries the second-best solution when it comes to a faster load-time.

Animation

JavaScript is a very robust, scripting language for animations. Still, it is completely unnecessary if what you aim to animate is something as necessary as fading in a pop-up window or creating a hover effect. Most of the time, just using CSS is enough. Nonetheless, complicated animations can be difficult to execute without using JavaScript. Although a well-optimised JavaScript is often just as fast (sometimes fasters than CSS animations), while 3D transforms do tend to be faster when animated with CSS.

In summary;

·       Use JavaScript — If the element requires user interaction (such as initiating on-hover, on-focus, or on-clicking functions).

·       Use CSS — If the element needs to be animated, if it requires a change in visibility or has any other visual change made to it.

NOTE: If you decide to use JavaScript, make sure to pick a suitable library that does not conflict with other libraries already in use.

Why JavaScript?

Over the years, JavaScript has established itself as one of the cornerstone frontend technologies of the World Wide Web. Alongside HTML & CSS, this recent, much more powerful frontend tool (there is no denying that) has made the web a better place for us all. JavaScript is a functional programming paradigm, that does not only support computation as an evaluation of mathematical functions, It also endorses state-change and mutable data. This gives JavaScript an extra edge over traditional imperative programming criterion — where a function’s arguments can affect a function’s resulting value alongside a global program state. More so, JavaScript has an added advantage, unlike conventional functional programming language, because it is also event-driven. This makes it possible to track user interactions such as mouse clicks and key presses together with sensor outputs and messages from other programs or threads. For this reason, it has become the perfect benchmark for contemporary frontend development techniques. Hence the extensive usage of its frameworks by the topmost tech companies on the planet such as — React (Facebook, Instagram, Whatsapp, Codecademy, Dropbox), Angular (Youtube, Netflix. PayPal, Upwork), Vue (Google, Apple, Nintendo, Fontawesome, Gitlab), etc.

Why Not JavaScript?

To think that the involvement of JavaScript with the ’big boys’ in the tech industry makes it more or less indispensable is merely a flare. CSS utilises a declarative approach to programming. Given that, with CSS, you apply your styles and expect things to work no matter what, which is in contrast to JavaScript, where you have more granular control and subsequently have to verify that all of your tweaking actually worked. Makes CSS debug-friendly, hence an under-represented style formatting language. Although a year younger than JavaScript (established 1990), the credibility of CSS can never be over-emphasised.

After all, we recognise that the primary advantage of maintaining core CSS functionalities — such as CSS animations, CSS GRID & CSS state changes, lies majorly within the neighbourhood of a faster load-time. We do still acknowledge the importance of JavaScript. Even though I have already listed a handful of fortune 500 companies that use JavaScript, CSS still enjoys a considerable fan-base. And as it keeps evolving, it continually establishes a straightforward fact — no modern frontend technology or framework would be complete without CSS and its preprocessors. Period!

Conclusion

In conclusion, it becomes pertinent to note that whichever one amongst these two technologies can be highly effective if utilised by a multi-faceted and skilled web artisan. However, this definitive walkthrough is scripted for frontend developers within the junior-to-intermediate tiers. Who would typically love to use the 'mighty’ JavaScript? But alas, just found out about the hidden prowess of CSS.

However, I would encourage you to conduct personalised research to see which among these two technologies delivers the best performance as per your project needs.

Nick

Nick

Nick is our Marketing Owner and works from our UK office, he has a wealth of experience in digital & data marketing and is a Certified Scrum Product Owner.