Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda