Next.js Discord

Discord Forum

Styles return undefined

Answered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
I am trying to scope some styles as they are only used in this component.

Here is what I am doing…

journal/[slug]/page.jsx
import styles from '../../../src/styles/components/journalSingle.scss';

return (
  <article class={`${styles.journalSingle} flow-l`}>
  </article>
)

src/styles/components/journalSingle.scss
.journalSingle {
  padding-block-start: 8.75rem;
  padding-block-end: 0rem;

  @media screen and (min-width: 768px) {
    padding-block-start: 14.875rem;
  }
}

Returned HTML
<article class="undefined flow-l"></article>

Not really sure what I am doing wrong?

16 Replies

https://nextjs.org/docs/app/building-your-application/styling/css-modules: only module.css can be imported directly to a style of a tag/component, normally when importing it just makes the values global for all in the page
Asian black bear
tl;dr rename journalSingle.scss to journalSingle.module.scss
also in React it is customary to use the prop className= instead of class=
I don't know if there are module scss tho...
But the className part I am amazed that it worked (but yeah should fix)
Asian black bear
@riský I am not sure about the builtin support, but I have personally used CSS modules with SCSS, so it definitely exists.
Asian black bear
You can use component-level Sass via CSS Modules and the .module.scss or .module.sass extension.
https://nextjs.org/docs/app/building-your-application/styling/sass
I didn't see it on the CSS module page, that's all, but nice to know 🙂
Asian black bear
I agree they really should mention it on the page about modules
Masai LionOP
Thanks for the responses. class not className is just a typo here but thanks for pointing out. So is there no way of scoping this little chunk of SCSS to the component?
Masai LionOP
Thanks again, the Next article doesn't explain what is actually happen here. Adding module to .scss just looks like a naming convention, but I suspect it does more. What does module actually do?
Answer
and it isn't just a naming convention because nextjs does things when it is there...
@Masai Lion just checking, do you understand how scss modules work/ is your original issue solved?