Without a working example, it's hard to say, but here's a hide/show toggle example (click Run code snippet). Try copying/pasting this example into your project and seeing if it works. If it doesn't, then there's something not set up properly with your project, where stylesheets aren't being properly imported.

class App extends React.Component {
  constructor(props) { 
    super(props);
    
    this.state = { clicks: 0, hidden: false };
    
    this.handleIncreaseClicks = this.handleIncreaseClicks.bind(this);
    this.handleButtonDisplay = this.handleButtonDisplay.bind(this);
  } 
   
  handleIncreaseClicks() {
    this.setState(state => ({ clicks: state.clicks + 1 }));
  }  
   
  handleButtonDisplay() {
    this.setState(state => ({ hidden: !state.hidden }));
  }

  render() {   
    return(
      <React.Fragment>
        <div className={`container ${this.state.hidden ? "hide-me" : ""}`}>
          <p className="label">Clicks:</p>
          <button 
            className="clicks" 
            onClick={this.handleIncreaseClicks}
          >
            {this.state.clicks}
          </button>
          <br />
        </div>
        <button 
          className="hide-show-button" 
          onClick={this.handleButtonDisplay}
        >
          {this.state.hidden ? "Show" : "Hide"} Clicks
        </button>
      </React.Fragment>
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
);
.container {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.label {
  font-weight: 700;
  margin-right: 10px;
}

.hide-show-button {
  cursor: pointer;
  margin: 0 5px;
  text-align: center;
  font-size: 14px;
  width: 100px;
  padding: 4px;
  border-radius: 3px;
  border: 1px solid #333333;
  transition: all 0.2s ease-in-out;
}

.hide-show-button {
  background-color: #f56342;
  color: white;
}

.hide-show-button:hover {
  background-color: #be391c;
}

.clicks {
  cursor: pointer;
  font-size: 14px;
  width: 100px;
  padding: 4px;
  border-radius: 3px;
  border: 1px solid #333333;
  text-align: center;
  transition: all 0.2s ease-in-out;
}

.clicks:hover {
  background-color: #c1c1c1;
}

.clicks:focus, .hide-show-button:focus {
  outline: 0;
}

.hide-me {
  display: none
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>


<div id='root'>
</div>

Answer from Matt Carlotta on Stack Overflow
Top answer
1 of 5
3

Without a working example, it's hard to say, but here's a hide/show toggle example (click Run code snippet). Try copying/pasting this example into your project and seeing if it works. If it doesn't, then there's something not set up properly with your project, where stylesheets aren't being properly imported.

class App extends React.Component {
  constructor(props) { 
    super(props);
    
    this.state = { clicks: 0, hidden: false };
    
    this.handleIncreaseClicks = this.handleIncreaseClicks.bind(this);
    this.handleButtonDisplay = this.handleButtonDisplay.bind(this);
  } 
   
  handleIncreaseClicks() {
    this.setState(state => ({ clicks: state.clicks + 1 }));
  }  
   
  handleButtonDisplay() {
    this.setState(state => ({ hidden: !state.hidden }));
  }

  render() {   
    return(
      <React.Fragment>
        <div className={`container ${this.state.hidden ? "hide-me" : ""}`}>
          <p className="label">Clicks:</p>
          <button 
            className="clicks" 
            onClick={this.handleIncreaseClicks}
          >
            {this.state.clicks}
          </button>
          <br />
        </div>
        <button 
          className="hide-show-button" 
          onClick={this.handleButtonDisplay}
        >
          {this.state.hidden ? "Show" : "Hide"} Clicks
        </button>
      </React.Fragment>
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
);
.container {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.label {
  font-weight: 700;
  margin-right: 10px;
}

.hide-show-button {
  cursor: pointer;
  margin: 0 5px;
  text-align: center;
  font-size: 14px;
  width: 100px;
  padding: 4px;
  border-radius: 3px;
  border: 1px solid #333333;
  transition: all 0.2s ease-in-out;
}

.hide-show-button {
  background-color: #f56342;
  color: white;
}

.hide-show-button:hover {
  background-color: #be391c;
}

.clicks {
  cursor: pointer;
  font-size: 14px;
  width: 100px;
  padding: 4px;
  border-radius: 3px;
  border: 1px solid #333333;
  text-align: center;
  transition: all 0.2s ease-in-out;
}

.clicks:hover {
  background-color: #c1c1c1;
}

.clicks:focus, .hide-show-button:focus {
  outline: 0;
}

.hide-me {
  display: none
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>


<div id='root'>
</div>

2 of 5
2

I ran into the same issue as you and fixed by doing a forced refresh. New styles aren't applied on a regular refresh since the browser is using the cached version of that page. A cache issue might explain how this magically went away for you.

You can do a force refresh and clear the page click by shift-clicking the refresh button in Chrome or Firefox.

Refer to this article for more

🌐
Quora
quora.com › Why-is-React-not-reading-a-CSS-file-React-js
Why is React not reading a CSS file (React.js)? - Quora
Answer: This is not a React problem, it is either a loading or css (class specificity, etc) problem. In the css file, add an [code ]!important[/code] css rule to turn all text red, targeting the body tag.
🌐
DhiWise
dhiwise.com › post › troubleshooting-guide-how-to-fix-react-css-not-applying
Troubleshooting Guide: Resolving React CSS Not Applying
May 30, 2024 - Verify that your CSS file is being loaded correctly in your React application. Check the network tab in browser dev tools to ensure that the CSS file is not missing. Use developer tools to inspect elements and see which styles are being applied ...
🌐
Reddit
reddit.com › r/react › css style not applying
r/react on Reddit: CSS style not applying
March 5, 2023 -

Hello, this is my first time working with React and I'm running into some issues.

I created a CSS file but it is not applying the styles when I use className or id. However it is applying the style when I use only

the html elements (i.e. h1, input, ul, etc).

I saved the css file in the src folder and wrote the import statement so I am very confused as to what's going on.

Please let me know if further information is needed and thank you in advance!

🌐
GitHub
github.com › ReactJSResources › react-webpack-babel › issues › 241
Css don't apply to react components · Issue #241 · ReactJSResources/react-webpack-babel
September 17, 2016 - Hello, don't know if I am doying something wrong, but I imported partial scss files into index.scss file and written few react components included into app.js and the styles doesn't aply to them. I...
Author   andylacko
🌐
GitHub
github.com › facebook › create-react-app › issues › 13597
CSS is not working correctly after applying the React code Splitting. · Issue #13597 · facebook/create-react-app
May 23, 2024 - if I go to another component and come back the CSS is working correctly. ... const Search = lazy(() => import("./search/search")); <Suspense fallback={<Shimmer />}> <Search /> </Suspense> ... Note: The CSS was working before adding the code splitting.
Published   May 23, 2024
Author   lalit-tudip
🌐
Render
community.render.com › t › css-errors-when-deploying-react-app › 8053
CSS errors when deploying React app - Render
November 9, 2022 - Hello! I’ve been having some troubles deploying my React app as a static app. The app is correctly deployed and working well, but my App.css file is not taken into account. When running the app locally, I’ve got no issue, but when deploying on Render, the styles specified in the App.css ...
Top answer
1 of 7
13

In a react project created with create-react-app or npx create-react-app, I also had the same issue.

I had imported index.css file in my App Component but my styles were not being applied properly to my React Components.

I even tried directly adding index.css file in my html file in the public folder and using link tag to link to my index.css file (which resides within src folder).

<link rel="stylesheet" href="./../src/index.css">

That also didn't work.

Finally, I read an article about 7 ways to apply CSS into React. One best way was to install node-sass into our project and use index.scss ( import './index.scss') into App Component instead of index.css.

And Hurray!!! My CSS worked fine, All the Media Queries started to work fine.

Below is the code snippet you can try.

import React from "react";
import ReactDom from "react-dom";
import './index.scss';


// --- Data to work with ---
const books = [
  {
    id: 1,
    name: 'The Rudest Book Ever',
    author: 'Shwetabh Gangwar',
    img: 'https://m.media-amazon.com/images/I/81Rift0ymZL._AC_UY218_.jpg'
  },
  {
    id: 2,
    name: 'The Rudest Book Ever',
    author: 'Shwetabh Gangwar',
    img: 'https://m.media-amazon.com/images/I/81Rift0ymZL._AC_UY218_.jpg'
  },
  {
    id: 3,
    name: 'The Rudest Book Ever',
    author: 'Shwetabh Gangwar',
    img: 'https://m.media-amazon.com/images/I/81Rift0ymZL._AC_UY218_.jpg'
  },
  {
    id: 4,
    name: 'The Rudest Book Ever',
    author: 'Shwetabh Gangwar',
    img: 'https://m.media-amazon.com/images/I/81Rift0ymZL._AC_UY218_.jpg'
  },
];


const Book = ({ book }) => {
  return (
    <div className={"book"}>

      <img src={book.img} alt="book image" />

      <h3>{book.name}</h3>
      <p>{book.author}</p>
    </div>
  )
};

const Books = () => {
  return (
    <main className={"books"}>
      {
        books.map(book => {
          return (<Book book={book} key={book.id} />)
        })
      }
    </main>
  )
};


// Work a bit fast | one step at a time
const App = () => {
  return (
    <main>
      <h2>Books</h2>
      <Books />
    </main>
  )
}

ReactDom.render(<App />, document.getElementById("root"));
/* --- Mobile First Design --- */
.books{
  text-align: center;
};

.book{
  border: 1px solid #ccc;
  text-align: center;
  width: 200px;
  padding: 1rem;
  background: #001a6e; 
  color: #fff;
  margin:auto;
};

h2{
  text-align: center;
}

/* --- Adding Media Queries --- */
@media only screen and (min-width: 900px){
  .books,.persons{
    display:grid;
    grid-template-columns: 1fr 1fr;
  }
}

To install node-sass, simple do npm install node-sass --save Then rename all your .css files with .scss and your project with work properly.

The package.json should have the node-sass dependency added as shown below:

 "dependencies": {
    "node-sass": "^4.14.1",
    "react": "^16.8.3",
    "react-dom": "^16.8.3",
    "react-scripts": "2.1.5"
  },

Hope this will help many developers :)

2 of 7
4

It would be helpful to see your React component as well.

Given this code, you are passing className as a property into the component rather than assigning a class to it:

export default class App extends Component {

  constructor(props) {
    super(props);
    this.state = {
      data: null
    };
  }

  render() {
    return (
      <div>
        /** This line specifically **/
        <ContactList className="contactList" />
        <ContactDetail />
        <AddContactModal />
      </div>
    );
  }
}

Inside your component, you would need to use className={this.props.className} on a regular HTML tag inside of your component in order to pass the className through.

Find elsewhere
🌐
YouTube
youtube.com › aks programming
How to fix className styles not working in React and JavaScript | Aksprogramming - YouTube
In this tutorial we can resolve className styles not working in React and JavaScript.Just rename the the css or scss file to filename.module and recover thi...
Published   July 2, 2022
Views   5K
🌐
GitHub
github.com › JedWatson › react-select › issues › 3309
Some css styles are not created in production but work in development · Issue #3309 · JedWatson/react-select
September 25, 2018 - The rest of the dynamic css rules (e.g. on the options when you click on the control) do get created in the emotion stylesheet. This only happens on a few pages in our app, while other parts work as expected. Here is a screenshot of the the DOM when the page loads. You'll notice that the select outermost div was assigned a classname of css-10nd86i, but that css rule does not actually exist in the DOM.
Author   jjlauer
🌐
Glitch
support.glitch.com › coding help
Css isn’t working in React - Coding Help - Glitch Community Forum
July 8, 2022 - this has happened in previous projects. regardless of what CSS selectors i use, the styling doesn’t render. even stranger, when i was fussing with the buttons on the bottom to make the styling work, and i deleted the CSS functions for those buttons, the rest of the page, which was working fine, suddenly ceased to render the styles i added. i changed nothing in their functions, however. broken page somewhat unnecessary screenshot: