Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
How to apply css for a class in react component?

I am a new react-js learner and I am having a hard time adding css to my classes that I have inside my react component.

Here is the current code:

import React from 'react';

const Home = () => {
  return (
    <>
      <div class="container">
        <h1 class="mainHeader">Home</h1>
        <h2>helloo</h2>
      </div>
    </>
  );
};

.container {
// CSS would go here
}



export default Home;

In just HTML and CSS, I was able to apply css on the container div class by just using '.' and whatever the class name was. However, this is giving me an error.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Put the css in its own file, with a .css extension, then import it. Assuming you used create-react-app to set up your project, it will already have appropriate configuration for importing css files. Additionally, you need to use className for the prop, not class

// In a new file home.css:
.container {
  // css goes here
}
// In the file you've shown:
import React from 'react';
import './home.css';

const Home = () => {
  return (
    <>
      <div className="container">
        <h1 className="mainHeader">Home</h1>
        <h2>helloo</h2>
      </div>
    </>
  );
};

export default Home;
about 4 years ago · Juan Pablo Isaza Report

0

Or you can declare it in json format or like you would an object, not in CSS form. Treat it as you are writing in js, which you actually are. See the edit below:

import React from 'react';

const Home = () => {
  return (
    <>
      <div style={container}>
        <h1 className="mainHeader">Home</h1>
        <h2>helloo</h2>
      </div>
    </>
  );
};

const container = {
// CSS would go here
  color: 'red',
  background: 'blue'
}

export default Home;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!