ReactJS Styling: Exploring Different Techniques with Examples
Published: 2024-03-07 11:59:20


![ReactJs Logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plavmxx7dngb39e0u266.jpeg)

In ReactJs development, how we style things is super important. This article explores different ways to style React components, each with its own benefits and best uses.

There are various methods or, more precisely, numerous ways to utilize CSS in ReactJs :

**Traditional Stylesheets:**
The conventional method involves using external stylesheets (CSS or SCSS) and importing them directly into React components. For instance:

```jsx
// styles.css
.button {
  background-color: #3498db;
  color: #fff;
  padding: 10px 15px;
  border: none;
  border-radius: 5px;
}

// Button.js
import React from 'react';
import './styles.css';

const Button = () => (
  <button className="button">
    Click me
  </button>
);

export default Button;
```

**Inline Styles:**
React supports inline styles, allowing developers to apply styles directly within components using JavaScript objects. For instance:

```jsx
const Button = () => {
  const buttonStyles = {
    backgroundColor: '#2ecc71',
    color: '#fff',
    padding: '10px 15px',
    border: 'none',
    borderRadius: '5px',
  };

  return (
    <button style={buttonStyles}>
      Click me
    </button>
  );
};
```

**CSS Modules:**
CSS Modules offer local scoping within React components to avoid global styling conflicts. For instance:

```jsx
// Button.module.css
.button {
  background-color: #e74c3c;
  color: #fff;
  padding: 10px 15px;
  border: none;
  border-radius: 5px;
}

// Button.js
import React from 'react';
import styles from './Button.module.css';

const Button = () => (
  <button className={styles.button}>
    Click me
  </button>
);

export default Button;
```

**Styled Components:**
Styled-components allow the definition of styles directly within JavaScript files. For instance:

```jsx
import styled from 'styled-components';

const StyledButton = styled.button`
  background-color: #f39c12;
  color: #fff;
  padding: 10px 15px;
  border: none;
  border-radius: 5px;
`;

const Button = () => (
  <StyledButton>
    Click me
  </StyledButton>
);
```

**Preprocessors(SASS/LESS):**
Integrating CSS preprocessors enhances styling capabilities. For instance, using SASS:

```scss
// styles.scss
$primary-color: #9b59b6;

.button {
  background-color: $primary-color;
  color: #fff;
  padding: 10px 15px;
  border: none;
  border-radius: 5px;
}

// Button.js
import React from 'react';
import './styles.scss';

const Button = () => (
  <button className="button">
    Click me
  </button>
);

export default Button;
```

**Utility-First CSS:**
Libraries like Tailwind CSS provide a utility-first approach. For instance:

```jsx
import React from 'react';

const Button = () => (
  <button className="bg-gray-800 text-white p-2 rounded">
    Click me
  </button>
);
```

**Conclusion:**
In ReactJs, choosing the right styling approach is crucial. Whether using traditional stylesheets, inline styles, CSS Modules, Styled Components, preprocessors, or a utility-first paradigm, understanding these techniques empowers developers for tailored decisions in their projects. This tutorial is brought to you by [Jsconfig.com](https://jsconfig.com/), specializing in [React.js training](https://jsconfig.com/reactjs-pricing-plans).

Feel free to contribute any additional methods you're familiar with in the comment section.