最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to Add a google font to Next.Js Project locally while using tailwind css - Stack Overflow

matteradmin9PV0评论

for a website that I want to create, I want to use the "Work Sans" Font from google. I downloaded "WorkSans-Black.ttf" file, created in the Folder "public" a subfolder "fonts" and threw the file in there. Following I have screenshoted the folder structure for you guys. Can anybody please help me, what are the next steps ?

for a website that I want to create, I want to use the "Work Sans" Font from google. I downloaded "WorkSans-Black.ttf" file, created in the Folder "public" a subfolder "fonts" and threw the file in there. Following I have screenshoted the folder structure for you guys. Can anybody please help me, what are the next steps ?

Share Improve this question edited Apr 13, 2021 at 16:59 Khashayar asked Apr 13, 2021 at 16:18 KhashayarKhashayar 472 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

1. Download Font

Download the font and place it in the public/font directory. I would suggest using this tool to download Google Fonts: https://google-webfonts-helper.herokuapp./fonts/work-sans?subsets=latin

2. Extend Tailwind's font stack with your font

// tailwind.config.js

const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
  theme: {
    extend: {
      fontFamily: {
        sans: ['Work Sans', ...defaultTheme.fontFamily.sans],
      },
    },
  },
  // ...
}
// tailwind.css

@tailwind base;
@tailwind ponents;

// Repeat this for all the weights you downladed
@font-face {
  font-family: 'Work Sans';
  font-style: normal;
  font-weight: 400;
  src: local(''),
       url('public/fonts/work-sans-v9-latin-regular.woff2') format('woff2'),
       url('public/fonts/work-sans-v9-latin-regular.woff') format('woff');

@font-face {
  font-family: 'Work Sans';
  font-style: normal;
  font-weight: 100;
  src: local(''),
       url('public/fonts/work-sans-v9-latin-100.woff2') format('woff2'),
       url('public/fonts/work-sans-v9-latin-100.woff') format('woff');

@tailwind utilities;

3. (Optional) Preload font for performance

// Ref.: https://kirazhang./posts/nextjs-custom-fonts

import Head from "next/head";
import Link from "next/link";

export default function Layout {
    return (
    <div>
      <Head>
          <link
            rel="preload"
            href="public/fonts/work-sans-v9-latin-regular.woff2"
            as="font"
            crossOrigin=""
          />
          <link
            rel="preload"
            href="public/fonts/work-sans-v9-latin-100.woff2"
            as="font"
            crossOrigin=""
          />
        </Head>
            <body><Main /></body>
        </div>
    )
}
Post a comment

comment list (0)

  1. No comments so far