Table of contents
1.
Introduction
2.
How to Integrate React with CSS
2.1.
1. Inline Styling
2.2.
camelCased Property Names
2.3.
2. CSS Stylesheet
2.4.
3. CSS Modules
2.5.
4. Styled Components
2.6.
Installation
3.
Best Practices for Organizing and Maintaining Your CSS in a Large React Application
3.1.
1. Use Component-Based CSS
3.2.
2. Use CSS Modules
3.3.
3. Use a Preprocessor like Sass
3.4.
4. Use a CSS-in-JS Library
3.5.
5. Keep Your CSS DRY
4.
Frequently Asked Questions
4.1.
What is the best way to apply CSS in React?
4.2.
Why use camelCase in inline styles?
4.3.
What is the advantage of CSS Modules?
5.
Conclusion
Last Updated: Feb 2, 2025
Medium

Styling React Using CSS

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Styling React Using CSS is an important technique for developers who want to create visually appealing and responsive user interfaces. In React, styling can be achieved in various ways, such as using traditional CSS files, inline styles, or CSS-in-JS libraries. While React focuses on building components and managing state, CSS is essential for making those components look good and providing an enhanced user experience.

Styling React Using CSS

In this article, you will learn about different methods for styling React components using CSS, including the pros and cons of each approach, and how to implement them effectively in your React projects.

How to Integrate React with CSS

There are several ways to style components in React:

  1. Inline Styling
     
  2. CSS Stylesheet
     
  3. CSS Modules
     
  4. Styled Components
     

Each method has its own advantages and is used based on the requirements of the project.

1. Inline Styling

Inline styling allows you to add styles directly to an element using the style attribute in JSX.

Example:

import React from 'react';

const InlineStyleExample = () => {
  return (
    <div style={{ color: 'blue', fontSize: '20px', padding: '10px' }}>
      This is a styled div using inline CSS.
    </div>
  );
};

export default InlineStyleExample;

Output:

A div with blue text, a font size of 20px, and padding of 10px.

camelCased Property Names

In inline styling, property names follow camelCase instead of hyphens.

Example:

const headingStyle = {
  backgroundColor: 'yellow',
  textAlign: 'center',
  padding: '10px'
};

const InlineStyledComponent = () => {
  return <h1 style={headingStyle}>Styled Heading</h1>;
};

export default InlineStyledComponent;

Output:

A heading with a yellow background, centered text, and 10px padding.

2. CSS Stylesheet

A CSS file can be created separately and imported into the React component.

Example:

Create a CSS file (styles.css):

.container {
  color: red;
  font-size: 24px;
}

 

Import and use it in a React component:

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

const StylesheetExample = () => {
  return <div className="container">This is styled using an external CSS file.</div>;
};

export default StylesheetExample;


Output:

A div with red text and font size of 24px.

3. CSS Modules

CSS Modules help prevent class name conflicts by scoping styles locally to the component.

Example:

Create a CSS module file (styles.module.css):

.title {
  color: green;
  font-size: 22px;
}


Import and use it in a React component:

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

const CSSModuleExample = () => {
  return <h2 className={styles.title}>Styled using CSS Modules</h2>;
};

export default CSSModuleExample;


Output:

A heading with green text and font size of 22px.

4. Styled Components

Styled Components is a library for writing CSS inside JavaScript files.

Installation

To use styled components, install the package:

npm install styled-components

Example:

import React from 'react';
import styled from 'styled-components';
const Button = styled.button`
  background-color: blue;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;

  &:hover {
    background-color: darkblue;
  }
`;
const StyledComponentExample = () => {
  return <Button>Click Me</Button>;
};
export default StyledComponentExample;


Output:

A blue button with white text, rounded corners, and hover effects.

Best Practices for Organizing and Maintaining Your CSS in a Large React Application

Whenever you are working on a large React application, it’s very important to keep your CSS organized. This helps you manage styles better and makes your code easier to maintain. Let’s take a look at some best practices to follow:

1. Use Component-Based CSS

In React, each component can have its own CSS file. This helps you keep styles specific to each component and avoids conflicts. For example, if you have a Button component, you can create a Button.css file.

For example:

Button.js

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

function Button() {
  return (
    <button className="custom-button">Click Me</button>
  );
}
export default Button;


Button.css

.custom-button {
  background-color: blue;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.custom-button:hover {
  background-color: darkblue;
}


This way, the styles for the Button component are contained within Button.css and won’t affect other components.

2. Use CSS Modules

CSS Modules help you scope your CSS to specific components. This means styles defined in one component won’t affect others. To use CSS Modules, you just need to rename your CSS file to have a .module.css extension.

For example:

Button.module.css

.button {
  background-color: green;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.button:hover {
  background-color: darkgreen;
}


Button.js

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


function Button() {
  return (
    <button className={styles.button}>Click Me</button>
  );
}
export default Button;


In this example, styles.button refers to the styles defined in Button.module.css. This ensures that the styles are scoped to the Button component only.

3. Use a Preprocessor like Sass

Sass is a powerful CSS preprocessor that adds more features to CSS. It allows you to use variables, nested rules, and mixins, making your CSS more maintainable.

First, you need to install Sass:

npm install node-sass --save-dev


Then, you can create a Sass file and use it in your React component:


Button.scss

$primary-color: blue;
.button {
  background-color: $primary-color;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;


  &:hover {
    background-color: darken($primary-color, 10%);
  }
}


Button.js

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

function Button() {
  return (
    <button className="button">Click Me</button>
  );
}


export default Button;


In this example, $primary-color is a variable that can be reused throughout your styles. The darken function is used to create a darker shade of the primary color for the hover state.

4. Use a CSS-in-JS Library

CSS-in-JS libraries allow you to write CSS directly in your JavaScript files. This can be very powerful and helps you keep your styles close to your components.

One popular library is styled-components:

First, install styled-components:

npm install styled-components

 

Then, you can use it in your component:

import React from 'react';
import styled from 'styled-components';


const StyledButton = styled.button`
  background-color: blue;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;


  &:hover {
    background-color: darkblue;
  }
`;


function Button() {
  return (
    <StyledButton>Click Me</StyledButton>
  );
}


export default Button;


In this example, StyledButton is a styled component that includes all the styles directly in the JavaScript file.

5. Keep Your CSS DRY

DRY stands for "Don't Repeat Yourself." This means you should avoid duplicating styles. Instead, use classes and mixins to reuse styles.

For example, if you have a common button style, you can create a mixin and reuse it:

styles.scss

@mixin button-style {
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
.button {
  @include button-style;
  background-color: blue;
  color: white;

  &:hover {
    background-color: darkblue;
  }
}


This way, you can reuse the button-style mixin in other components without duplicating the code.

Frequently Asked Questions

What is the best way to apply CSS in React?

It depends on the project. For global styles, use CSS stylesheets. For scoped styles, use CSS Modules or Styled Components.

Why use camelCase in inline styles?

React uses camelCase because JSX uses JavaScript objects instead of traditional CSS syntax.

What is the advantage of CSS Modules?

CSS Modules prevent class name conflicts by scoping styles locally to the component.

Conclusion

In this article, we discussed various ways to style React components, including inline styles, CSS stylesheets, CSS modules, and styled components. Each method has its benefits, and choosing the right one depends on your project requirements.

Live masterclass