How to style your React app

16 November 2021

There are many ways to style your React app.

  1. Utility-first CSS
  2. Inline CSS
  3. Normal CSS
  4. CSS Preprocessors
  5. CSS Modules
  6. CSS-in-JS
  7. Premade CSS, and more.

I explained and compared all of them here. If you want to avoid decision fatigue, I recommend utility-first CSS (especially Tailwind CSS).

Utility-first CSS

This category includes Tailwind CSS, Tachyons, Shed.css, and more. I'll talk about Tailwind CSS here.

Button.jsx
const Button = () => <button className='bg-blue-500 text-white'>Buy</button>

export default Button

šŸ‘ Pros

šŸ‘Ž Cons

Inline CSS

Button.jsx
const Button = () => (
  <button
    style={{
      backgroundColor: 'blue',
      color: 'white',
    }}
  >
    Buy
  </button>
)

export default Button

You add an object with CSS properties to the style attribute. Use camelCase. For example, background-color becomes backgroundColor.

šŸ‘ Pros

šŸ‘Ž Cons

Normal CSS

Button.css
button {
  background-color: blue;
  color: white;
}

Write your CSS in a .css file and import it into your component file.

Button.jsx
import './Button.css'

const Button = () => <button>Buy</button>

export default Button

šŸ‘ Pros

šŸ‘Ž Cons

CSS Preprocessors

This type includes Sass, LESS, Stylus, PostCSS, and more. These are similar to normal CSS, but they have more features like extending styles, and nesting. Sass is the most popular in this category so I'll talk about it here. You can use .sass or .scss extensions for your files. .scss has the same syntax as normal CSS. .sass doesn't require curly braces and semicolons.

Button.sass
button
  background-color: blue
  color: white

šŸ‘ Pros

šŸ‘Ž Cons

CSS Modules

If you want to use CSS Modules, use the [name].module.css file naming convention. If you want to use CSS Modules with Sass, use the file extension [name].module.sass or [name].module.scss.

Button.module.css
.button {
  background-color: blue;
  color: white;
}

In your component file, import the CSS modules and reference as a JavaScript object.

Button.jsx
import styles from './Button.module.css'

const Button = () => <button className={styles.button}>Buy</button>

export default Button

Learn more about CSS Modules.

šŸ‘ Pros

šŸ‘Ž Cons

CSS-in-JS

This category includes styled-components, Emotion, styled-jsx, and more. styled-components is the most popular in this category so I'll talk about it here. The syntax looks like this:

Button.jsx
import styled from 'styled-components'

const StyledButton = styled.button`
  background-color: blue;
  color: white;
`

const Button = () => <StyledButton>Buy</StyledButton>

export default Button

Most CSS-in-JS use a special type of JavaScript function called a tagged template literal. You can associate styles with any HTML elements, for example, styled.button, styled.h1.

šŸ‘ Pros

šŸ‘Ž Cons

Premade CSS

This category includes Bootstrap, Materialize, Semantic UI, and more. I'll talk about Bootstrap here.

Button.jsx
const Button = () => <button className='btn btn-primary'>Buy</button>

export default Button

šŸ‘ Pros

šŸ‘Ž Cons

Read other blogs