-
SummaryI am using tailwindcss version 4, is it possible to make next js generate css for each page? For example, I have a link / - homepage homepage contains many components, and therefore the result will be more css than contacts.... What is a possible implementation? What is the best solution? Thank You Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
|
This was a nice investigation. Starting with a fresh @import "tailwindcss" source(none);
:root {
--background: #ffffff;
--foreground: #171717;
}
@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
--font-sans: var(--font-geist-sans);
--font-mono: var(--font-geist-mono);
}
@media (prefers-color-scheme: dark) {
:root {
--background: #0a0a0a;
--foreground: #ededed;
}
}
body {
background: var(--background);
color: var(--foreground);
font-family: Arial, Helvetica, sans-serif;
}
@source "./layout.tsx";This way we scan only the layout, as far as I understand anyway Then I made @import "tailwindcss" source(none);
@source "./page.tsx";And @import "tailwindcss" source(none);
@source "./page.tsx";
@source "../../components/**/*.{tsx}";
.tomato {
@apply text-red-300;
}And then I was able to get some different stylesheets: Screen.Recording.2025-11-11.at.10.59.07.movI'd start by trying a whole bunch of things with different patterns and get comfortable with this. The default of one single stylesheet, while not optimal, should be just fine for most cases. After you feel like you are in full control of this pattern, then perhaps, you might find a page or two that would benefit from this. I am not sure this should be the best practice or default to go behavior. |
Beta Was this translation helpful? Give feedback.
-
|
Thank you, is it possible to load more than one css file? For example, I import a package button from another and I want to load the styles extra for that button.... |
Beta Was this translation helpful? Give feedback.
-
|
I don't really understand how it works. I have the following structure: layout.css button.css apps/test-app/src/app/page.tsx packages-ui/test-ui/src/component/button.tsx and result if call This is OK and next css is NOT OK. Contain classes page.tsx The CSS file should only contain color: I'm preparing a large project that will have many components and I would like to have it always separate so that there are no unnecessary styles... I would like to have it so that if a component from another package is loaded on the page, the css file for that component is loaded. Maybe there is a problem with how I'm using tailwindcss? Or does this have to be solved differently? But I would like to use tailwindcss, but it must be able to solve it with separate CSS per component. |
Beta Was this translation helpful? Give feedback.
-
|
You're right.... so if I wanted to have specific styles, I could prefix the class in the component. And it could work like this: I would compile the style in the component package, to And where I have, for example, a button - I would import I tried it and it behaves exactly as I would expect - in the global style I have everything related to the application, including the tailwindcss class, and where the button is loaded, the extra style is loaded from that package, where I only have specific styles for the button. |
Beta Was this translation helpful? Give feedback.
You're right.... so if I wanted to have specific styles, I could prefix the class in the component. And it could work like this:
I would compile the style in the component package, to
/dist/styles.cssAnd where I have, for example, a button - I would import
import '../../dist/styles.css'I tried it and it behaves exactly as I would expect - in the global style I have everything related to the application, including the tailwindcss class, and where the button is loaded, the extra style is loaded from that package, where I only have specific styles for the button.