Skip to content

Local styles aren't scoped when nested #14616

@KTibow

Description

@KTibow

Describe the bug

Say I have an element in a component that needs to depend on its parent's state. I might write something like this:

<div class="tint"></div>

<style>
  :global(*):hover > .tint {
    opacity: 0.08;
  }
</style>

Now let's say I want to also account for focus. To prevent repetition, I might nest my styles:

<div class="tint"></div>

<style>
  :global(*) {
    &:hover > .tint {
      opacity: 0.08;
    }
    &:focus-visible > .tint {
      opacity: 0.12;
    }
  }
</style>

However, Svelte outputs CSS that works differently across these cases. It works correctly for the first one by applying special scoping classes to .tint, but it doesn't with the second one. (See the CSS output tab of the REPL.)

This means that any .tint that has a hovered parent will get the styling that was only supposed to apply to one component. That's a problem.

Reproduction

https://svelte.dev/playground/fe63a82ba4da46799977fcbca797adfc?version=5.9.0

Logs

No response

System Info

N/A (using the REPL in Firefox 133.0 (64-bit) on Fedora Workstation 40)

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcssStuff related to Svelte's built-in CSS handling

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions