Rendering DataExtra· 35 min read

Styling React Components

Three ways to style React: CSS classes, CSS Modules, and inline styles.

What you will learn

  • Style with className and a CSS file
  • Use inline style objects
  • Know about CSS Modules

CSS files + className

The most common approach: write normal CSS in a .css file, import it, and use className.

App.css
/* App.css */
.btn { background: #4338ca; color: white; padding: 10px 18px; border: none; border-radius: 8px; }

This is just ordinary CSS in a file called App.css. It defines one class, .btn, with a purple background, white text, padding and rounded corners — nothing React-specific yet.

Import the CSS and use className
// App.jsx
import './App.css';

function App() {
  return <button className="btn">Styled with a CSS class</button>;
}

In the component file we import './App.css' at the top — this tells the build tool to load that stylesheet. Then on the button we write className="btn" to attach the .btn rule. Remember: in JSX it is className, not class. The result is a purple, rounded button.

Note: Output: A purple button with white text and rounded corners reading Styled with a CSS class.

Inline styles (an object)

Inline styles use a JavaScript object with camelCased properties — note the double braces {{ }} (one for JSX, one for the object).

Inline style object (runs live)
function App() {
  const boxStyle = { background: "#06b6d4", color: "white", padding: 14, borderRadius: 10 };
  return <div style={boxStyle}>Styled with an inline style object</div>;
}
Live preview

Here the style is a JavaScript object stored in boxStyle: keys like background and borderRadius with their values. We attach it with style={boxStyle}. The double braces you often see — style={{ ... }} — are simply the object written inline: the outer { } is “JavaScript here”, and the inner { } is the object itself. Note keys are camelCase (borderRadius, not border-radius).

Note: Output: A cyan (teal) box with white text and rounded corners reading Styled with an inline style object.

Note: CSS Modules (Button.module.css) scope styles to one component to avoid name clashes — great for bigger apps. Many teams also use Tailwind CSS (utility classes) with React.

Q. In JSX, inline styles are written as:

Answer: Inline styles take an object: style={{ color: "red", fontSize: 16 }} — camelCase keys, hence the double braces.

✍️ Practice

  1. Style a button with an imported CSS class.
  2. Style a div with an inline style object.

🏠 Homework

  1. Style a small card component using a CSS file and className.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →