Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
Style Element Inside of Component with CSS and React.js

I am trying to make a button that allows the user to switch between light and dark theme. The method I am using is to change the className of my App div. All of the other components in my project are inside of the App component. Then in the individual CSS files for the components I first select the className of the App div which is either .light or .dark. Then I write the name of the component that I want to set a theme color to. For example:

.light p {
  background-color: white;
}

However, this doesn't work.

Here are some of the files:

/*App.js*/
import { useState } from "react";

import TaskPage from "./pages/taskPage";

import { fas } from "@fortawesome/free-solid-svg-icons";
import { library } from "@fortawesome/fontawesome-svg-core";
import { MainContextProvider } from "./store/MainContext";

library.add(fas);

function App() {
  const [theme, setTheme] = useState("light");

  const switchTheme = () => {
    setTheme((prevTheme) => (prevTheme === "light" ? "dark" : "light"));
  };

  return (
    <MainContextProvider>
      <div className={`App ${theme}`}>
        <div className="pageTop">
          <h1 className="title">Task Board</h1>
        </div>
        <button className="button" onClick={switchTheme}>
          Change theme
        </button>
        <TaskPage />
      </div>
    </MainContextProvider>
  );
}

export default App;
/*index.css*/
@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap");

.App {
  overflow-x: hidden;
  padding-bottom: 40px;
  height: 100%;
  width: 100%;
}

.light {
  background-color: #fcfcfc;
}

.dark {
  background-color: #181A1B;
}

.title {
  font-family: "Roboto", sans-serif;
  font-size: 50px;
  font-weight: 700;
  text-align: center;
  margin-top: 40px;
  text-shadow: 0px 3px 4px rgba(0, 0, 0, 0.2);
}

.light .title {
  color: black;
}

.dark .title {
  color: white;
}

.pageTop {
  margin-bottom: 50px;
}
/*todo.module.css*/
.taskCard {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  transition: 0.3s;
  width: 340px;
  height: 260px;
  min-width: 340px;
  min-height: 260px;
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  border-radius: 12px;
  padding: 14px;
  box-sizing: border-box;
}

.light .taskCard {
  background-color: white;
}

.dark .taskCard {
  background-color: #2B2E30;
}

I used this sandbox as an example: https://codesandbox.io/s/stupefied-jackson-0ynz6?file=/src/App.js

I am a beginner with React.js

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda