Styling in React is flexible. You can style components in many ways depending on your project size, team preferences, and required features like theme switching or scoped styles.
React does not enforce a single styling method, so understanding your options is important.
This is the simplest way to style React components.
Create a .css file and import it into your component or globally.
App.css
.title {
color: royalblue;
font-size: 28px;
font-weight: bold;
}
App.jsx
import './App.css';
export default function App() {
return <h1 className="title">Hello React</h1>;
}
Styles are passed directly as an object to the element.
export default function App() {
const titleStyle = {
color: "crimson",
fontSize: "24px",
};
return <h1 style={titleStyle}>Inline Style Example</h1>;
}
:hover)A very popular method for avoiding style conflicts.
CSS Modules automatically scope class names to each component.
Button.module.css
.btn {
background: dodgerblue;
border: none;
padding: 12px 20px;
color: white;
border-radius: 5px;
}
Button.jsx
import styles from './Button.module.css';
export default function Button() {
return <button className={styles.btn}>Click Me</button>;
}
A popular library that lets you write real CSS directly in JavaScript.
Perfect for component-based styling and theming.
import styled from "styled-components";
const Button = styled.button`
background: purple;
padding: 10px 20px;
color: white;
border-radius: 6px;
&:hover {
background: indigo;
}
`;
export default function App() {
return <Button>Styled Button</Button>;
}
Similar to styled-components but more flexible and performant.
/** @jsxImportSource @emotion/react */import { css } from "@emotion/react";
const style = css`
color: teal;
font-size: 30px;
`;
export default () => <h1 css={style}>Hello Emotion</h1>;
A utility-first CSS framework recommended for modern apps.
export default function App() {
return <h1 className="text-3xl font-bold text-red-500">Tailwind in React</h1>;
} React apps can use external UI libraries.
import Button from '@mui/material/Button';
export default function App() {
return <Button variant="contained">Material UI Button</Button>;
}
React makes it easy to apply styles based on state.
export default function App() {
const [active, setActive] = useState(false);
return (
<button
className={active ? "btn btn-active" : "btn"}
onClick={() => setActive(!active)}
>
Toggle
</button>
);
} <div style={{ background: active ? "green" : "gray" }}>
Status Box
</div>
classNames LibraryHelps manage conditional class names.
import classNames from "classnames";
const btn = classNames("btn", {
"btn-active": active,
"btn-disabled": disabled,
}); With styled-components:
import { createGlobalStyle } from "styled-components";
const Global = createGlobalStyle`
body {
background: #111;
color: #fff;
}
`;
| Method | Scoped? | Supports Pseudo? | Best For |
|---|---|---|---|
| CSS File | ❌ | ✔ | Small apps |
| Inline | ❌ | ❌ | Dynamic styles |
| CSS Modules | ✔ | ✔ | Medium/large apps |
| Styled Components | ✔ | ✔ | Design systems |
| Emotion | ✔ | ✔ | Performant apps |
| Tailwind | ✔ | ✔ | Fast UI building |
| Frameworks | ✔ | ✔ | Quick prototypes |
Latest tech news and coding tips.
Visual Studio Code (VS Code) is powerful out of the box, but its real strength…
1. What Is a Variable in JavaScript? A variable is a named container used to store data…
1. What Is a Queue? A Queue is a linear data structure that follows the principle: FIFO – First…
Angular is a full-featured frontend framework built by Google for creating large, maintainable, and high-performance web applications.…
What Is Responsive Web Design? Responsive Web Design (RWD) is an approach to building websites…
The Geolocation API allows a web application to access a user’s geographical location (latitude, longitude, and more), with…