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 OverflowWithout 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>
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
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!
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 :)
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.
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>
I had an issue with CSS not being applied, but it wasn't the modules, it ended up being the syntax of applying multiple classes. The syntax below solved the problem
<div className={[styles['col-sm-16'], styles['col-md-10'], styles['col-lg-8']].join(' ')}> some content </div>
Your webpack configuration may be changing the class name depending on its settings. (CSS Modules enabled, though your comment about stylesheet intact implies it's not).
Since you are importing the './style.css' file, try referencing the class name like: style.foo. Ex:
<div className={ style.foo }>foo div</div>
This article: http://javascriptplayground.com/blog/2016/07/css-modules-webpack-react/ describes the pattern of importing and referencing the class name pretty well.
If you're like me and use the style attribute for styling then afterwards move them into proper classes in .css files (and wonder why it doesn't work), remember to remove the JSX string format,
height: "100px"
is not the same as
height: 100px
I had this same problem and solved it by renaming my css file to:
myName.module.css
and also importing like this:
import styles from './myName.module.css'
There is no need to follow the steps of updating the webpack files any more.
in react 16.13 and [email protected] and higher you don't need to eject your project.
Your CSS files must be named camelCase + .modules.css and import to your projects like this:
import React, { Component } from 'react';
import styles from './app.module.css'; // Import css modules stylesheet as styles
class App extends Component {
render() {
return <button className={styles.test}>Error Button</button>;
}
}
export default App;
and in app.module.css
.test{
color: blue;
}
if you eject projects, in webpack.config.js file, in css modules change like this:
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
sourceMap: isEnvProduction && shouldUseSourceMap,
modules: true,