How to load font from @font-face with Parcel?

妖精的绣舞 提交于 2021-01-27 12:20:51

问题


I'm using Parcel for bundling and I want to include custom font to my project

In my SCSS

@font-face {
    font-family: "Storytella";
    src: url("./fonts/Storytella.otf") format("otf");
}

Then I'm using it somewhere like this
font-family: 'Storytella', serif;

package.json

{
  "name": "pr",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "normalize.css": "^8.0.1",
    "parcel-bundler": "^1.12.3"
  },
  "devDependencies": {
    "node-sass": "^4.12.0",
    "parcel-plugin-static-files-copy": "^2.2.0",
    "pug": "^2.0.4"
  },
  "scripts": {
    "dev": "parcel index.pug",
    "build": "parcel build index.pug --out-dir dist"
  },
  "staticFiles": {
    "staticPath": "dist",
    "watcherGlob": "**"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

While running npm run dev the font is placed inside dist folder but the font itself doesn't get loaded to the page.

I get no errors in Networks in Chrome or anything like this but also no loading of the font.


回答1:


in index.html you need link fonts.css like this

<link rel="stylesheet" type="text/css" href="/styles/fonts.css" />

in fonts.css

@font-face {
  font-family: "Storytella";
  src: url("./fonts/Storytella.otf") format("otf");
}

body {
  font-family: 'Storytella', serif;
}


来源:https://stackoverflow.com/questions/57279163/how-to-load-font-from-font-face-with-parcel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!