【问题标题】:How to load font from @font-face with Parcel?如何使用 Parcel 从@font-face 加载字体?
【发布时间】:2019-07-30 19:57:16
【问题描述】:

我正在使用 Parcel 进行捆绑,我想在我的项目中包含自定义字体

在我的 SCSS 中

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

然后我在这样的地方使用它
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"
}

在运行npm run dev 时,字体被放置在dist 文件夹中,但字体本身并没有加载到页面中。

我在 Chrome 中的网络或类似的东西中没有错误,但也没有加载字体。

【问题讨论】:

    标签: css sass fonts font-face parceljs


    【解决方案1】:

    在 index.html 中你需要像这样链接 fonts.css

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

    在 fonts.css 中

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

    【讨论】:

      猜你喜欢
      • 2010-11-22
      • 2013-01-15
      • 2018-01-11
      • 2016-12-24
      • 2016-10-21
      • 1970-01-01
      • 2012-04-20
      • 2019-06-02
      • 1970-01-01
      相关资源
      最近更新 更多