Easily Styling React Apps In JavaScript
Easily Styling React Apps In JavaScript

While many developers still use CSS to style their React apps, writing styles in JavaScript has become an increasingly popular practice as it gives all of the conveniences of CSS preprocessors without the need to learn a new language.

Most CSS-in-JS libraries involve creating a JavaScript object of styles. As an example, this is what a style object looks like using my preferred library, Aphrodite.


Many best practices from SASS/LESS workflows still apply when using JavaScript styles. Colors fonts, and other constants can be stored as variables, which can be used like so:

New ES6 language features come are handy for sharing styles cleanly. In the above example, border is referenced with the object literal shorthand notation.

Use JS objects in place of mixins

The spread operator (…) is particularly useful when defining a commonly-used sequence of styles (in this example the rules needed to add an ellipsis to overflowing text).

Create helper functions for more complex motifs

More complicated sequences of rules can be abstracted behind helper functions. For example, when styling placeholder text, I use a function that returns an object of styles:

Be consistent about naming schemes

One of biggest advantages of writing styles in JavaScript is that classNames are scoped to each component by default. This means that conventions like BEM are unnecessary. Still, it is helpful to be consistent with a naming scheme for classNames. For example, I use container for the outermost className of each React component.

Check out cool JS libraries

There are lots of helpful libraries for creating JavaScript styles. One that I particularly like is called color and handles common color manipulations:

Helpful Resources

Are you interested in frontend, design, and UI? Sign up to our mailing list to get updated when more articles like this are published.

Thank you for reading.
Give your queries and feedback or leave a message. You can also comment your questions below.
Also, don’t forget to subscribe to our blog.
And If you like this article, then please share it and help us grow



Please enter your comment!
Please enter your name here