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

154
Vistas
How to change css property in react

I want to change css width property of my element on some condition

<div className="consoleLayoutRoot-sideMenu">
        <ConsoleSideMenu />
 </div>

css

.consoleLayoutRoot-sideMenu .ant-menu {
  padding-top: 30px;
 
  /* background-color: #191146 !important; */
}

I am doing this way..but nothing is happening

document.getElementsByClassName("conjnjnot-sideMenjnjbhbhu.annjn ").style.width = "77px";
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

That's not working because you're treating a list as though it were an element. But it's also fundamentally not how you would do this in a React project.

Instead, you'd have the component re-render when the condition becomes true (perhaps by setting a state member). When rendering the div, you optionally include a style or a class name depending on whether you want the width applied:

<div className={`consoleLayoutRoot-sideMenu ${shouldHaveWidthClass ? "width-class" : ""}`}>
        <ConsoleSideMenu />
</div>

...where .width-class { width: 50px; } is in your stylesheet.

Or with inline style, but inline styles are best avoided:

<div className="consoleLayoutRoot-sideMenu" style={shouldHaveWidthSetting ? { width: "50px" } : undefined}>
        <ConsoleSideMenu />
</div>

Here's an example (using a class);

const {useState} = React;

const ConsoleSideMenu = () => <span>x</span>;

const Example = () => {
    const [includeWidth, setIncludeWidth] = useState(false);
    
    const toggle = ({currentTarget: { checked }}) => {
        setIncludeWidth(checked);
    };
    
    return <React.Fragment>
        <div className={`consoleLayoutRoot-sideMenu ${includeWidth ? "width-class" : ""}`}>
                <ConsoleSideMenu />
        </div>
        <label>
            <input type="checkbox" onChange={toggle} checked={includeWidth} />
            Include width class
        </label>
    </React.Fragment>;
};

ReactDOM.render(<Example />, document.getElementById("root"));
.width-class {
    width: 50px;
}
.consoleLayoutRoot-sideMenu {
    border: 1px solid blue;
}
<div id="root"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar
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