Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Use CSS in React

Use className to connect JSX elements to CSS classes, reserve inline styles for JavaScript-driven values, and load stylesheets through your project’s tool or framework.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use className in JSX to connect an element to an ordinary CSS class, then define that class in a stylesheet. Use React’s style prop when a style value comes from JavaScript data, and use conditional class names when a component’s state changes which reusable styles apply. How the CSS file gets loaded depends on your project’s build tool or framework.

Connect a CSS class to a React element

In JSX, write className rather than the HTML attribute class. Its value is the name of a CSS class, which you define in a stylesheet as usual.

// Card.jsx
import './Card.css';

export default function Card() {
  return (
    <article className="card">
      <h2 className="card__title">Profile</h2>
    </article>
  );
}
/* Card.css */
.card {
  padding: 1rem;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
}

.card__title {
  margin: 0;
  color: #222;
}

import './Card.css'; is a common bundler convention, not a universal React rule. React does not prescribe how CSS files are loaded: use the CSS-loading approach supported by your build tool or framework. In a simple HTML project, a stylesheet can instead be connected with a <link> element.

Choose classes or inline styles

Prefer CSS classes for values known ahead of time and styles you want to reuse, including rules that use selectors, pseudo-classes, or media queries. React recommends using the style attribute when styles depend on JavaScript variables.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The style prop takes a JavaScript object. Property names use camelCase, and numeric values are generally rendered with px unless the CSS property is unitless.

function Avatar({ size }) {
  return (
    <img
      className="avatar"
      style={{ width: size, height: size }}
      src="/avatar.png"
      alt=""
    />
  );
}

Here the class handles reusable avatar styling while the dimensions vary with the size value. For example, backgroundColor is the JavaScript style-property spelling corresponding to CSS background-color.

Apply styles conditionally

Build the className string with ordinary JavaScript. Keep the base class and add a modifier class when its condition is true.

import './Card.css';

export default function Card({ selected }) {
  return (
    <article className={selected ? 'card card--selected' : 'card'}>
      <h2 className="card__title">Profile</h2>
    </article>
  );
}
.card--selected {
  border-color: royalblue;
  background: #f3f7ff;
}

For more involved combinations, a helper such as classnames is optional; it can make class composition easier to read, but is not required by React.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What changes with React 19 stylesheet elements?

React 19 supports rendering stylesheet <link> and <style> elements as part of the rendered tree. A stylesheet link can use the precedence prop to let React manage stylesheet ordering in the document head; identical linked stylesheets can be deduplicated. This is a React 19 capability, not a requirement for using CSS in React. Whether it fits your app depends on its rendering architecture and framework.

Type an inline style object in TypeScript

Use React.CSSProperties to type an inline style object when you need to declare one separately.

import type { CSSProperties } from 'react';

const avatarStyle: CSSProperties = {
  width: 48,
  height: 48,
};

Then pass it to an element with style={avatarStyle}. Keep values that are fixed and reusable in CSS where practical; reserve the object for values driven by JavaScript.

Troubleshoot styles that do not appear

  • The class has no effect: Check that the JSX uses className, that its value matches the selector spelling in the CSS file, and that the stylesheet is actually loaded.
  • The stylesheet import fails: Confirm that your build tool or framework supports importing CSS from that location, and follow its stylesheet-loading instructions. A CSS import is not built into JSX as a universal loading mechanism.
  • An inline value looks wrong: Check camelCase property names and remember that numeric values normally gain a px unit unless the property is unitless. Use an explicit string when a different unit is needed, such as width: '50%'.
  • A conditional appearance never changes: Verify the condition’s value and inspect the resulting class string. Ensure the modifier selector is defined in a stylesheet that is loaded.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Capture a rendered page for review

If you need a screenshot of a deployed React page, first make the page reachable at a URL. A screenshot service cannot replace the CSS setup above; it captures the result after the browser renders the page.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Or skip the browser setup

ScreenshotNeo offers a screenshot API and MCP server. A GET request can return an image or PDF, which can be useful for capturing a rendered page without configuring a local browser automation flow. For example, this cURL request saves a WebP screenshot of a URL you can access:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Replace the example URL with your page URL and set your API key. See the ScreenshotNeo documentation for request options. Cookie banners, popups, and chat widgets are removed before capture; bot checks, blank pages, and failed loads are not billed. Its MCP server lets AI agents take screenshots. The Free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.