--- author: "Thomas Wilson" title: "What is CSS-in-JS and why do people keep using it?" date: 2019-09-20 slug: "what-and-why-use-css-in-js" draft: false tags: - css-in-js - javascript - frontend --- The elders tell us of a time where you would build websites by literally writing your `.html` files. If you wanted to add some styles, you’d write them in a `.css` file and then `` the two together. So you’d write your entire site in semantic HTML, about whatever it is people did back then (like mammoth hunting or pyramid building) and then you would `ftp` that onto your server, and other people could view it. At least as long as they could fight off polio or resist rebelling against the bourgeoisie. Maybe you’d go out and celebrate at the tavern afterwards, but actually that one sounds pretty relevant today. ## Component-orientated architectures arrive In the modern (but still terrifying) world of 2019 - HTML, CSS, and JS remain three central tenants of the internet: HTML providing content, CSS styling it, and JS adding functionality on top of everything. The way we get the result of these technologies, HTTP, has remained pretty constant too. However, at some point people started to realise that writing _just_ HTML could make it difficult to edit parts of their website. Say you have a navigation header, and you want to add a new page to your website. Well now you need to go through every `.html` file, find the navigation header, and add an item. And what happens if you miss one file by accident? This is the problem which facilitated the evolution of component-centred architecture: a component being some pre-defined visual element, simply a “thing” on a website: a tweet in a list of tweets, a list of currently trending articles on BBC News, a user’s avatar image. This lead to building websites as a set of components which you compose together, as opposed to single monolithic pages. This practice evolved, and more responsibility was handed over to JS and other technologies. People started writing Single Page Applications (SPAs), and static-site generators increased in popularity - where we could define and update components in a single place, and see them updated across our site without having to delve into the nested HTML ourselves. It doesn’t matter if the site is actually just a single JS function or runtime which hooks into the browser’s URL (e.g. a SPA written in React or Vue) or if it generates a set of HTML files (a static site generator). Component-based architecture is the practice of breaking out our visual element by their role, not their location. Pairing components with external data sources, like in a CMS or a databases, means that a website does not need to be rebuilt or re-made whenever a new product is added to a store, or the price of an item changes. It has given a lot of power to people in the business, but who weren’t developers. And it’s freed up developer time to focus on more developer-y things, like complaining about meetings and fixing their parents’ wi-fi. ## Where does CSS come into this? Look, the point I’m making is that modern web development considers a web page as composed of many components, which can be shared across pages (and even sites). But the way we do styling with CSS didn’t immediately go through a similar revolution. In a lot of cases we were still left with one global CSS file, which we would simply `