Skip to content

Styling Components

The Course
•
15 min study

React doesn't have its own styling language. You style components with CSS, like any web page. There are two everyday ways, and this lab shows both.

Classes from props React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import Badge from "./Badge.jsx"; export default function App() { return ( <main> <h1>Priorities</h1> <p>Review JSX <Badge level="high" /></p> <p>Tidy the CSS <Badge level="low" /></p> <p style={{ fontStyle: "italic", marginTop: "2rem" }}>Styles can live in CSS or in JSX.</p> </main> ); } export default function Badge({ level }) { return <span className={`badge badge-${level}`}>{level}</span>; } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; } .badge { display: inline-block; padding: 0 0.5rem; border-radius: 999px; font-size: 0.8rem; font-weight: 600; text-transform: uppercase; } .badge-high { background: #fdecea; color: #b42318; } .badge-low { background: #e6f7fc; color: #06617f; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

A class chosen by a prop

Badge.jsx builds its class name from its level prop:

JSX
<span className={`badge badge-${level}`}>{level}</span>

With level="high", the class is badge badge-high. styles.css has a rule for .badge (the shape) and rules for .badge-high and .badge-low (the colours). So the component decides which class to use, and the CSS file decides what that class looks like.

Add a third line to App, <p>Learn hooks <Badge level="medium" /></p>, and run it. The badge has its shape but no colour, because there's no .badge-medium rule yet. Add one to styles.css:

CSS
.badge-medium {
  background: #fff4dc;
  color: #8a5a00;
}

The style prop

App.jsx also styles one paragraph directly:

JSX
<p style={{ fontStyle: "italic", marginTop: "2rem" }}>...</p>

Two things look different from HTML:

  • Double braces. The outer pair means "a JavaScript value". The inner pair is that value: an object.
  • camelCase names. CSS's font-style becomes fontStyle, and margin-top becomes marginTop. Values are strings, so they go in quotes.

Inline styles are handy for a value you calculate, like a progress bar's width. For everything else, prefer classes: they keep the styling in one place and are easier to change.

Predict: what if you write style the HTML way?

Change the paragraph to style="font-style: italic" and run it. React stops with an error: The style prop expects a mapping from style properties to values, not a string. Change it back to the object form.

Your turn

Exercise

A done style

Write a TaskLine component with props title and done. It shows the title in a <p>. When done is true, the paragraph also gets the class done, which draws a line through the text and turns it grey. Show one finished and one unfinished task in App.

Your working space React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import Badge from "./Badge.jsx"; export default function App() { return ( <main> <h1>Priorities</h1> <p>Review JSX <Badge level="high" /></p> <p>Tidy the CSS <Badge level="low" /></p> <p style={{ fontStyle: "italic", marginTop: "2rem" }}>Styles can live in CSS or in JSX.</p> </main> ); } export default function Badge({ level }) { return <span className={`badge badge-${level}`}>{level}</span>; } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; } .badge { display: inline-block; padding: 0 0.5rem; border-radius: 999px; font-size: 0.8rem; font-weight: 600; text-transform: uppercase; } .badge-high { background: #fdecea; color: #b42318; } .badge-low { background: #e6f7fc; color: #06617f; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea

There are two pieces. The CSS file needs a rule for a class called done (which CSS properties draw a line through text and change its colour?). The component needs a className that is "done" when done is true and empty otherwise. Which JavaScript operator picks one of two values?

Hint 2: a first step

Start with the CSS: add a .done rule with text-decoration: line-through, then write a fixed className="done" on the <p> for now. Both tasks are struck through. That proves the rule works; the last step is to make the class depend on the done prop.

Show one solution
JSX
function TaskLine({ title, done }) {
  return <p className={done ? "done" : ""}>{title}</p>;
}
JSX
<TaskLine title="Learn props" done={true} />
<TaskLine title="Learn state" done={false} />

And in styles.css:

CSS
.done {
  text-decoration: line-through;
  color: #6b7785;
}

Was this article helpful?

Your response is saved on this device.