Skip to content
ClapelloNotes by Barath
ReactCh 10 · 1/6
Beginner5 min readUpdated

React / 52

Ways to style a React component

TL;DR

Inline style objects, CSS files, CSS Modules, Sass, CSS-in-JS and component libraries, with what each one is good and bad at.

The two props everything uses

Plain HTML has three ways to add CSS: a style attribute (inline), a <style> tag in the page (internal), and a <link> to a .css file (external). React keeps the same ideas, with JavaScript-friendly spelling:

HTMLReactNotes
style="color: red"style={{ color: "red" }}An object, not a string
class="card"className="card"class is a reserved word in JavaScript
<link rel="stylesheet">import "./Card.css"Your bundler (Vite, Parcel) adds it to the page

Every approach below ends up as one of those two props. Click through them and watch the card stay exactly the same:

Same card, seven ways

Try it

the code

const card = {
  padding: 16,          // a number means px
  borderRadius: 12,
  backgroundColor: "linen",
};

<div style={card}>…</div>

what renders

Masala Dosa

Crispy, with coconut chutney

₹120

Add
Scoped to the component?
Yes, one element
Hover & media queries?
No :hover, no media queries
Cost
Nothing to install

Great for values that change at runtime (a progress bar's width). Painful for whole designs.

The card on screen never changes. Only where its styles live changes, and with it what you can do (hover, media queries) and what it costs.

1 · Inline style objects

ProgressBar.jsx
function ProgressBar({ percent }) {
  return (
    <div style={{ height: 8, backgroundColor: "lightgray", borderRadius: 4 }}>
      <div style={{ width: `${percent}%`, height: "100%", backgroundColor: "tomato" }} />
    </div>
  );
}
  • Property names are camelCase: backgroundColor, not background-color.
  • A plain number means pixels for most properties (height: 8 is 8px). Unitless ones like opacity, zIndex and lineHeight stay as numbers.
  • No :hover, :focus, media queries or ::before. That's the big limit.

Use it for values that are only known at runtime, like the width above. Don't build a whole design with it.

2 · Plain CSS files

Card.jsx
import "./Card.css";
 
export default function Card({ children }) {
  return <div className="card">{children}</div>;
}

Real CSS, so hover and media queries work. The catch: it's global. Importing it inside Card.jsx doesn't keep it inside Card. If another file also defines .card, the two fight. Teams avoid that with naming rules like BEM (.card__title--active).

3 · CSS Modules

Name the file *.module.css and the class names become local to the file. Vite and Parcel support this with no setup.

Card.jsx
import styles from "./Card.module.css";
 
export default function Card({ active, children }) {
  return <div className={`${styles.card} ${active ? styles.active : ""}`}>{children}</div>;
}

The build renames .card to something unique like _card_x7f2a, and styles.card holds that name. Plain CSS, no clashes. A solid default if you like writing CSS.

4 · Sass

Sass is CSS with extra powers: variables ($radius), nesting and mixins. Vite understands .scss files once you install the compiler:

npm install -D sass-embedded

Name it Card.module.scss to get scoping too. Modern CSS now has its own variables (--radius) and nesting, so you need Sass less than you used to.

5 · CSS-in-JS (styled-components, Emotion)

You write CSS inside JavaScript, and each style becomes a component:

Button.jsx
import styled from "styled-components";
 
const Button = styled.button`
  padding: 8px 16px;
  border-radius: 8px;
  background: ${(props) => (props.$primary ? "tomato" : "lightgray")};
`;
 
<Button $primary>Order now</Button>;

Styles sit next to the component and can read props. But the library builds the CSS in the browser while your app runs, which costs time. Runtime CSS-in-JS also doesn't work in React Server Components (used by Next.js), and styled-components' maintainer put it in maintenance mode in 2025 and advised against it for new projects. You'll still meet it in older codebases.

6 · Component libraries

Instead of styling a button yourself, you use one that's already built, styled and accessible:

  • MUI (Material UI): Google's Material Design look, huge set of components.
  • Chakra UI and Mantine: friendly APIs, easy theming.
  • shadcn/ui: not installed as a package. A command copies the component code into your project, styled with Tailwind, so you own and edit it.

Great for dashboards and admin tools where speed matters more than a unique look. The trade-off: a large dependency, and making it not look like everyone else's app takes work.

Which one should you pick?

You want…Reach for
A value that changes every render (width, position)Inline style
To write normal CSS without clashesCSS Modules
Speed, consistency and small CSSTailwind (next topics)
Ready-made accessible componentsA component library or shadcn/ui
To maintain an older appWhatever it already uses

Mixing is normal: Tailwind for layout plus an inline style for one dynamic value is a common, healthy combo.

Gotchas

Recall

Can you answer these?

0/5

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.