2

I can't get CSS modules to work in Js - the plain global CSS module file is working, and so are non-specific classes like body are working, however I can't get classes.className to work for example. I'd be glad of some help/ closure. Thanks,

Edit: I don't have an error upon compiling Edit 2: In Dev Tools no classes are shown unless I declare them statically like "className"

import React from "react";
import '.././Index.module.css';
import classes from "./CardLrg.module.css"


function CardLrg(props) {
    return (
        <div>
            <div className={classes.cardLrg}>
            <div className={classes.cardLrgHead}>
                <div className={classes.cardLrgDiscount}>{props.discount}</div>
                <img src="../../public/plus-square.svg" alt="Add to basket"/>
            </div>
            <div className={classes.cardLrgMainContent}>{props.sku}</div>
        </div>
        </div>
        
    )
}

export default CardLrg
.cardLrg {
    height: 160px;
    width: 160px;
    border-radius: 10px;
    border: 5px solid #000000;
    box-shadow: 10px 10px 0px #000000;
    position: relative;
    background-color: #f7f7f7;
}
7
  • 3
    What's the output of console.log(classes)? Commented Dec 21, 2022 at 14:37
  • @DaishoArch, outputting classes return an empty object Commented Dec 21, 2022 at 14:53
  • 1
    If it returns an empty object, eg classes.cardLrg will be undefined, makes sense why it's not working then? Commented Dec 21, 2022 at 15:49
  • What does your folder structure look like? Commented Dec 22, 2022 at 21:33
  • The syntax looks right.The path for the css module need to be in relation to the file using it. So if you have Parent -> css -> CardLrg.module.css & Parent -> component -> card.tsx (so css folder and component folder are siblings) It would need to be: import classes from "../css/CardLrg.module.css" Commented Dec 23, 2022 at 22:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.