According to the React Static Boilerplate website, they use CSS Modules - this would explain why the body tag is being respected but the class selector is not.

https://github.com/css-modules/css-modules

try importing the stylesheet like so:

import styles from './MyComponent.css';

The using it in your component like so:

<div className={styles.card}>something!</div>

Answer from Toby on Stack Overflow
🌐
DhiWise
dhiwise.com › post › troubleshooting-guide-how-to-fix-react-css-not-applying
Troubleshooting Guide: Resolving React CSS Not Applying
May 30, 2024 - This problem can arise due to various reasons, such as incorrect imports, conflicts with other styles, or issues with the CSS loader in the project setup. Some common reasons for CSS not being applied in React include:
Discussions

Why is my CSS not applying to my React components?
Say I'm creating a React app and have some CSS for components. I've added the style-loader and css-loader to my webpack config here: module.exports = { mode: 'development', entry: './client/in... More on stackoverflow.com
🌐 stackoverflow.com
CSS errors when deploying React app
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 ... More on community.render.com
🌐 community.render.com
1
0
December 22, 2022
CSS is not working correctly after applying the React code Splitting.
I have created my react app using the CRA and I recently added the react code splitting to it. After adding it, my CSS is not working properly. So if I have a search component that I have included ... More on github.com
🌐 github.com
1
May 23, 2024
Css don't apply to react components
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... More on github.com
🌐 github.com
14
September 16, 2016
🌐
Reddit
reddit.com › r/react › css style not applying
r/react on Reddit: CSS style not applying
March 4, 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!

🌐
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.
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.

🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
Solving the React Error: Not Picking Up CSS Style | Pluralsight
To make this code work, you just have to save the CSS file inside the src folder. But if you still want to separate the files, just make a new folder inside the src folder. Below is the correct code for importing the CSS file. import React, {Component} from 'react'; import ReactDOM from 'react-dom'; import '../src/CSS_Files/style.css';
🌐
Render
community.render.com › t › css-errors-when-deploying-react-app › 8053
CSS errors when deploying React app - Render
December 22, 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 ...
🌐
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 - I have created my react app using the CRA and I recently added the react code splitting to it. After adding it, my CSS is not working properly. So if I have a search component that I have included ...
Published   May 23, 2024
Author   lalit-tudip
Find elsewhere
🌐
GitHub
github.com › ReactJSResources › react-webpack-babel › issues › 241
Css don't apply to react components · Issue #241 · ReactJSResources/react-webpack-babel
September 16, 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
🌐
Stack Overflow
stackoverflow.com › questions › 71418190 › css-not-being-applied-to-component-in-react
CSS not being applied to component in React
I am not able to understand why ... Mar 10, 2022 at 2:31 · Ankit BiswasAnkit Biswas · 4144 bronze badges 2 · You have a typo in classname, it should be className with caps N · – Tushar Gupta Commented Mar 10, 2022 at ...
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

🌐
GitHub
github.com › orgs › community › discussions › 22495
CSS not being applied in pages :/ · community · Discussion #22495
June 10, 2020 - For React sites, edit package.json. Change the “homepage” setting to your custom domain. Ensure that this setting matches the exact domain specified in your repository’s CNAME file. For example, if your CNAME file contains mydomain.com, your package.json file should contain: ... If you are here looking for a solution to your CSS issue, I hope we’ve been able to help with the information above!
🌐
Stack Overflow
stackoverflow.com › questions › 77506008 › unable-to-import-css-file-in-react
reactjs - unable to import css file in react - Stack Overflow
The CSS file is not being referenced correctly in the component. In order for React to apply the styles from the CSS file to the component, you need to reference the styles in the component's className or style prop.
🌐
GitHub
github.com › JedWatson › react-select › issues › 602
Component CSS is Not loaded · Issue #602 · JedWatson/react-select
November 18, 2015 - Installed version v0.9.1, I can use the component, but seems no CSS is loaded.
🌐
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
January 1, 2019 - Hello, We are hitting an odd css/emotion issue in production, while the component works great in development. We've tried this on v2.0.0-v.2.2.0 and its the same issue with all of them. The dynamic css rules for the outer divs never get ...
Author   jjlauer