Applying plain CSS to one single page
Answered
block posted this in #help-forum
blockOP
Hello, I have an issue with CSS in my application. I have multiple page with a different styling, and I have issue where the css of one page stay when going to an other page. How could I have my css apply only to one single page? For example, I have a blank page with a button to the main page. When I use it then on the main page, click a button to go to this page, it suddenly have the styling of the main page. When I reload it becomes blank again. Any advice ?
Answered by B33fb0n3
you are right it would be a bit to complex to rewrite everything. You can see inside the docs, that css modules are made for locally scopes. Global CSS (which I assume global selectors as well) are handled in global css files (like
.css
files).and difficulty managing styles as the application grows.^ what you are experiencing now...
96 Replies
blockOP
@block Hello, I have an issue with CSS in my application. I have multiple page with a different styling, and I have issue where the css of one page stay when going to an other page. How could I have my css apply only to one single page? For example, I have a blank page with a button to the main page. When I use it then on the main page, click a button to go to this page, it suddenly have the styling of the main page. When I reload it becomes blank again. Any advice ?
you can do this when using css modules. For example like
And that can be imported into one single page:
styles.module.css
Inside it you can use normal css:.blog {
padding: 24px;
}
And that can be imported into one single page:
import styles from './styles.module.css'
blockOP
I tried this so many times, but it's a weird css with like *{
stuff
}
stuff
}
are you already using css modules or just basic css file like
styles.css
?blockOP
I actually tried 10 times using this, with adding className={styles.home} then adding.home everywhere
A weird one
I tried like css to tailwind converter, but my issue is that the css file is very big
It'd take sooo much time so I was looking for a solution that didnt take 5 hours
what's the name of your current css file?
(the full name)
blockOP
page.css and i tried with page.module.css after kinda rewriting to modular but it wasnt the same output
rewriting to modularcan you clarify this? It should be the same syntax as css is css
this for a WHOLE enormous css file
is a pain to "rewrite" to modular
Like u can't really img, body in modular, u need class if you want not to apply it to the whole app
I tried AI to convert it and stuff, it was just too big for it
@B33fb0n3 works fine for me 🤔
You can just copy paste it
blockOP
I mean, if you use body and stuff like a regular css
You still have the same bug
applying it to all of the files
Let me try tho
css modules should be scoped to a page. Normal css files (not modules) should stay when navigating. Css modules shouldn't
blockOP
it's a weird css
blockOP
not really, but the * isnt the problem, i'll remove it for the example
uhh actually
yea
Yea it's not working for me
But when I got the modular css to work, it was still applying on the whole page
@block But when I got the modular css to work, it was still applying on the whole page
it was still applying on the whole pagehow do you know?
blockOP
Because when going to the blank page with nothing applied to it, it had styling
i have a video
@block https://cdn.discordapp.com/attachments/1317942584261218375/1320855451243642891/wgZMnJVENu.mp4?ex=676b1dff&is=6769cc7f&hm=cb36100f638aa9927a232b4af915f5975c24b95f113804641891d705f2057df2&
thanks for showcasing this. Can you share a repro? For example via github or via [codesandbox](https://codesandbox.io/). I wasn't able to reproduce it with css modules
blockOP
hmm
i dont think the guy would really agree but i have some snippet
its a simple page without styling
in the directory app/stats/page.js
Please create a minimal reproduction that shows the specific issue for example via a github repo or codesandbox
blockOP
@B33fb0n3 I'm finish, recording it rn
@block <@301376057326567425> I'm finish, recording it rn
Please create a minimal reproduction that shows the specific issue for example via a github repo or codesandbox. A video won't be helpful
blockOP
Even with the code given?
It's VERY minimal code
yes
blockOP
yes usefull or yes with code is enough
only minimal reproduction. I don't want to look though your whole codebase
blockOP
Its 10 lines
yea, this is fine if it shows the issue
blockOP
import React from "react";
import Link from "next/link";
import './page.css'
const Testing1Page = () => {
return (
<div>
<h1>Testing1 Page (shoud have styling)</h1>
<Link href="./testing2">
<h1>Click me!</h1>
</Link>
</div>
);
};
export default Testing1Page;
=> /testing1import React from 'react';
import Link from "next/link";
const Testing2Page = () => {
return (
<div>
<h1>Testing2 Page (shoud not have styling)</h1>
<Link href='./testing1'><h1>Click me!</h1></Link>
</div>
);
};
export default Testing2Page;
=> /testing2
Please create a minimal reproduction that shows the specific issue for example via a github repo or codesandbox. A video or just code won't be helpful
I won't be able to help you while seeing these messages inside this thread. I can and will only help you when you shared a reproduction.
blockOP
wdym
It's 4 lines, it's way faster than sending a whole nextjs project
It's all the file done
Now I explain
and give the video of waht it does
------
Basically, this is creating to page
Testing 1 has a style (green text) while testing 2 doesn't
When accessing to both page individually, everything does work properly
HOWEVER When using the button to go from /testing1 (style) to /testing2 (no style) the testing2 page seem to have the style it shouldn't have!
Here is a 5 seconds video
Basically, this is creating to page
/testing1 and /testing2
with both a button using next/link
to go to the other page (from testing 1 to testing 2 and the opposite.Testing 1 has a style (green text) while testing 2 doesn't
When accessing to both page individually, everything does work properly
HOWEVER When using the button to go from /testing1 (style) to /testing2 (no style) the testing2 page seem to have the style it shouldn't have!
Here is a 5 seconds video
code side to side
done @B33fb0n3 in a condensed message
I won't read all that even if I would be able to provide a good solution.
Do this and I will look further into it: https://nextjs-forum.com/post/1320858815452942346#message-1320867641560272917 <--- click
Do this and I will look further into it: https://nextjs-forum.com/post/1320858815452942346#message-1320867641560272917 <--- click
blockOP
wth
fine but wth
I added /testing1 and /testing2
what do I need to do to reproduce the error?
blockOP
Go to /testing1 and /testing2 individually
One is styled one not
however, if you use the button to navigate, it will get "stuck" on the styling
Thanks for sharing a reproduction. I tried a little bit myself and as said: using css modules solves this error. I forked your repro to showcase you this. You can see it here: https://codesandbox.io/p/devbox/showcase-forked-rqdzkw?workspaceId=ws_U9MTFNwo3D8jjR8Ddi1Ynh
blockOP
Yes, I know this work, but my css is so big I'm looking for an alternative to using the class={styles.heading} (As shown, the css has a LOT of body, * and other weird stuff). I was looking for a way not to "give" the css to the testing 2
Or would you know any online convertor?
*{
padding: 0;
margin: 0;
outline: none;
border: none;
box-sizing: border-box;
}
Also I haven't found how to convert this to modular
padding: 0;
margin: 0;
outline: none;
border: none;
box-sizing: border-box;
}
Also I haven't found how to convert this to modular
you are right it would be a bit to complex to rewrite everything. You can see inside the docs, that css modules are made for locally scopes. Global CSS (which I assume global selectors as well) are handled in global css files (like
.css
files).and difficulty managing styles as the application grows.^ what you are experiencing now...
Answer
blockOP
Okay thank you, i'll see if I can get chatgpt to do the conversion for me :)
blockOP
If anyone has this issue IF FOUND THE SOLUTION GUYS!!!!!!!!!!!!!!
Put everything into the public folder