I wrote a comprehensive Guide to style React Apps

I wrote a comprehensive Guide to style React Apps

A quick guide for CSS & Sass in React Apps

·

2 min read

Styling in React can be approached in various ways, each with its own advantages and use cases. Here, we'll explore several methods to style React components effectively, including examples and best practices.

💡
If you enjoy this article, please check my 2024 Masterclass Full-Stack Developer for $12.

1. External Stylesheets

You can create a CSS file and import it into your React components. This method is similar to traditional web development and allows for easy reuse of styles across multiple components.

// styles.css
.red-text {
  color: red;
}
.large-text {
  font-size: 32px;
}

// MyComponent.jsx
import React from 'react';
import './styles.css';

const MyComponent = () => {
  return <div className="red-text large-text">Hello World</div>;
};

export default MyComponent;

2. Inline Styles

Inline styles are applied directly to elements using the style attribute in JSX. This approach is useful for dynamic styling but can become unwieldy for complex styles.

const MyComponent = () => {
  return <div style={{ backgroundColor: 'blue', color: 'white' }}>Hello World</div>;
};

3. CSS Modules

CSS Modules scope CSS to the component, preventing style conflicts. This method is ideal for large applications where encapsulation is crucial.

// styles.module.css
.myClass {
  color: blue;
}

// MyComponent.jsx
import React from 'react';
import styles from './styles.module.css';

const MyComponent = () => {
  return <div className={styles.myClass}>Hello World</div>;
};

export default MyComponent;

4. CSS-in-JS

Libraries like styled-components allow you to write CSS directly within your JavaScript files. This approach promotes component-level styling and supports dynamic styles based on props.

import styled from 'styled-components';

const StyledDiv = styled.div`
  background-color: blue;
  color: white;
`;

const MyComponent = () => {
  return <StyledDiv>Hello World</StyledDiv>;
};

export default MyComponent;

5. Preprocessors (Sass)

Using Sass or other preprocessors can enhance your CSS with features like variables, nesting, and mixins. These need to be compiled to standard CSS.

// styles.scss
$primary-color: blue;

.myClass {
  color: $primary-color;
}

// MyComponent.jsx
import React from 'react';
import './styles.scss';

const MyComponent = () => {
  return <div className="myClass">Hello World</div>;
};

export default MyComponent;

6. Utility-First CSS (Tailwind CSS)

Tailwind CSS provides utility classes for rapid UI development. It promotes a different way of thinking about styling by applying classes directly to elements.

const MyComponent = () => {
  return <div className="bg-blue-500 text-white">Hello World</div>;
};

export default MyComponent;

Best Practices

  • Consistency: Stick to a single method for consistency.

  • Modularity: Use CSS Modules or styled-components for better encapsulation.

  • Maintainability: Keep styles organized and reusable.

  • Performance: Avoid excessive inline styles to prevent performance issues.

I hope you enjoyed this quick article,

Guillaume Duhan

Â