question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[SOLVED] Local webfonts not working

See original GitHub issue

Version

3.5.1

Environment info

  System:
    OS: Windows 10
    CPU: (8) x64 Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
  Binaries:
    Node: 10.15.3 - C:\Program Files\nodejs\node.EXE
    Yarn: Not Found
    npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: 44.17763.1.0
  npmPackages:
    @vue/babel-helper-vue-jsx-merge-props:  1.0.0-beta.2
    @vue/babel-plugin-transform-vue-jsx:  1.0.0-beta.2
    @vue/babel-preset-app:  3.5.1
    @vue/babel-preset-jsx:  1.0.0-beta.2
    @vue/babel-sugar-functional-vue:  1.0.0-beta.2
    @vue/babel-sugar-inject-h:  1.0.0-beta.2
    @vue/babel-sugar-v-model:  1.0.0-beta.2
    @vue/babel-sugar-v-on:  1.0.0-beta.2
    @vue/cli-overlay:  3.5.1
    @vue/cli-plugin-babel: ^3.5.0 => 3.5.1
    @vue/cli-plugin-eslint: ^3.5.0 => 3.5.1
    @vue/cli-service: ^3.5.0 => 3.5.1
    @vue/cli-shared-utils:  3.5.1
    @vue/component-compiler-utils:  2.6.0
    @vue/preload-webpack-plugin:  1.1.0
    @vue/web-component-wrapper:  1.2.0
    eslint-plugin-vue: ^5.0.0 => 5.2.2
    vue: ^2.6.6 => 2.6.9
    vue-eslint-parser:  2.0.3
    vue-hot-reload-api:  2.3.3
    vue-loader:  15.7.0
    vue-style-loader:  4.1.2
    vue-template-compiler: ^2.5.21 => 2.6.9
    vue-template-es2015-compiler:  1.9.1
  npmGlobalPackages:
    @vue/cli: Not Found

Steps to reproduce

Hello everyone,

I made a simple Vue Cli project using default configuration. In src/assets I created both a “css” and a “fonts” folders.

In the “fonts” folder I pasted those font files :

  • Roboto-regular.woff2
  • Roboto-regular.woff
  • Roboto-regular.ttf
  • Roboto-black.woff2
  • Roboto-black.woff
  • Roboto-black.ttf

These font files have been generated via https://www.fontsquirrel.com/tools/webfont-generator … But for an easier reproduction, I guess you can use any kind of font file you want. For my example I’ll keep it to Roboto regular and black.

In the “css” folder I created those files :

fonts.css contain this code :

$font_path: '~@/assets/fonts/';

@font-face {
  font-family: 'Roboto';
  src: local('Roboto'), local('Roboto-Regular'),
    url($font_path+'Roboto-regular.woff2') format('woff2'),
    url($font_path+'Roboto-regular.woff') format('woff'),
    url($font_path+'Roboto-regular.ttf') format('truetype');
  font-style: normal;
  font-weight: 400;
}
@font-face {
  font-family: "Roboto";
  src: local("Roboto Black"), local("Roboto-Black"),
    url($font_path+"Roboto-black.woff2") format("woff2"),
    url($font_path+"Roboto-black.woff") format("woff"),
    url($font_path+"Roboto-black.ttf") format("truetype");
  font-style: normal;
  font-weight: 900;
}

Then I edited the src/App.vue like this :

<template>
  <div id="app">
    <p>Hello,</p>
    <h1>world</h1>
  </div>
</template>

<script></script>

<style>
@import "./assets/css/minireset.min.css";
@import "./assets/css/fonts.css";

#app {
  font-family: "Roboto";
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #333333;
  margin-top: 120px;
}

#app h1 {
  font-weight: 900;
  margin-top: 30px;
}
</style>

Then I did a npm run serve to test it.

What is expected?

On localhost website : “Hello,” should use Roboto font regular. “World” should use Roboto font black.

What is actually happening?

No fonts seems to be deployed via Dev Tool > Sources, thus, the website shows no custom local webfonts…


My fonts are stored in “./src/assets/fonts/” I call the css inside the App.vue. The css “fonts.css” should call the fonts but it doesn’t.

When I build the project, the fonts aren’t copied in the dist folder.

I’m a neophyte trying to code my own portfolio with Vue Cli 3 and I can’t get past this issue.

I don’t understand where the problem is… Does anyone have an idea why the fonts doesn’t load when served or build ?

Thanks in advance !

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

26reactions
blueframescommented, Jan 4, 2020

In the vuejs project I am working on,

This one did not worked:

@font-face {
   font-family: 'name-of-choice';
   src: url("~@/assets/<location-to-your-font-file>/font-file-name.ttf") format('font-type');
}

This worked:

@font-face {
   font-family: 'name-of-choice';
   src: url(~@/assets/<location-to-your-font-file>/font-file-name.ttf) format('font-type');
}

And I’ve observed that if we use the solution without double quote for one single time and then add double quotes, it again works! If anyone know what’s happening here, please do answer.

Solution: Try not enclosing URL String in quotes.

4reactions
djcaesar9114commented, Jan 1, 2021

In the vuejs project I am working on,

This one did not worked:

@font-face {
   font-family: 'name-of-choice';
   src: url("~@/assets/<location-to-your-font-file>/font-file-name.ttf") format('font-type');
}

This worked:

@font-face {
   font-family: 'name-of-choice';
   src: url(~@/assets/<location-to-your-font-file>/font-file-name.ttf) format('font-type');
}

And I’ve observed that if we use the solution without double quote for one single time and then add double quotes, it again works! If anyone know what’s happening here, please do answer.

Solution: Try not enclosing URL String in quotes.

This also worked for me. Happy new year!

Read more comments on GitHub >

github_iconTop Results From Across the Web

css - @font-face not working - Stack Overflow
The solution is quite easy in my case: I deleted all the font files and put the original files in again, then everything...
Read more >
How to load web fonts to avoid performance issues and speed ...
Use the correct font format ; Preload fonts; Use the correct font-face declaration; Avoid invisible text during font loading. Let's break down ...
Read more >
Troubleshooting guide: Adding fonts to a website
Use this guide to troubleshoot issues with adding fonts to a website. ... Problem. Solution. Embed code is missing from site.
Read more >
Your page is not using font-display rule when loading the ...
Hi, I'm trying to optimize my wordpress web site and I installed your plugin. I run the test and I'm getting this message:...
Read more >
Web Fonts: How to Make Them Work Perfectly in Email - Litmus
Web fonts only work in some email clients, and care has to be taken to ... Make sure it's a public URL and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found