Style a component with CSS Modules
Keep styles with the component
React describes the UI; CSS controls its appearance. A CSS Module is a stylesheet processed by a build tool so local class names are mapped to generated names. Vite supports files ending in .module.css. Use the first two files together in the Chapter 3 client project, with reviewed React and Vite versions. The CSS filename is part of the import contract; JSX alone cannot perform this transformation.
import React from 'react';import styles from './Card.module.css';export default function App() { return <article className={styles.card}><h2>River stones</h2><p>Gallery A</p></article>;}.card { padding: 1rem; border: 1px solid #64748b; border-radius: .5rem; color: #172554; background: #fff; }.featured { border-width: 3px; }.card:focus-within { outline: 2px solid #1d4ed8; outline-offset: 3px; }The expected card has padding, a visible border and rounded corners. The import object maps card to a generated class name. It is not a React component. CSS Modules reduce class-name collisions, not the whole cascade: inherited values, global rules and selector specificity still matter. A normal CSS file imported for side effects instead uses global selectors. A typo such as styles.cards can yield undefined rather than the intended class.
Combine classes and inline values
import React from 'react';import styles from './Card.module.css';function cardClass(base, featured, active) { return active ? `${base} ${featured}` : base;}function Card({name, featured, width}) { return <article className={cardClass(styles.card, styles.featured, featured)} style={{maxWidth: width}}> <h2>{name}</h2><p>{featured ? 'Featured exhibit' : 'Standard exhibit'}</p> </article>;}export default function App() { return <main><Card name="River stones" featured={true} width={320} /> <Card name="Forest seeds" featured={false} width={320} /></main>;}Both cards use the base class; only the featured card gets a thicker border and a textual status. The numeric maxWidth becomes a pixel value; unitless properties such as opacity behave differently. Inline styles take JavaScript objects with camelCase property names; they cannot directly express pseudo-classes or media queries. Prefer CSS for stable rules and inline values for bounded dynamic data. Do not construct arbitrary styles from untrusted input. Utility CSS uses predefined classes; Tailwind v4.3 documentation describes a Vite plugin and CSS import workflow, unlike older v3 setup. CSS-in-JS libraries may generate styles at build time or runtime; evaluate bundle, SSR, CSP and maintenance trade-offs. Neither is required by React. No third-party styling package is installed or tested here.
Practice
Add a second local variant class and combine it with the base class. Verify that a non-featured card retains its base appearance. Check generated names in a real Vite build before treating module isolation as proven.