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.

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:
- Inline Styling
- CSS Stylesheet
- CSS Modules
- 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.