20

I have a project with Laravel and Vue.js and I use scss. I want to add new font into my project and I can't do this. I create file fonts.scss, create a variable with font

@font-face {
  font-family: "Proxima Nova Bold";
  src: url(/Proxima-Nova-Bold.otf);
}

$proxima-nova-bold: 'Proxima Nova Bold', sans-serif;

And I have console mistake GET http://local.{my-domain}/Proxima-Nova-Bold.otf net::ERR_ABORTED

Here is how I import scss file in Vue component

@import '../../../fonts/fonts.scss';

Here is my laravel mix

let mix = require('laravel-mix');

mix.copy('resources/assets/images', 'public/images', false)
    .js('resources/assets/js/app.js', 'public/js')
    .sass('resources/assets/sass/app.scss', 'public/css')

Where did I make mistake? Or tell me how to add fonts correctly?

5
  • 1
    You told it how to load the font face, and you gave it a path. If that file doesn't exist at public/Proxima-Nova-Bold.otf, the client can't request it. Make sure it does. Commented Jul 25, 2018 at 16:54
  • Yes, you are right. But how can I work only in resources, not public? maybe should I write some rule for webpack? Commented Jul 25, 2018 at 17:27
  • Yep. You should use the file-loader plugin for this. Commented Jul 25, 2018 at 17:29
  • What is your exact build setup for your frontend? laravel mix ? vue-cli ? plain webpack ? For a first guess I don't see any reason of using an absolute path in the url there. Use the relative path and if webpack is setup correctly it will handle resolving and bundling the font correctly. Commented Jul 25, 2018 at 18:44
  • @FrankProvost I use laravel mix. Add code snippet to the question Commented Jul 25, 2018 at 19:50

3 Answers 3

18

Works for vue3

  1. Download the font and place it with other static assets.
  2. In app.vue register the font family with @font-face like below:
<style>
@font-face {
    font-family: <name_u_choose>;
    src: url('~@/assets/fonts/<static_font_file>.ttf');
}
  1. Use it in your components like below
<style>
<you-selector> {
    font-family: <name_u_chose_before>;
}
</style>
Sign up to request clarification or add additional context in comments.

1 Comment

it worked for me without ~ at the start of url: src: url('@/assets/fonts/<static_font_file>.ttf');
6

Based on this issue in the laravel mix github https://github.com/JeffreyWay/laravel-mix/issues/1172 laravel mix will copy the fonts automatically when referencing them correctly.

Therefore i suggest to use a relative path when referencing the font. This should actually trigger laravel mix to create a folder "fonts" in your "public" folder

@font-face {
  font-family: "Proxima Nova Bold";
  src: url('./../fonts/Proxima-Nova-Bold.otf');
}

1 Comment

this cause an error while building, the only way to run yarn dev is only this way @font-face { font-family: "Proxima Nova Bold"; src: url(/Proxima-Nova-Bold.otf); }
0

You may need to add a line like this to your mix file:

.copy('resources/fonts', 'public/fonts')

2 Comments

This should not be required when using laravel-mix
This method imports only scss file, but not the fonts. How could I import fonts files?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.