CSS in JS in real-life

Jul 20, 2018 · 16 min read

By Artur Siery

Recently I have stumbled across a lot of blog posts describing changes in styling approaches. Clearly, we’re entering a new era of styling in JS, that simply can be called CSS in JS. But what all of those posts miss, in my opinion, is a deep comparison of the features provided by those solutions and how they perform in real-world project. Code examples always look fresh and reusable and sexy, but what happens when we pick such solution and use it in a live application? Are there any drawbacks that turn beautiful ideas into coding hell? Let’s find out.

Side note: Recently I’ve been a React developer and when I did the research I was focusing mainly on how I could utilize the technology in my projects, so therefore I will mostly focus on the ease of use in React. Ability to read JSX will help you to understand what the heck is going on.

Image for post
Image for post

CSS Modules

Putting CSS Modules here can be a bit of a stretch, depending on how you define CSS in JS, because you write basically in CSS (with some cool additions). What’s change is the way of loading the styles into components. But they were the first, so treat it like a honorable mention on the list. Chances are that you probably seen CSS Modules somewhere. But let’s take a look at them (or, you know, you can always scroll to the next section). Let’s start with this simple example:

Okay, so as you can see, the CSS are still the same. You simply write them like you did for years. What changes is the way of loading them into your application. Now, while bundling, the CSS files are being parsed and turn into pure JS objects, that are easy to use inside code. Styles are imported explicitly and used like any other JS object, which is very cool from the semantic perspective. But that’s not where CSS Modules shine.

Local Scope

Having classnames parsed, CSS Modules is able to change the names of the output classnames into totally unique strings. This gives us local scope inside CSS. That’s huge! The generated output will look like this:

The unique classnames are awesome! Are the solution to everything! And are totally un-debuggable. But there’s a small fix for it:

Ah, much better! Now it’s clearly visible what styles come from what files. You can adjust the naming convention according to what’s suits you the most.


  • Pure CSS (no learning curve)
  • Some additional features
  • Local scoping
  • Nice semantics using JavaScript’s import statement
  • Easy debugging (with the right Webpack setting)


  • No more global styles, which is generally a good thing, but you need to invest some time to adjust to this approach
  • Additional Webpack (or other bundler) configuration

SASS Modules / LESS Modules

CSS Modules on their own bring some additional functionalities, like composition or global pseudo class. But if you’re missing some of the cool stuff like mixins or variables you can always add SASS or LESS. Just add the respective loader to your webpack config. Now, you have a very powerful duo, fully pack with features and possibilities.


  • All of the CSS Modules features
  • All of the SASS/LESS features (!)


  • Similar as CSS Modules

For a long time I thought that using SASS with CSS Modules was the ultimate solution. Then, the other solutions came along.

Styled Components

Although one can argue, that CSS Modules were the first CSS in JS, Styled Components were the one that started the whole avalanche of such solutions recently.

Styled Components utilize tagged template strings and allow to put your CSS code inside JavaScript directly. You can still split it into 2 files, to get that feeling of logic and styles being separate. But it’s optional. So, how our example would look like in Styled Components? Let’s see:

It’s pretty straightforward. Just use tags provided by the library to render a component that you would like having styling added to inside the template string literal. As simple as that.

Extending styles

Let’s say, we would like to extend the styling of existing component and add some more. That’s very simple:

Things get a little bit complicated, when you would like to add additional handlers to styled components. Let’s take a simple button and add a onClickhandler to it. Sadly, we can’t do it in one step. What we need to do, is basically create 2 components — one with logic, and one that brings styles:

So this makes things a bit excessive. Every time you would like to add some logic to a component, you will end up with 2 components. While it is kinda in style of HoC, but it just doesn’t go well with other HoC, so it can’t be used with cool HoC helpers like recompose. I must admit, that I’m a big fan of HoC and not being able to use Styled Components in that fashion just misses the mark for me.

Creating a separate component every time that a styling is needed seems like a good idea, but when I used such approach I suddenly missed the ability to just write a bit more complicated markup. So, in theory this approach is very appealing, but practice shows that sometimes you need to produce much more small components. This can lead to less readable code. Because if I can do something in 20 lines of JSX, I would prefer that over creating 10 small components.

Dynamic Styles

One of cool features of Styled Components, that was just not possible in CSS Modules, is the ability to read components’ props and generate the appropriate styling. Let’s assume that we have 2 props: important and width. They can be accessed inside the style in such manner:

Dynamic styles are a pretty cool feature. Although my concern is that it can lead to more logic-heavy styles. Depending on the project, that can be a double edged sword.


Another great feature is theme support. That’s right, you can simply define your theme or color palette (as a simple object) and access its properties whenever you like. But what if I could tell you, that you can change themes on the fly? Yup, it’s possible. Let’s see it in action:

The usage of ThemeProvider allows to add a theme (via property) to all components inside. If you’re in need of adding another theme, just place your components inside another ThemeProvider and enter a new theme property. As simple as that. The question arises, how many times you were in need of a theming that can be changed? I didn’t see much of such projects recently, but if you need it, it’s there.


Now, from debugging perspective, the generated output is fairly good. The StyledButton from the example above will not render a component inside a component, which is good. At the very beginning only the class names are unreadable (such as in case of CSS Modules). But there’s a Babel plugin for that, and you’re good to go.


  • Easy to use, easy to understand.
  • Local scope
  • Dynamic styling based on props
  • Theming
  • Easy debugging (with babel plugin)
  • Works with React Native (although I didn’t test that)


  • Excessive creation of components (2 components each time handler or lifecycle method needs to be used)
  • Possible too much of too small components
  • Syntax highlighting needs an external plugin


Glamorous takes inspiration from Styled Components and aims to change a bit the way you can use it. Notably, the biggest difference is the departure from tagged template strings in favor of plain objects. You still would use it in a similar way, but replacing string with objects. Just take a look:

To use media queries, pseudo classes and pseudo elements, simply create nested objects with selectors as their names, like so:


Well, this is something that does not come with Styled Components. Glamorous has a build in helper for creating CSS animations. That’s something really cool. Nowadays fluid animations and transitions are important parts of good UX and everything that improves creation of those effects is more then welcome.

Okay, so it’s not a game changing functionality. But it allows to create unique keyframes animations in a simple manner.

Dynamic Styling

Just like in Styled Components, Glamorous has the ability to use dynamic styling. In my opinion, it does it in much clearer way. Because we work with objects, not with string literals, conditional operations are much more natural. Simply, instead of object, pass a function that receives props and returns a valid object.

Obviously, having the ability to pass a function into the component factory gives you much more flexibility, but allows to create even more logic-heavy styles. Double edged sword once again.

Theming and reusing styles

Theming for Glamorous looks like it was taken directly from Styled Components, just define a theme object and pass it to your components. They will have access to it via props.theme. Simple as that.

Glamorous allows also to reuse and extend existing styles. For that you can use the glamorous object as a function, pass the component and then styles to override the existing ones.

Moreover, it allows you to copy existing styles to a different tag via withComponent method:

Not HoC compliant

When it comes to overall usage, Glamorous suffers from the same drawback of not really being in compliant with HoC approach. So, once again, to build a more advanced component with styling, you will need to create 2 components. Let’s revisit the example from Styled Components:

So this comes with the same issue: too many small components.


When it comes to debugging, Glamorous on it’s own is not very readable in the DevTools. But, obviously, there’s a plugin for that.


  • ...styledComponents.pros
  • Object notation instead template literals
  • Keyframes helper


  • (Potentially) excessive components creation


When it comes to Styletron, I’m probably quite biased. I have become involved in a project using it and it quickly pushed me to do research on other similar solutions to migrate away to. I felt so limited and distracted by it so I couldn’t just bare with styling anymore. I get the impression, that Styletron does a lot of things similar to Glamorous, but fails to deliver really usable in real-life solution. Let’s start with basics:

When it comes to simple usage, it’s almost the same as in Glamorous.

Themes, dynamic styles and extending styles

Styletron provides ways to extend existing styles, or to build dynamic styles. It also supports theming in a very similar fashion, as we have seen previously. Let’s see all of those features in action together:

Similar to previous solutions, Styletron does not allow to create more lifecycle-heavy components with styles. Instead, you need to create 2 components, one with logic, and one with styles:

So, let’s see, what Styletron renders in HTML…

Generated output

The output for the example above will be something like:

And two issues can be seen here. Firstly, that the rendered code is totally not debuggable. You can’t really deduce from which component any of the styles really come. Or even what component you’re looking at. Switching to React DevTools doesn’t help either. All you gonna see is that it’s <styled(div)>. That’s really helpful.

Oh, and each style property gets it’s own, unique class name. Imagine having a well styled component with over 30 properties. Yeah…

And what’s even worse, there is no debugging plugin for it!

Moreover, imagine that the example would have 10 nested components, one inside another. Could you tell the number of generated divs? Yup, 20. Because every time you need to have more advanced component, Styletron will need to add additional wrapper for styling purposes. That’s something I like to call a Wrapper-iada. And it’s no good.

Other issues

Styletron has a bug that’s basically prevents you from using dynamic tags (so the tags used depending on the props). Let me explain:

Now, if I would like to add styling to my DynamicHeader, I would go with:

None of the new styles won’t be applied. Why? I don’t have an idea, but it’s not good.

No animations

That’s right, Styletron does not support css keyframes! Obviously, you can use animation like the following:

And it will work. But you can’t define the keyframes of bounce using Styletron. If you would really like to somehow define keyframes, you need to use good, ol’ CSS. That’s a terrible solution.

Let’s sum up:


  • Looks similar to Glamorous
  • Local scope
  • Theming, dynamic styles, extending styles


  • Undebuggable (!)
  • Wrapper-iada
  • No dynamic elements
  • No keyframes support

Styled JSX

Next CSS-in-JS solution that I researched was Styled JSX. At the very beginning it looks different to the solutions above, it kinda looks more inline with JSX approach and I find it a bit more appealing. The basic example looks like this:

Styled JSX uses template literals inside of a <style jsx> tag. What’s really neat, is that you can use the plain old CSS inside. It’s readable, it’s safe due to local scope. If you like, you can move the string to a separate file because everything is in a string. If you’re running React in versions 16.2 or higher, you can replace the div with React’s fragment.

Same features and more freedom

Obviously, defining the styles inside component’s render method allows us to access all of the props and hence build dynamic styles. Or you can access the theme provided by ThemeProvider although you need to use additional module for that, as Styled JSX don’t have a built in ThemeProvider.

What I like here, is that I can write styles and apply them to components with lifecycle methods. Another thing to like is that Styled JSX integrates nicely with React’s idea of rendering components. That was not possible with any of the previous libraries. But here I can decide when I should split component into smaller ones. That kind of freedom is something I really appreciate. Artificial code splitting forced by previous solutions does not always end up in a very readable code.


I must say I really like this approach, but the drawbacks just kill it for me. The problems start when you want to extend existing styling. Or when you would like to alter the default styling of a child component. There is no easy way of doing such things. While in Glamorous all you needed was to call glamorous(DefaultButton)(newStyles), in Styled JSX you need to come up with your own approach to reusing existing styling. So as I said before, this kills the Styled JSX for me.


  • More inline with JSX approach
  • Doesn’t force artificial code splitting
  • Easy to understand
  • Local scope
  • Dynamic Styles, Themes support


  • Reusability of styles is hard if non-existent


The last but not least — JSS. At first glance, JSS looks very similar to Glamorous. You create objects literals with styles, you apply them to components. But it also successfully adds the ability to write couple styles for multiple tags inside one component.

First what I have to admit, and what really got me excited about JSS is that finally, it’s a HoC! You pass a object with different class names to it, and the HoC will produce a wrapper that adds a classes property containing generated, unique class names. In a way, it brings the functionality of both Styled JSX and Glamorous neatly together. That’s off to a good start.

Theming and dynamic styles

Just like abovementioned solutions, JSS supports both themes and dynamic styles. It comes with build in ThemeProvider which is always a neat solution.

The theme object is available when instead of styles object, you create a function that returns the styles object. Component’s props are available in a slightly more annoying way, as a argument for function returning value of each property.

Reusing and overriding styles

When it comes to extending styles, well, JSS does not provide any simple way of doing so. But, then again, they are all JS object, so we can export styles and extend them on our own using the spread operator.

A bit of work, but it’s very explicit. No under-the-hood magic. There is a extend-plugin, however its usage is very similar to the spread operator.

JSS has a neat feature that is not available in other libraries, and that’s ability to override default styles of the component. You can call the withStyles HoC again and again on component, overriding it’s default styles, like so:

Because withStyles performs a shallow merge of the style objects, the StyledA component will contain the original styles for paragraph, but it will have only the newest styles for wrapper. This means that it will have only the color: blue property but it will lose the fontSize: 1.4em property.

You can obviously combine the two methods to override styles with extending the existing ones. However, since it is a HoC, you can write your own HoC that does just that.

Other features

JSS comes with variety of plugins. You can use them, for example, to write styles inside template literals, if that’s your thing, or to extend existing classes via class names or objects.


When it comes to debugging, JSS is very user friendly. At the very beginning, it renders human-friendly class names, so you can quickly find your way around the generated HTML markup. It also does not change the generated components’ names, as it only works on the className property, which is quite nice.


  • Local Scope
  • Theming, dynamic styles
  • No artificial code splitting
  • Overriding default styles (!)
  • works as a HoC


  • No native style extending solution (but you can work around this)


It’s time to sum up. Is there a clear winner? In my opinion, yes. JSS takes the top spot. It gives me all of the features of other CSS-in-JS solutions and doesn’t forces me to create super-small components each time I need styling for a div. In addition to that, it works as a HoC, so if I don’t like the default behavior (like when overriding styles) I can always create my own HoC that wraps it and changes the behavior. It’s simple to use and flexible. And at the end of the day, it’s the solution that devs from the Material UI are gonna use. For me, that’s a hell of a recommendation for JSS.

As I already mentioned, I thought that SASS + CSS Modules were the future. But now I can clearly see, that CSS in JS is at least as good, with JSS being my favorite. It’s really usable and works great in the real-life development. My personal pick after doing the research: JSS.

Image for post
Image for post

We talk about JavaScript. Every month in Warsaw, Poland.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store