What does css do in javascript?

CSS-in-JS libraries provide us with a new approach for writing CSS. They aim to tackle the limitations of CSS, such as the lack of dynamic functionality, scoping, and portability. In spite of its advantages, CSS-in-JS is a controversial technology, as many developers ask if it’s worth further complicating front-end development. 

This article intends to give you a high-level overview of CSS-in-JS libraries. We’ll look into how these libraries work, which problems they solve, and how to decide if you should use them.

What Is CSS-in-JS?

CSS-in-JS libraries have been gaining traction since component-based JavaScript frameworks appeared in front-end development. Angular, React, Vue, and other frameworks are all based on modules called “components” from which you can build up an entire single-page application (SPA). A component is usually a UI element such as a button, pop-up, or navigation bar. You only need to create a component once and you can reuse it in any context within the application. 

But, how should you style SPA components efficiently? If you use global CSS files then it will be hard to tell what the end result will look like. Due to the cascading nature of CSS (Cascading Style Sheets), stylesheets can load in any order and override each other in any combination. Managing dependencies is another problem when it comes to styling SPAs. If dependency management is difficult when working with regular websites, it’ll be almost impossible with a modular web app above a certain complexity.

A well-designed CSS-in-JS library can solve all of these problems. CSS-in-JS is, in fact, a JavaScript library that bundles each JavaScript component with all its belonging CSS rules and dependencies. As a result, components can run independently, without relying on any external CSS file. This is how Max Stoiber, the co-creator of the Styled Components library explains in his excellent blog post how CSS-in-JS libraries work:

“For three years, I have styled my web apps without any .css files. Instead, I have written all the CSS in JavaScript. ... I can add, change and delete CSS without any unexpected consequences. My changes to the styling of a component will not affect anything else. If I delete a component, I delete its CSS too. No more append-only stylesheets!” – Max Stoiber

I think this statement summarizes the technique pretty well. With CSS-in-JS libraries, you write all your CSS within .js files so that they can work as JavaScript modules.

Examples of CSS-in-JS Libraries

Although every CSS-in-JS library serves the same purpose (i.e. bundling components with CSS), each comes with slightly different features and syntax. Let’s see how the most popular CSS-in-JS libraries stack up to each other.

Framework-Specific vs. Framework-Agnostic

Some libraries only work with a specific JavaScript framework. For example, Radium has been created for React apps, while Styled JSX only supports components written in JSX (that you can use both with and without React). 

Framework-specific CSS-in-JS libraries use the same syntax as the framework they support. For instance, Styled JSX uses template literals within the JSX syntax to add CSS styles to components. The following code (from Styled JSX’s GitHub docs) creates two types of buttons, a regular and a large one:

const Button = (props) => (
  
)

/* Creates a regular button */


/* Creates a large button */

Other CSS-in-JS libraries such as JSS, Emotion, and the aforementioned Styled Components are framework-agnostic. So, you can use them together with any component-based frameworks, plain JavaScript, and some of them such as Aphrodite work with Web Components as well.

What does css do in javascript?
What does css do in javascript?
What does css do in javascript?

To see how framework-agnostic CSS-in-JS libraries look in real life, you can try JSS’ online playgrounds or check out the following Web Component example from Aphrodite’s GitHub docs:

/* Registers a Web Component with red background */
import { StyleSheet, css } from 'aphrodite';

const styles = StyleSheet.create({
    red: {
        backgroundColor: 'red'
    }
});

class App extends HTMLElement {
    attachedCallback() {
        this.innerHTML = `
            
This is red.
`; } } document.registerElement('my-app', App);

Unique Selectors vs. Inline Styles

To handle scoping, most CSS-in-JS libraries such as JSS, Emotion, and Styled Components automatically generate a unique selector for each component. To see it for yourself, you can go to Styled Component’s homepage and inspect one of the examples with your browser’s developer tools. For example, I’ve inspected the pink-ish "I'm a styled