Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I keep "unused css selector"?

Tags:

svelte

I noticed that svelte will purge my css automatically, all "unused css selector" will get removed.

For example:

<p class="blue">This is a paragraph.</p>

<style>
    .red{
        color: red;
    }
    .blue{
        color: blue;
    }
</style>

The style for class 'red' will be removed. How can I keep the '.red' selector? I want to use it later at some point.

like image 880
yaquawa Avatar asked Nov 17 '25 02:11

yaquawa


2 Answers

You probably want to wrap the selector in :global(...), like

:global(.red) {
    color: red;
}

This forces Svelte to keep the class around, and also makes it so the selector isn't scoped to that single component. This is usually what you want when Svelte is removing selectors that you want to keep.

like image 150
Smitop Avatar answered Nov 18 '25 20:11

Smitop


You can also use global in the style tag, if you have svelte-preprocess installed (link):

<style global>
    .red{
        color: red;
    }
    .blue{
        color: blue;
    }
</style>
like image 28
tbdrz Avatar answered Nov 18 '25 19:11

tbdrz