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

151
Views
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 answers
Answer question

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 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!