Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React : CSS class name import not working

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,
        }),
 },
like image 563
Shamseer Ahammed Avatar asked Dec 01 '18 15:12

Shamseer Ahammed


People also ask

Why cant I import CSS into React?

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.

How do I import a CSS class into React?

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.

Why className in React is not working?

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.

Where do I put CSS in React project?

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.

How to fix ClassName styles not working in react and JavaScript?

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.

How to import CSS file in react 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

Why is my CSS not applying to my React components?

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

How to wrap a class as an object in react?

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:


1 Answers

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>

like image 133
Chimera.Zen Avatar answered Oct 27 '22 20:10

Chimera.Zen