Next.js Discord

Discord Forum

Change properties of a css module from a component in another css module

Answered
Common paper wasp posted this in #help-forum
Open in Discord
Common paper waspOP
Simple example:

This is my Home.tsx
export default function Home() {
   return (
    <Myh1 />
  );
}

This is Myh1.tsx
import styles from "./myh1.module.css";

export default function Myh1() {
    return <h1 className={styles.text}>Teste</h1>
}

This is myh1.module.css
.text {
    color: red;
}

I want to know how in my home.module.css I can access the .text class and change its color inside my home.module.css

I tried to do this in myhome.module.css but it didn't work
@import "../myh1/myh1.module.css";

.text {
    color: green;
}
Answered by Ray
try this
return (
  <Myh1 additiveClass={[styles.green, styles.blue]} />
);

.green {
  color: green;
}

.blue {
  color: blue;
}

import styles from "./myh1.module.css";

export default function Myh1({ additiveClass = [] }: { additiveClass: string[] }) {
  return <h1 className={[styles.text, ...additiveClass].join(" ")}>Teste</h1>
}
View full answer

5 Replies

Munchkin
You can import styles from your module in the Home() again and then write code to edit the css.

Otherwise what i think is better practice is to import in your layout.jsx/tsx

This way it gets imported into the whole project and thus becomes accessible without having to import more than once.
and for this you might have to do that in html as i am not sure that css files can talk to each other
@Ray something like this ts export default function Home() { return ( <Myh1 blueText /> ); } import styles from "./myh1.module.css"; export default function Myh1({blueText}) { return <h1 className={blueText ? styles.textBlue : styles.text}>Teste</h1> } .text { color: red; } .textBlue{ color: blue; }
Common paper waspOP
Your example gave me an alternative.
Would there be another way to do this, but more flexible to use in a media query without creating an additiveClass?
return (
    <Myh1 additiveClass={styles.green} />
  );

.green {
    color: green;
}

import styles from "./myh1.module.css";

export default function Myh1({ additiveClass }: { additiveClass?: string }) {
    return <h1 className={`${styles.text} ${additiveClass ?? ""}`}>Teste</h1>
}
@Common paper wasp Your example gave me an alternative. Would there be another way to do this, but more flexible to use in a media query without creating an additiveClass? jsx return ( <Myh1 additiveClass={styles.green} /> ); css .green { color: green; } jsx import styles from "./myh1.module.css"; export default function Myh1({ additiveClass }: { additiveClass?: string }) { return <h1 className={`${styles.text} ${additiveClass ?? ""}`}>Teste</h1> }
try this
return (
  <Myh1 additiveClass={[styles.green, styles.blue]} />
);

.green {
  color: green;
}

.blue {
  color: blue;
}

import styles from "./myh1.module.css";

export default function Myh1({ additiveClass = [] }: { additiveClass: string[] }) {
  return <h1 className={[styles.text, ...additiveClass].join(" ")}>Teste</h1>
}
Answer