i have a <main>
div in my react component and im importing some class name from a class css file, but the class name is not getting integrated to the main div when i try to inspect it in the browser. when i simply use other class names its working like <main className="test">
but importing classes is not working.
This is my component :
import React from 'react';
import Aux from '../../hoc/Aux';
import classes from './Layout.css';
const layout = (props) => (
<Aux>
<div>
Test paragraph
</div>
<main className={classes.Content}>
{props.children}
</main>
</Aux>
);
export default layout;
This is my css
.Content{
color: red;
margin-top: 20px;
}
I did npm run eject
command after creation, if there is something to do with the webpack configuration file please help me
( i haven't made any changes there after eject command )
Here is the css part of webpack dev config file
{
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
}),
},
// Adds support for CSS Modules (https://github.com/css-modules/css-modules)
// using the extension .module.css
{
test: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
modules: true,
getLocalIdent: getCSSModuleLocalIdent,
}),
},
This error is generated because the compiler is only able to import files from the src folder. Here, the CSS file is saved outside the src folder, so the compiler failed to import it. To make this code work, you just have to save the CSS file inside the src folder.
CSS in Create React App js file where you import the component and render it to the DOM. You need to import the CSS file here also: import React from "react"; import ReactDOM from "react-dom"; import "./styles. css"; import App from "./App"; const rootElement = document.
Try to rename the .scss filename extension to .module.scssmodule. scss extension. I had the same problem and it fixed my issue. You can also check here the question I've asked of it.
External stylesheet You can create a new CSS file in your project directory and add your CSS inside it. You can then import the file in your component, class, or React JS page.
To fix className styles not working in React and JavaScript, we can import the styles file as a module. to add some classes with styles into a styles file. to import the styles module as a default export. Next, we reference the app class in styles.module.scss with styles.app.
Solution is just to name your CSS files as (in your case) Layout.module.cssand then import them as such. You don't need to eject as from the Create React App 2.0since it uses CSS Modules out of the box. Share Improve this answer
9 Why is my CSS not applying to my React components? 1 Unable to import (require) css, less, sass file on react component when using express server
import './Layout.css'; Then use it like a normal CSS <main className="Content"> You can also use your classes as objects with this format: In your CSS file: Wrap your classes and id's with :local(.className) Example :local(.Content) { width: 100px; } In your React Component:
import './Layout.css';
Then use it like a normal CSS
<main className="Content">
You can also use your classes as objects with this format:
In your CSS file:
Wrap your classes and id's with :local(.className)
Example:local(.Content) { width: 100px; }
In your React Component:import classes from './stylesheet.css'
<div className={classes.Content}></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With