Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SvelteKit: use local assets inside of global css file?

If I have a global CSS file which defines some basic colors and fonts and all that, and include it in my top-level layout file with a simple import "$lib/assets/app.css", then how can I reference local fonts and images from within that css file, which are also in the /lib/assets folder? Currently it seems I have to put those files into /static, but that results in warnings when creating a build, and more importantly, they get cached in the browser for only 4 hours.

As an example, I have something like this in my $lib/assets/app.css file:

@font-face {
  font-family: "Custom";
  src: url("/fonts/Bitter-Regular.ttf");
  font-display: swap;
}

That works fine when that font is placed in the /static folder but I’d rather have it placed in $lib/assets because presumably that would mean it becomes part of the immutable build and gets cached way way longer.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can just use $lib in your CSS:

@font-face {
  font-family: "Custom";
  src: url("$lib/assets/fonts/Bitter-Regular.ttf");
  font-display: swap;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading