I know it probably uses @font-face but I dont know where to put my woff files localy to get Svelte to use a custom font. I dont know where to put the @font-face either. Thanks in advance!
Svelte doesn't require anything special to use fonts.
You can inline a @font-face
in your stylesheet or inside any .svelte
file's <style>
tag:
<h1>Hello World!</h1>
<style>
@font-face {
font-family: 'Gelasio';
font-style: normal;
font-weight: 400;
src: local('Gelasio Regular'), local('Gelasio-Regular'), url(https://fonts.gstatic.com/s/gelasio/v1/cIf9MaFfvUQxTTqS9C6hYQ.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
h1 {
font-family: Gelasio
}
</style>
Alternatively, you can include the font with a <link>
inside the head. For this approach <svelte:head>
is handy:
<svelte:head>
<link href="https://fonts.googleapis.com/css?family=Gelasio" rel="stylesheet">
</svelte:head>
src: url('cIf9MaFfvUQxTTqS9C6hYQ.woff2')
with the font as a local file in the svelte directory, it wouldn't have worked. When Eddysanoli says "where to put my woff files locally" I assume this is what they mean. I should clarify I only meant your main solution doesn't work— the <svelte:head> alternative you wrote certainly does work. –
Degroot If you are using Sveltekit, you can load fonts locally using the static
directory.
Store your font files under static/fonts
, and then use either a CSS file or a <style>
tag to reference your font files.
/* fonts.css */
@font-face {
font-family: 'Lora';
font-style: normal;
font-weight: 500;
src: url('/fonts/lora-v20-latin-500.eot'); /* IE9 Compat Modes */
src: local(''), url('/fonts/lora-v20-latin-500.eot?#iefix') format('embedded-opentype'),
/* IE6-IE8 */ url('/fonts/lora-v20-latin-500.woff2') format('woff2'),
/* Super Modern Browsers */ url('/fonts/lora-v20-latin-500.woff') format('woff'),
/* Modern Browsers */ url('/fonts/lora-v20-latin-500.ttf') format('truetype'),
/* Safari, Android, iOS */ url('/fonts/lora-v20-latin-500.svg#Lora') format('svg'); /* Legacy iOS */
}
Finally, just import the CSS file in your __layout.svelte
file:
<!-- __layout.svelte -->
<script lang="ts">
import '/styles/fonts.css';
</script>
Svelte doesn't require anything special to use fonts.
You can inline a @font-face
in your stylesheet or inside any .svelte
file's <style>
tag:
<h1>Hello World!</h1>
<style>
@font-face {
font-family: 'Gelasio';
font-style: normal;
font-weight: 400;
src: local('Gelasio Regular'), local('Gelasio-Regular'), url(https://fonts.gstatic.com/s/gelasio/v1/cIf9MaFfvUQxTTqS9C6hYQ.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
h1 {
font-family: Gelasio
}
</style>
Alternatively, you can include the font with a <link>
inside the head. For this approach <svelte:head>
is handy:
<svelte:head>
<link href="https://fonts.googleapis.com/css?family=Gelasio" rel="stylesheet">
</svelte:head>
src: url('cIf9MaFfvUQxTTqS9C6hYQ.woff2')
with the font as a local file in the svelte directory, it wouldn't have worked. When Eddysanoli says "where to put my woff files locally" I assume this is what they mean. I should clarify I only meant your main solution doesn't work— the <svelte:head> alternative you wrote certainly does work. –
Degroot Not sure if you got it to work already but make sure your custom fonts are placed somewhere in the /public
folder and not in /src
.
So if you place them in /public/fonts
you can simply access them in your global.css
with:
@font-face{
font-family: 'yourFont';
src: url('/fonts/yourFont.woff') format('woff');
}
npm run dev
) –
Shun This worked for me:
<svelte:head>
<style>
@import url("https://fonts.googleapis.com/css?family=Raleway&display=swap");
</style>
</svelte:head>
I did it like this: I downloaded the font, put it in static folder. Then, in index.html, I added this style tag:
<style>
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: url('%sveltekit.assets%/open-sans-v27-latin-regular.woff2') format('woff2');
}
</style>
And in +layout.svelte, I added this:
<style global>
body {
font-family: 'Open Sans', sans-serif;
font-size: 14px;
}
</style>
Recently I had the problem that the fonts were loading to late and they caused a layout shift. Therefore, I added those 2 lines to the head section of index.html
:
<link rel="preload" as="font" href="%sveltekit.assets%/open-sans-v27-latin-regular.woff2" type="font/woff2" crossorigin="anonymous">
<link rel="preload" as="font" href="%sveltekit.assets%/open-sans-v27-latin-600.woff2" type="font/woff2" crossorigin="anonymous">
I suspect this has something to do with https://github.com/sveltejs/sapper/issues/904 - current workaround : use relative path to the font files.
If is anyone came to this reddit and the answers are not clear, i'll explain how i've solved:
@font-face {
font-family: 'Minecraft';
font-style: normal;
font-weight: normal;
src: url('$lib/fonts/minecrafttext.woff') format('woff2');
}
note that you should put your woff file into any folder on $lib/ and load it by calling the $lib folders on the url, sveltekit will handle it. I am using SSR
You can also set top level custom font and load another font on specific layout or page files.
© 2022 - 2024 — McMap. All rights reserved.