Navigate back to the homepage
📚 Books

Styling React Applications

Mark Pollmann, 
May 3rd, 2018 · 2 min read

When you start writing web applications in React, sooner or later you have to handle styling. React itself doesn’t have an opinion about how to style components, so it’s on you to find something that works best for you.

Here are some of the options:

Write Oldschool CSS

This is probably the easiest way to get started. You just write css classes in your .css files, import the files into your app and apply classes to your components with the className attribute. You can write your css in nicely separated files or in one huge file but in the end all the stylings are global. To handle complexity you can use a methodology like BEM or SMACSS to keep your styles in check.

This way you can also use CSS frameworks like Bootstrap or Bulma. Just import the corresponding .css-files and you’re good to go. Remember that if you want to overwrite some styles import your own css last.

Use Component libraries

Using libraries like React-Bootstrap or reactstrap you can import pre-styled components and don’t have to write the CSS yourself. It works like this (example from the reactstrap homepage):

1import React from "react";
2import { Button } from "reactstrap";
3
4export default props => {
5 return <Button color="danger">Danger!</Button>;
6};

This works pretty well if you’re ok with a pretty generic-looking website (you can still get a pretty original look but it will take more work) and you know that the styling won’t have to change much.

Try some CSS-in-JS

This is my currently preferred approach since it allows local CSS namespaces pretty easily. It gets a lot of probably deserved backlash but for the moment I will stick with it as it reigns-in the complexity, there are no css name-clashes and all of the component is visible in one file.
I use styled components as a library and it looks like this:

1import styled from 'styled-components;
2
3const MyWrapper = styled.div`
4 display: flex;
5 justify-content: center;
6`;
7
8...
9 render() {
10 return (
11 <MyWrapper>
12 <h1>Hi there</h1>
13 <p>We are centered</p>
14 </MyWrapper>
15 )
16 }
17...

There’s more functionality than this: for example you can define a ThemeProvider which injects props to every component with your globally-defined stylings (for example your primary and secondary app-wide colors which you just define inside a normal object). The code is not pretty, though (see this example from the styled-components docs):

1const Button = styled.button`
2 font-size: 1em;
3 margin: 1em;
4 padding: 0.25em 1em;
5 border-radius: 3px;
6
7 /* Color the border and text with theme.main */
8 color: ${props => props.theme.main};
9 border: 2px solid ${props => props.theme.main};
10`;

CSS modules, the future?

This is something I don’t have much experience with but it sounds very promising. I will definitely start playing around with it once create-react-app 2 with support for CSS modules is released. Until then you can read a good introduction here.

Conclusion

As with most of the React ecosystem, you have a lot of options to choose from and only in the last time clear winners started to emerge (react-router v4 for routing, create-react-app for bootstrapping, etc).

I’m not totally satisfied with the styling choices but since I heard about Vue.js single file components I really like having everything about my component in one place as styled-components, for example, provides. I’m still open to new approaches to styling. Let’s see what the future brings.

More articles from Mark Pollmann

React Development in TypeScript

For a better development experience and faster time to market

January 24th, 2018 · 2 min read

Advanced Git

Looking beyond the basics

November 3rd, 2017 · 3 min read
© 2017–2021 Mark Pollmann
Link to $https://twitter.com/MarkPollmannLink to $https://github.com/MarkPollmannLink to $https://www.linkedin.com/in/mark-pollmann-961446132/