Scope styles to a page
Heyo! I want to be able to scope some CSS to only apply when that page component is mounted. This CSS targets the
html
and body
tags and is not for the component itself.
Currently I am doing import "./layout.css";
, which correctly applies the styles when on this page, but then the styles remain in effect when I navigate to other pages.
I could use :has()
but support isn't all the way there yet. I could utilize mount and cleanup, but not sure how I could "run" and dispose of the CSS because that's not how CSS works.7 Replies
I may just have to use JS to set these styles in the page lifetime functions.
You cannot do remove css without doing a browser refresh, so instead you should just remove the classes on navigation to other pages
This works easily enough! A little hacky for now until a better structure is in place.
the parkour solution is to use solid-styled
Hey, I like this! My use case is too narrow to pull in an extra dependency, but I'll keep this in mind for the future. Feels a lot more expressive, like Svelte and Vue style blocks.
yeah which is why I called it a "parkour" solution