Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add font in Vue.js project

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?

like image 423
Yaroslav Saenko Avatar asked Jul 25 '18 16:07

Yaroslav Saenko


People also ask

How do I add Google fonts to Vue JS?

To add a Google Font to a Vue. js component, we can imnport it in our CSS. @import url("https://fonts.googleapis.com/css?family=Roboto+Condensed"); Then we can set the font-family to Roboto in the elements we want.

How do I import Font Awesome into Vue 3?

import in main.js inside your vuejs 3 project, /src folder. //vue-app/src/main. js import { library } from "@fortawesome/fontawesome-svg-core"; import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome"; import { fas } from '@fortawesome/free-solid-svg-icons' library.

How do I add icons to Vue?

You can add icons to your Vue 2 or Vue 3 project using a string format or an array format. You can now call the icons, just use the syntax below wherever you want the icons to appear in your project, like in the src/App. vue file.

How do you use font family?

Start with the font you want, and always end with a generic family, to let the browser pick a similar font in the generic family, if no other fonts are available. Note: Separate each value with a comma. Note: If a font name contains white-space, it must be quoted.


2 Answers

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>
like image 161
Venkatesh A Avatar answered Oct 02 '22 02:10

Venkatesh A


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');
}
like image 45
Frnak Avatar answered Oct 02 '22 03:10

Frnak