We have seen CSS-in-JS become an essential part of modern frontend development in the past few years. According to styled-components creator Max Stoiber, around 60 percent of React installs also install a CSS-in-JS library. The adventure that began with JSS in November 2014 is now relatively stable, with styled-components acquiring a large chunk of the CSS-in-JS market.
Here, we will be sharing some cool alternatives to styled-components that provide great value and could become the CSS-in-JS library for your next app.
Maintainers | Bundle size | npm downloads |
Callstack | 510B | > 19k |
One of the drawbacks of CSS-in-JS libraries is the runtime cost, as most of them add and remove styles from the <style>
tag when DOM elements are changed.
Linaria solves this issue by extracting all CSS in files during the build. Another fabulous feature is that all dynamic styles are applied using CSS variables, which lead to total independence from any runtime.
That does come at a cost, however — dynamic styling is not available in browsers that don’t support CSS variables. Linaria also supports Sass-like syntax for nested styles.
As for developer experience, it supports stylelint and also provides a CSS source map for a seamless debugging experience. It also has a Babel loader with a webpack guide, a Rollup plugin, and Gatsby, Svelte, and Preact guides.
If you’re planning to move from styled-components, Linaria has a styled helper, making it easy to switch. linaria/react also supports dynamic styles with a styled-components-like syntax.
Maintainers | Bundle size | npm downloads |
510B | — |
One of the core challenges for styling libraries today to find the best tradeoff between performance and maintainability. CSS Blocks envisions to give the best of both. CSS Blocks is inspired by CSS Modules, BEM, and Atomic CSS.
Best of all, CSS Blocks is statically analyzable. It can look at your codebase and analyze which parts of your CSS are used, unused, or conditionally used. It divides all of the rules in CSS into unique groups without repetitions. It allows you to keep your CSS more maintainable for yourself and other developers and better optimized for your end users.
CSS Blocks offers a new model as compared to styled-components or similar styling libraries. Some teams may take some time to learn and adapt to it, but the performance and maintainability gains make it worthwhile.
Maintainers | Bundle size | npm downloads |
Modulz | 28.8 kB | < 1k |
Its maintainers describe Stitches as a styling library with near-zero runtime, server-side rendering, multi-variant support, and best-in-class developer experience. As compared to Linaria and CSS Blocks, Stitches is closer to styled-components when it comes to architecture. With a smaller size than styled-components, it provides much of the same functionality with a similar API.
The best part of Stitches is variants, which help in developing better component APIs. You can define styles for each variant and also combine them. It also uses CSS variables for theming; this allows it to avoid runtime props interpolation, providing a decent performance boost compared to other styling libraries available.
Another beautiful feature is tokens, which enable you to declare variables and use them as CSS values — yes, even in shorthand. Also, the switch from styled-components is relatively seamless as their APIs are quite similar.
Maintainers | Bundle size | npm downloads |
OSS – Used by Uber | 28.8 kB | > 20k |
Developing libraries and components that work well with any UI library or framework is something that all frontend devs look for today. Styletron, being library-agnostic, allows you to write components that work well with any UI library, be it React or any other.
Styletron is good for both Atomic CSS and critical rendering paths. It adds only required CSS to style tags and performs declaration-level de-duplication, which reduces the size of CSS that the browser has to process. You get all this in a tiny, 8KB gzipped library. As for developer experience, it doesn’t require any bundler configurations or tooling setup.
Maintainers | Bundle size | npm downloads |
Open Source Contributors | 15.4 kB | > 724k |
Emotion has been around for a while now and pioneered many ideas that other styling libraries have since adopted. It is framework-agnostic and also has a style API to create React components with styles attached to them.
Emotion provides a great developer experience because of its source map support. It comes with an out-of-the-box theming mechanism, ESLint plugins, and support for CSS props.
In short, Emotion has everything that styled-components offers — with a slightly smaller bundle size, to boot — so you can swap styled-components with Emotion with minimal effort.
Maintainers | Bundle size | npm downloads |
Open Source Contributors | 24.6 kB | > 18k |
Fela is based on a unique principle: if the view is a function of the state, your CSS should be, too, because it’s part of your view. Just like React and Redux, Fela doesn’t explicitly tell you how to write your styles; it comes with a powerful API that helps you build your styling environment.
Fela considers dynamic styling its core, and it is built to be framework-agnostic. It also adopts the Atomic CSS principle as every rule is given a unique class, making your CSS smaller and more performant.
As for its API and switching from styled-components, Fela has a different mental model and a very different API. It might take a little time to catch up on the pace, but it’s unique and has great benefit for apps that have a lot of dynamic styles.
Maintainers | Bundle size | npm downloads |
Open Source Contributors | 2.3 kB | > 8k |
Goober is a lesser-known library built with the motivation to avoid the bundle size impact of styled-components (~12KB) and Emotion (~11KB). Goober claims to have all the goods offered by styled-components.
From a performance perspective, Goober can beat out styled-components. But when benchmarked for SSR, it was slightly behind Emotion. As for its features, almost all of the most widely used features of styled-components are available with Goober.
Its API is quite similar to styled-components’, and as the mental model remains the same, it shouldn’t be much of a hassle to move from styled-components to Goober.
CSS-in-JS has provided a way for developers to maintain app styles with little hassle and solved many of the issues associated with bare CSS. As we move forward, the amazing frontend community, along with various tech giants, are now building scalable and more performant solutions based on the learnings we got from styled-components.
I appreciate people who are putting in the effort to maintain all of this cool stuff. All of them have their unique pros and cons, and each can be handy in certain scenarios. If you’re looking for more, have a look at some other CSS-in-JS libraries here, as compared by Michele Bertoli.
Install LogRocket via npm or script tag. LogRocket.init()
must be called client-side, not
server-side
$ npm i --save logrocket // Code: import LogRocket from 'logrocket'; LogRocket.init('app/id');
// Add to your HTML: <script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script> <script>window.LogRocket && window.LogRocket.init('app/id');</script>
Hey there, want to help make our blog better?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowBuild scalable admin dashboards with Filament and Laravel using Form Builder, Notifications, and Actions for clean, interactive panels.
Break down the parts of a URL and explore APIs for working with them in JavaScript, parsing them, building query strings, checking their validity, etc.
In this guide, explore lazy loading and error loading as two techniques for fetching data in React apps.
Deno is a popular JavaScript runtime, and it recently launched version 2.0 with several new features, bug fixes, and improvements […]