Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svelte scoped global styles do not work #14661

Open
Loizzus opened this issue Dec 10, 2024 · 2 comments
Open

Svelte scoped global styles do not work #14661

Loizzus opened this issue Dec 10, 2024 · 2 comments

Comments

@Loizzus
Copy link

Loizzus commented Dec 10, 2024

Describe the bug

As discussed in this closed feature request, the newly introduced scoped global styles do not work in most cases because of CSS specificity.

Because of CSS specificity if an element has more classes than another then it takes precedence when being rendered. So if in one of my +page.svelte files I want to edit an element to make it work perfectly for that page I will usually add a class to it (in most of my pages I have special cases that need custom styles).
image
However as you can see my custom styles are being ignored in favour of the global styles from my +layout.svelte file.

I did try using ID's instead of classes to select the element and that does work in some cases, but would not work if I wanted to select the same class name that is called in the +layout.svelte file (without creating a second selector for every element).
image

Also generally ID's are for javascript and classes for CSS, I have not used them in my design work for a long long time. It would also require an substantial amount of work to change all classes to ID's.

Reproduction

Download this repository: https://github.com/Loizzus/SvelteScopedGlobalStylesIssue
npm i
npm run dev

Logs

No response

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (12) x64 AMD Ryzen 5 3600XT 6-Core Processor
    Memory: 14.93 GB / 31.91 GB
  Binaries:
    Node: 20.11.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (130.0.2849.80)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    svelte: ^5.0.0 => 5.10.1

Severity

annoyance

@Rich-Harris
Copy link
Member

This is just how CSS works. A selector like div p will always override one that's just p

@Loizzus
Copy link
Author

Loizzus commented Dec 11, 2024

Hi @Rich-Harris, big fan 🥳

I agree that is how CSS works, orginally I opened a feature request for this, not an issue. The suggested solution is what has this issue.

How I think this feature could work would be if Svelte handled it in the background in a similar way to how it currently appends a .myclass.randomId to each class. If it could do that for all the CSS that it grabs from the +layout.svelte files above it then the experience would be seamless.

Since global is currently for the whole project perhaps a different name would work like:

:globalScoped{
    .main {
	    display: flex;
	    color: red;
    }
    p {
	    color: green;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants