亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Next.js + Tailwind 中的自定義字體:沒有錯誤,但字體錯誤

Next.js + Tailwind 中的自定義字體:沒有錯誤,但字體錯誤

鳳凰求蠱 2022-10-13 14:34:52
在我的 Next.js (9.4.4) / Tailwind.css (1.4.6) 項目中,我使用了一種名為 SpaceGrotesk 的自定義字體。為了使它工作,我把我的字體 my public/fonts/spaceGrotesk,然后,我配置我的文件如下:// next.config.jsmodule.exports = {    cssModules: true,    webpack: (config, options) => {        config.node = {            fs: "empty",        };        config.module.rules.push({            test: /\.(png|woff|woff2|eot|ttf|svg)$/,            use: [                options.defaultLoaders.babel,                {                    loader: "url-loader?limit=100000",                },                {                    loader: "file-loader",                },            ],        });        return config;    },};/** tailwind.css */@tailwind base;@tailwind components;@tailwind utilities;@font-face {    font-family: SpaceGrotesk;    font-weight: 400;    font-display: auto;    src: url(../public/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff) format("woff");}// tailwind.config.jsmodule.exports = {    purge: {        mode: "all",        content: [            "./components/**/*.js",            "./Layout/**/*.js",            "./pages/**/*.js"        ],    },    important: true,    theme: {        extend: {            fontFamily: {                paragraph: ["Crimson Text, serif"],                spaceGrotesk: ["SpaceGrotesk, sans-serif"],            },        },    },};我曾經在控制臺上顯示導入錯誤時遇到很多麻煩,但都修復了它們。但是,現在我仍然沒有得到正確的字體。控制臺沒有顯示警告,檢查器似乎說字體加載正確,但仍然使用備用字體(無襯線)而不是 SpaceGrotesk。導入字體我做錯了什么?
查看完整描述

4 回答

?
呼如林

TA貢獻1798條經驗 獲得超3個贊

為了將您自己的字體集成到您的 Next 項目中,您不需要 npm 模塊形式的另一個依賴項。


要獲取 globals.css 中的字體,您必須將字體放入公用文件夾中。然后將字體集成到 globals.css 中,以將其提供給 tailwind.config.js 中的 CSS 框架。之后,您只需將其添加到相應的元素中,或者全局定義它。


全局的.css


@tailwind base;

@tailwind components;

@tailwind utilities;


@font-face {

  font-family: "Custom";

  src: url("/CustomFont.woff2");

}


body {

  @apply font-custom; //if u want the font globally applied

}

tailwind.config.js


module.exports = {

  purge: ["./pages/**/*.{js,ts,jsx,tsx}", "./components/**/*.{js,ts,jsx,tsx}"],

  darkMode: false,

  theme: {

    extend: {

      fontFamily: {

        custom: ["Custom", "sans-serif"]

      }

    }

  }

}


查看完整回答
反對 回復 2022-10-13
?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

由于next-fonts軟件包,我終于解決了這個問題:


步驟1:

安裝next-fonts:


npm install --save next-fonts

第2步:

將其導入next.config.js:


// next.config.js


const withFonts = require("next-fonts");


module.exports = withFonts({

    webpack(config, options) {

        config.node = {

            fs: "empty",

        };

        config.module.rules.push({

            test: /\.(png|woff|woff2|eot|ttf|svg)$/,

            use: [

                options.defaultLoaders.babel,

                {

                    loader: "url-loader?limit=100000",

                },

                {

                    loader: "file-loader",

                },

            ],

        });

        return config;

    },

});


第 3 步:

將您的文件放在文件public夾中的任何位置。例如,我將 SpaceGrotesk 字體放入public/fonts/spaceGrotesk.


第4步:

使用@font-face 在你的 CSS 中導入它們:


// tailwind.css


@font-face {

    font-family: "SpaceGrotesk";

    font-weight: 400;

    src: url(/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff) format("woff");

}

注意URL/public前面沒有。這就是我遇到問題的原因。


第 5 步:

只需像使用其他字體一樣使用您的字體:


// tailwind.css


a {

    font-family: "SpaceGrotesk";

}

第 6 步(可選):

您可以將字體添加到配置中,以便使用該類font-space-grotesk:


// tailwind.config.js


module.exports = {

    // ...The rest of your config here...

    theme: {

        extend: {

            fontFamily: {

                "space-grotesk": ["SpaceGrotesk, sans-serif"],

            },

        },

    },

};


查看完整回答
反對 回復 2022-10-13
?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

在 Next.JS 10 及以上版本中,不確定以前的版本,不需要替換../public/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff 為/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff

從公用文件夾提供靜態資產時,您無需指定公用文件夾,只需指定鏈接,就好像公用文件夾是根文件夾一樣。靜態文件服務


查看完整回答
反對 回復 2022-10-13
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

沒有“font-woff”格式。

代替

src: url(../public/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff) format("font-woff");

src: url(../public/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff) format("woff");



查看完整回答
反對 回復 2022-10-13
  • 4 回答
  • 0 關注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號