search

Home  >  Q&A  >  body text

Issue with styles not being applied when using .modules. In Next.js

<p>I launched a Next.js 13 application,<br /><br />I have a main.modules.css file</p><p><code>< ;/code></p> <pre class="brush:php;toolbar:false;">.heading { font-size: 4rem; color: #000; } .dark .heading { color: #fff; }</pre> <p>I use it to style components like this</p> <pre class="brush:php;toolbar:false;">import styles from "@/styles/main.module.scss"; export default function Home() { return ( <> <Header /> <h1 className={styles["heading"]}>Hi! I was styled by scss</h1> </> ); }</pre> <p>From. The styles for the heading class are applied correctly, but from. The attribute dark .heading does not have. <br /><br />My theme provider does add one to the HTML element. dark category. <br /><br />I used a normal one. scss file and apply a class like this</p><p><br /></p> <pre class="brush:php;toolbar:false;"><h1 className="heading">Hi! I was styled by scss</h1></pre> <p>Then it worked fine</p>
P粉504080992P粉504080992485 days ago498

reply all(2)I'll reply

  • P粉111227898

    P粉1112278982023-08-03 12:21:28

    You can try using styles. title. < h1 className = {styles.heading} >Hello! My style is scss</h1>

    reply
    0
  • P粉787806024

    P粉7878060242023-08-03 10:19:52

    You can use :global to declare that you are using a global class

    So main. modules. scss will become

    .heading {
        font-size: 4rem;
        color: #000;
    }
    
    :global(.dark) .heading {
        color: #fff;
    }
    
    

    Why is this happening?

    Why is this happening?
    The scope of CSS modules is the components they use.

    This means by default. The dark selector will be compiled and will not reference the global one. dark category.

    reply
    0
  • Cancelreply