Documentation Shento Hendriks
Svelte / SvelteKit
Svelte / SvelteKit
  • Getting Started with SvelteKit
    • Installing SvelteKit
      • Tailwind Typography plugin
    • Base Syntax & Core Features of Svelte
      • Curly Braces & Understanding Core Syntax
      • Reactive Variables
      • Two-way-binding Shortcut
      • Using multiple components
      • Components & Communication via Props
      • Display HTML in Svelte
      • Dynamic classes
      • Using $effect
      • Working with Javascript and Rune reactivity
    • Working with Conditionals & Loops
      • Showing code conditionally in Svelte
      • Looping through arrays (lists)
      • Lists & Keys
    • Closer look at reactivity
      • Updating Arrays & Objects
      • Event Modifiers
    • Component Events
    • Best Practices
    • Adding Google Font in Tailwind and SvelteKit
    • Adding Local Fonts to SvelteKit with Tailwind
    • Prevent flicker in images
    • Enhanced Images
    • Form Submissions in SvelteKit
    • Effective SEO in SvelteKit
    • Automatic Site Maps in Svelte
    • Creating Custom Elements with Svelte, SvelteKit, and Vite
    • Creating a Svelte Library with JavaScript Functions
  • Useful Code Snippets
    • Hover effects
    • Navigation
    • Centering Items
    • Setting Footer Bottom Page
  • Installing Useful Libraries
    • Documenting Javascript with JSDocs (Crash Course)
    • Adding Lottie Animations to Svelte Application
    • SvelteKit Melt-ui
      • First Component - accordion
    • Installing Shadcn/ui Svelte
    • Getting Started with GSAP with SvelteKit
      • What is GSAP?
      • Installing GSAP & Your first animation
      • GSAP in SvelteKit
    • Installing Locomotive Scroll with Svelte 5 and SvelteKit
  • SvelteKit Server Fetching and Deployment
    • Preparing a SvelteKit application for Deployment
    • Utility Functions for URL Handling in SvelteKit
    • Fetching Directus Data from Rest API with SvelteKit
    • Fetching data from api with caching
    • Displaying Fetched Data in a Each Loop
    • Creating slug friendly urls based on query data
    • Finding correct query in api based on url slug
Powered by GitBook
On this page
  • 1. Download and Place the Font Files
  • 2. Define the Font in CSS
  • 3. Import the CSS in Svelte
  • 4. Configure TailwindCSS
  • 5. Use the Custom Font in Your Components
  1. Getting Started with SvelteKit

Adding Local Fonts to SvelteKit with Tailwind

To add WOFF2 fonts to a SvelteKit project using TailwindCSS, you can follow these steps:

1. Download and Place the Font Files

First, download your WOFF2 font files and place them in a directory within your project. A common practice is to place them in the lib or src directory.

For this example, let's place them in the src/lib/fonts directory:

src/lib/fonts/YourFont.woff2

2. Define the Font in CSS

Create a CSS file or use an existing one to define the @font-face rule. This will tell the browser where to find the font files and how to use them.

For example, create a file named global.css in the src directory and add the following code:

@layer base {
  @font-face {
    font-family: 'YourFont';
    font-style: normal;
    font-weight: 400;
    src: url('$lib/fonts/YourFont.woff2') format('woff2');
  }
}

3. Import the CSS in Svelte

Ensure that your CSS file is imported in your Svelte components. Typically, you would import it in the +layout.svelte file to make the font available globally:

<script>
  import '../global.css';
</script>
<slot />

4. Configure TailwindCSS

Update your tailwind.config.js file to include your custom font. You can either replace the default font family or extend the existing configuration.

Option 1: Replace the Default Font

// tailwind.config.js
/** @type {import('tailwindcss').Config} */
export default {
  content: ['./src/**/*.{html,js,svelte,ts}'],
  theme: {
    fontFamily: {
      sans: ["YourFont", 'sans-serif'],
    },
    extend: {},
  },
  plugins: [],
};

Option 2: Add as an Extended Font

// tailwind.config.js
/** @type {import('tailwindcss').Config} */
export default {
  content: ['./src/**/*.{html,js,svelte,ts}'],
  theme: {
    extend: {
      fontFamily: {
        custom: ["YourFont", 'sans-serif'],
      },
    },
  },
  plugins: [],
};

5. Use the Custom Font in Your Components

Now, you can use the custom font in your Svelte components by applying the corresponding TailwindCSS class.

For example, in a Svelte component:

<div class="font-custom">
  <h1 class="text-5xl">Hello, World!</h1>
</div>

Or, if you replaced the default font:

<div class="font-sans">
  <h1 class="text-5xl">Hello, World!</h1>
</div>
PreviousAdding Google Font in Tailwind and SvelteKitNextPrevent flicker in images

Last updated 9 months ago