🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to React Notes
Topic #38

React CSS Styling


There are many ways to style React with CSS, this tutorial will take a closer look at three common ways:

  • Inline styling
  • CSS stylesheets
  • CSS Modules

Inline Styling

To style an element with the inline style attribute, the value must be a JavaScript object:

Example:

const Header = () => {
  return (
    <>
      <h1 style={{color: "red"}}>Hello Style!</h1>
      <p>Add a little style!</p>
    </>
  );
}

Note: In JSX, JavaScript expressions are written inside curly braces, and since JavaScript objects also use curly braces, the styling in the example above is written inside two sets of curly braces {{}}.


camelCased Property Names

Since the inline CSS is written in a JavaScript object, properties with hyphen separators, like background-color, must be written with camel case syntax:

Example:

const Header = () => {
  return (
    <>
      <h1 style={{backgroundColor: "lightblue"}}>Hello Style!</h1>
      <p>Add a little style!</p>
    </>
  );
}

JavaScript Object

You can also create an object with styling information, and refer to it in the style attribute:

Example:

const Header = () => {
  const myStyle = {
    color: "white",
    backgroundColor: "DodgerBlue",
    padding: "10px",
    fontFamily: "Sans-Serif"
  };
  return (
    <>
      <h1 style={myStyle}>Hello Style!</h1>
      <p>Add a little style!</p>
    </>
  );
}

CSS Stylesheet

You can write your CSS styling in a separate file, just save the file with the .css file extension, and import it in your application.

Example

body {
  background-color: #282c34;
  color: white;
  padding: 40px;
  font-family: Sans-Serif;
  text-align: center;
}

Note: You can call the file whatever you like, just remember the correct file extension.

Import the stylesheet in your application:

Example

import { createRoot } from 'react-dom/client';
import './MyStylesheet.css';

const Header = () => {
  return (
    <>
      <h1>Hello Style!</h1>
      <p>Add a little style!.</p>
    </>
  );
}

createRoot(document.getElementById('root')).render(
  <Header />
);

React knows that files imported with the .css extension, should be treated as a CSS Stylesheet.

Note: The styles will only be available for the component that imported it.


CSS Modules

Another way of adding styles to your application is to use CSS Modules.

CSS Modules are convenient for components that are placed in separate files.

Note: The CSS inside a module is available only for the component that imported it, and you do not have to worry about name conflicts.

Create the CSS module with the .module.css extension, example: my-style.module.css.

Example

.bigred {
  color: Tomato;
  padding: 40px;
  font-family: Sans-Serif;
  text-align: center;
}

Import the stylesheet in your component:

Example

import { createRoot } from 'react-dom/client';
import styles from './my-style.module.css';

const Car = () => {
  return <h1 className={styles.bigred}>Hello Car!</h1>;
}

createRoot(document.getElementById('root')).render(
  <Car />
);

You will learn more about CSS Modules in the next chapter.

Want to go beyond the notes?

Join CodingNow 2.0's React course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

React CSS Styling – FAQs

Quick answers about learning React CSS Styling in React.

This free note from CodingNow 2.0 explains React CSS Styling in React — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every React topic on CodingNow 2.0, including React CSS Styling, is 100% free with no signup required.
With focused practice, most students grasp React CSS Styling in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now