Together, we will look at the top 15 CSS-in-JS libraries as judged by total accumulated GitHub stars. We’ll compare their features, strengths & weaknesses. Included are some really new libraries such as emotion
, as well as some of the originals, such as aphrodite
.
If you’re like me, it took you a while to get on board the CSS-in-JS train. If you’re not sure what that is, then it’s worth checking out @vjeux’s original “React: CSS in JS” slides. Fast forward nearly three years, and today there is no shortage of CSS-in-JS libraries to help us style our React Components using JavaScript. In fact, at the time of writing, there are 55 such libraries listed on @MicheleBertoli’s css-in-js
repo, which is a giant centralized list of libraries with individual examples of how to style a basic button in each library. Super helpful!
About every six months or so, a new library emerges. Far from eliciting JavaScript fatigue, this rate of change is very encouraging! Each new approach builds on the strengths of the previous libraries and innovates on some of the prior libraries’ shortcomings. While some of the newer libraries provide more features and performance, there is still something to be said for the longevity of some of the older libraries, which is why they are included as well. Keep that in mind when deciding on which to use for your next project.
For the sake of brevity, we’ll keep it short here and do not include any code samples. If you would like to see how each library is used, check out each project’s GitHub links to read its full description.
emotion
is the new kid on the block. But for its age, it not only provides an impressive array of features, but it also seems to be gaining traction extremely quickly. It is going for the sweet spot of great developer experience AND an obsession with performance. The killer feature here is the combination of both styled-components, as well as the ability to provide traditional selectors for nested components. emotion
is definitely worth keeping an eye on.
glamorous
wraps the popular glamor
library and provides a styled-component
-like experience but using JavaScript syntax rather than interpolated strings. Dynamic props are a big win for this library over others, as is themeing. If you want to experiment with creating styled React components using JavaScript syntax, glamorous
might be right for you.
styled-jsx
allows you to add <style jsx />
tags inside your Components’ render functions. This allows you to write styles in plain CSS with the added benefit that the CSS inside of these is only applied to the current scope of the DOM being rendered at that level. If you would like to comingle your CSS inside your render
function, styled-jsx
has your name all over it.
jsxstyle
allows you to forget thinking about the HTML elements that you use simply for styling purposes (e.g., <div />
, <span />
, <table />
, etc.), and instead use these styled components (<Block />
, <Flex />
, <Inline />
, <Table />
, etc.). It also disregards many of the norms of CSS, and instead provides an interesting developer experience of being able to specify psuedo-selectors as props (e.g., hoverColor
). If you are looking to remove the tedium of writing <div className={styles.container} />
for the 100th time, you should check out jsxstyle
.
TypeStyle
is the TypeScript of CSS – hence the name. The focus is on compile-time linting, autocomplete, etc. to ensure a good developer experience and protect against runtime issues. Best of all, it’s framework agnostic, so you could learn it once and use it anywhere. If you are already a TypeScript fan, then TypeStyle
is worth a look.
Far and away the most popular of the CSS-in-JS libraries, styled-components
flips the problem on its head. Rather than adding styles to existing components, you instead create components that embody the styles. This allows for declarative and reusable components that are halfway between a regular HTML element and a fully-fledged React Component. This philosophy, as well as the GraphQL-esque string interpolation, take a bit of getting used to, but seeing as it’s nearly twice as popular as the second-place CSS-in-JS library, it’s safe to say there’s something here worth looking at.
The real power of glamor
is in its ability to allow you to write and combine CSS rules. Usually, extending / overwriting CSS-in-JS rules can be a hassle; however, glamor
lets you either splat them sequentially inside a Component’s props, or list them inside its className
. If you are looking for a trusted “what you expect” library, then look no further than glamor
.
styletron
’s claim to fame is its speed, derived from its use of “Virtual CSS” (like the “Virtual DOM”) which abstracts away the conversion of JS-to-CSS in order to provide optimizations, speedups, dead CSS pruning, etc. Of particular focus is minimizing injected CSS on server-side rendering for fast page load times. If you are looking for a good developer experience which provides a huge boost in speed, start by looking at styletron
.
radium
was one of the original CSS-in-JS libraries which seeks to solve the issue of handling media queries, pseudo selectors, and modifiers. It is still one of the most popular libraries out there, which speaks to its practicality and longevity.
react-css-modules
is close as it gets to being able to write pure CSS in your React Components. Literally, you import .css
files and use their keys in the className
prop. As one of the first CSS-in-JS libraries, the main problem it was trying to solve was scoping CSS selectors. It’s worth seeing whether babel-plugin-react-css-modules
would meet your needs. Although it has fewer features, it is a much smaller package, and much more performant.
aphrodite
is written and maintained by the good folks at the Khan Academy. As one of the original CSS-in-JS libraries, the primary problem it was trying to solve was getting CSS-parity but using JavaScript syntax. As such the focus is on selectors, media queries, animations, etc. If you are looking for a respected library which has the longevity of an established maintainer, aphrodite
might be anchor in the storm of JS libraries.
As one of the original CSS-in-JS libraries, reactcss
focus was on dynamically swapping styles based on props. While this is a feature that many newer libraries, it should be noted that this was a feature ahead of the curve back in the day. It is still worth looking at, especially if need full CSS support (including Autoprefixing) or React Native support.
👉 Big thank you to @MicheleBertoli and all the other contributors to css-in-js.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!