react js

React CSS Styling

Styling in React is flexible. You can style components in many ways depending on your project size, team preferences, and required features like theme switching or scoped styles.

Start Learning React

React does not enforce a single styling method, so understanding your options is important.

1. Traditional CSS (External Stylesheets)

This is the simplest way to style React components.

How It Works

Create a .css file and import it into your component or globally.

Example

App.css

.title {
  color: royalblue;
  font-size: 28px;
  font-weight: bold;
}

App.jsx

import './App.css';

export default function App() {
  return <h1 className="title">Hello React</h1>;
}

Pros

  • Easy for beginners
  • Great for small projects
  • Works like normal HTML/CSS

Cons

  • Styles are global → naming conflicts possible
  • Not ideal for large applications

2. Inline Styling

Styles are passed directly as an object to the element.

Example

export default function App() {
  const titleStyle = {
    color: "crimson",
    fontSize: "24px",
  };

  return <h1 style={titleStyle}>Inline Style Example</h1>;
}

Pros

  • Useful for dynamic styles
  • No external file needed

Cons

  • No pseudo-selectors (like :hover)
  • No media queries
  • Hard to maintain

3. CSS Modules (Scoped CSS)

A very popular method for avoiding style conflicts.
CSS Modules automatically scope class names to each component.

Example

Button.module.css

.btn {
  background: dodgerblue;
  border: none;
  padding: 12px 20px;
  color: white;
  border-radius: 5px;
}

Button.jsx

import styles from './Button.module.css';

export default function Button() {
  return <button className={styles.btn}>Click Me</button>;
}

Pros

  • Styles are scoped → no conflicts
  • Supports media queries, animations, etc.
  • Easy to reason about

Cons

  • Class names become hashed
  • Slightly more setup

4. Styled Components (CSS-in-JS)

A popular library that lets you write real CSS directly in JavaScript.
Perfect for component-based styling and theming.

Example

import styled from "styled-components";

const Button = styled.button`
  background: purple;
  padding: 10px 20px;
  color: white;
  border-radius: 6px;

  &:hover {
    background: indigo;
  }
`;

export default function App() {
  return <Button>Styled Button</Button>;
}

Pros

  • Scoped styles by default
  • Supports props & dynamic styling
  • Built-in theming support
  • Great for design systems

Cons

  • Adds extra library weight
  • Harder debugging sometimes

5. Emotion (Another CSS-in-JS)

Similar to styled-components but more flexible and performant.

Example

/** @jsxImportSource @emotion/react */import { css } from "@emotion/react";

const style = css`
  color: teal;
  font-size: 30px;
`;

export default () => <h1 css={style}>Hello Emotion</h1>;

6. Tailwind CSS

A utility-first CSS framework recommended for modern apps.

Example

export default function App() {
  return <h1 className="text-3xl font-bold text-red-500">Tailwind in React</h1>;
}

Pros

  • Very fast to build UI
  • Small CSS output when purged
  • Great for teams

Cons

  • HTML becomes cluttered
  • Learning curve for utility-first classes

7. CSS Frameworks (Bootstrap, Material UI, Chakra UI)

React apps can use external UI libraries.

Example (Material UI)

import Button from '@mui/material/Button';

export default function App() {
  return <Button variant="contained">Material UI Button</Button>;
}

Pros

  • Prebuilt components
  • Fast development
  • Great consistency

Cons

  • Limited design customization
  • You rely on library updates

8. Dynamic & Conditional Styling in React

React makes it easy to apply styles based on state.

Example

export default function App() {
  const [active, setActive] = useState(false);

  return (
    <button
      className={active ? "btn btn-active" : "btn"}
      onClick={() => setActive(!active)}
    >
      Toggle
    </button>
  );
}

Dynamic Inline Styles

<div style={{ background: active ? "green" : "gray" }}>
  Status Box
</div>

9. Using classNames Library

Helps manage conditional class names.

import classNames from "classnames";

const btn = classNames("btn", {
  "btn-active": active,
  "btn-disabled": disabled,
});

10. Global Styles with CSS-in-JS

With styled-components:

import { createGlobalStyle } from "styled-components";

const Global = createGlobalStyle`
  body {
    background: #111;
    color: #fff;
  }
`;

Best Practices for React Styling

✔ Prefer CSS Modules or CSS-in-JS for large apps

✔ Use Tailwind for rapid UI development

✔ Keep styles close to components

✔ Use meaningful class names

✔ Avoid inline styles for large components

✔ Build a design system for scalability

Summary Table

MethodScoped?Supports Pseudo?Best For
CSS FileSmall apps
InlineDynamic styles
CSS ModulesMedium/large apps
Styled ComponentsDesign systems
EmotionPerformant apps
TailwindFast UI building
FrameworksQuick prototypes

Recent Posts

Linux Steam Locomotive Bash program

What is Steam Locomotive (sl)? Steam Locomotive (sl) is a small terminal program on Unix/Linux systems…

1 week ago

Rate Limiting in Node JS

What is Rate Limiting? Download this article as a PDF on the Codeflare Mobile App…

3 weeks ago

JavaScript promise chaining

Learn on the Go. Download the Codeflare Mobile from iOS App Store.  1. What is…

3 weeks ago

UI/UX Design — Explained Like You’re 5

Download the Codeflare iOS app and learn on the Go 1. What UI and UX…

1 month ago

Costly Linux Mistakes Beginners Make

1. Running Everything as Root One of the biggest beginner errors. Many new users log…

2 months ago

How Keyloggers Work

A keylogger is a type of surveillance software or hardware that records every keystroke made…

2 months ago