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"]
}
}
}
}

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"],
},
},
},
};

TA貢獻1993條經驗 獲得超6個贊
在 Next.JS 10 及以上版本中,不確定以前的版本,不需要替換../public/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff
為/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff
從公用文件夾提供靜態資產時,您無需指定公用文件夾,只需指定鏈接,就好像公用文件夾是根文件夾一樣。靜態文件服務

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");
添加回答
舉報