Behavior when importing normal CSS files in components
Answered
Flemish Giant posted this in #help-forum
Flemish GiantOP
It looks like the document only covers the case where you import CSS files in your
pages/_app.ts or app/layout.tsx. Where can I find what exactly happens when you import CSS files in your components?Answered by joulev
check [this](https://nextjs.org/docs/app/building-your-application/styling/css-modules#global-styles) which has this sentence which is related to this question
Global styles can be imported into any layout, page, or component inside the app directory.
11 Replies
@Flemish Giant It looks like the document only covers the case where you import CSS files in your `pages/_app.ts` or `app/layout.tsx`. Where can I find what exactly happens when you import CSS files in your components?
the css becomes available to all pages using that component. so if you declare
note that the above applies to non-module css. [
also note that importing non-module css files to any arbitrary component only works if
* you use the app router, or
* you use the pages router and the css file is inside
.your-class you can use className="your-class" in those pagesnote that the above applies to non-module css. [
.module.css](https://nextjs.org/docs/app/building-your-application/styling/css-modules) files then check the docsalso note that importing non-module css files to any arbitrary component only works if
* you use the app router, or
* you use the pages router and the css file is inside
node_modules (so not a local css file, but a third party css file)@joulev the css becomes available to all pages using that component. so if you declare `.your-class` you can use `className="your-class"` in those pages
note that the above applies to non-module css. [`.module.css`](<https://nextjs.org/docs/app/building-your-application/styling/css-modules>) files then check the docs
also note that importing non-module css files to any arbitrary component only works if
* you use the app router, or
* you use the pages router *and* the css file is inside `node_modules` (so not a local css file, but a third party css file)
Flemish GiantOP
I use app router and yes, I meant non-module css.
Does CSS have effect until the page is refreshed once loaded by any component?
Does CSS have effect until the page is refreshed once loaded by any component?
And does it intelligently not load the CSS files multiple times?
@Flemish Giant I use app router and yes, I meant non-module css.
Does CSS have effect until the page is refreshed once loaded by any component?
i don't know fully how it works behind the scenes, but in my current understanding of it, it's equivalent to importing that css file to the root layout except the css only takes effect for pages using that component
it's available before the page is mounted just like global css
@Flemish Giant And does it intelligently not load the CSS files multiple times?
this one idk at all but likely yes. you can try it
Flemish GiantOP
okay thank you, and there’s no documentation for this I guess?
Probably I should create an issue/discussion on the repo to ask whether I can rely on its behavior
@Flemish Giant okay thank you, and there’s no documentation for this I guess?
check [this](https://nextjs.org/docs/app/building-your-application/styling/css-modules#global-styles) which has this sentence which is related to this question
Global styles can be imported into any layout, page, or component inside the app directory.
Answer
@Flemish Giant Probably I should create an issue/discussion on the repo to ask whether I can rely on its behavior
yes you can rely on it, the above sentence should affirm it
Flemish GiantOP
okay, thank you!