Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

React CSS-in-JS


What is CSS-in-JS?

CSS-in-JS is a styling technique where you can write CSS directly in your JavaScript code.

This approach allows you to:

  • Write CSS using JavaScript
  • Create component-scoped styles
  • Use dynamic styles based on props
  • Avoid CSS class name conflicts

In this tutorial, we'll use the popular styled-components library.


Getting Started

CSS-in-JS is not a part of the React core library, but can be installed using many React build tools, like Vite, Webpack, or Create React App.

To install styled-components, run the following command:

npm install styled-components

Now you can start writing CSS directly in your .jsx files:

Example

Insert styles directly in the .jsx files:

import styled from 'styled-components';

const MyHeader = styled.h1`
  padding: 10px 20px;
  background-color: #007bff;
  color: white;
`;

function App() {
  return (
    <>
      <MyHeader>Welcome!</MyHeader>
    </>
  );
}

Run Example »

Without CSS-in-JS, you would have to either:

  • Write CSS in a separate .css file and import it into your React components
  • Use inline styles

Notice that:

  • We use the styled object to create components
  • Styles are written inside template literals (backticks)
  • You can use regular CSS syntax

Props in Styled Components

Another powerful feature of CSS-in-JS is the ability to use props to make styles dynamic.

Let's create an example where we have two buttons, one primary and one secondary.

We want to base their background color on the value of the btntype prop:

Example

import styled from 'styled-components';

const Button = styled.button`
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  background-color: ${props => props.btntype === 'primary' ? '#007bff' : '#6c757d'};
  color: white;
  cursor: pointer;
`;

function App() {
  return (
    <div>
      <Button btntype="primary">Primary Button</Button>
      <br />
      <br />
      <Button>Secondary Button</Button>
    </div>
  );
}

Run Example »


Extending Styles

Another way of letting multiple elements have the same styles is to extend existing styled components.

For example, we can create a PrimaryButton and SuccessButton that both have the same styles as the Button component, but they also have separate background colors:

Example

import styled from 'styled-components';

const Button = styled.button`
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  color: white;
  cursor: pointer;
`;

const PrimaryButton = styled(Button)`
  background-color: #007bff;
`;

const SuccessButton = styled(Button)`
  background-color: #28a745;
`;

function App() {
  return (
    <div>
      <PrimaryButton>Primary</PrimaryButton>
      <SuccessButton>Success</SuccessButton>
    </div>
  );
}

Run Example »


Component-Scoped Styles

Just like with CSS Modules, styles created in CSS-in-JS are scoped to the component.

They are given unique names and can only be used in the component that imports them.

If we view the source of the page in the example above, the buttons will have unique class names like bSOFjJ and pkxvl, and will be refering to the CSS with the same unique class names in the style section.

React CSS in JS

Global Styles

You can also create global styles, which will be given normal class name and will be available for all components:

Example

import { createGlobalStyle } from 'styled-components';

const GlobalStyle = createGlobalStyle`
  h1 {
    color: white;
    background-color: purple;
    font-family: Arial, sans-serif;
  }

  .myparagraph {
    font-family: courier, monospace;
    color: blue;
  }
`;

function App() {
  return (
    <>
      <GlobalStyle />
      <h1>Welcome!</h1>
      <p className="myparagraph">This paragraph is styled with global styles.</p>
    </>
  );
}

Run Example »

If we view the source of the result in the example above, the CSS will have normal names and will be available for all components.

React CSS in JS

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.