Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice/Style for importing font into angular 4 cli web application

I have been working on an Angular 4 personal project, and wanted to add the Ubuntu font family to my Angular application. What is the best practice or style for adding a number of custom fonts to a project? I currently have saved the ubuntu font family into /assets/fonts/ubuntu-font-family-0.83 and added it to the outer most component CSS file, app.component.css with font face.

@font-face {    font-family: 'Ubuntu';    src: url('/assets/fonts/ubuntu-font-family-0.83/Ubuntu-R.ttf'); } 

By putting this in the original component I don't have to redefine the font in nested components i just treat it like a default font-family.

Is there a clearer/better way to do this and still cut out duplicate code?

like image 932
ob1 Avatar asked Jul 03 '17 05:07

ob1


People also ask

How do I use custom fonts in angular 8?

Step 1: In the folder src/assets create a new folder fonts so the new path looks like src/assets/fonts . Step 2: Place your custom font . ttf file inside this src/assets/fonts so the new path looks like src/assets/fonts/custom_font. ttf .


2 Answers

There is better way to include fonts to the website, not only to angular app.

Checkout https://fonts.google.com

Why it is better?

  • higher performance
  • higher chance, that your customer will have font in his cache
  • you don't have to worry about attaching files, and use yours origin bandwidth

In your case you would import following in your css file:

@import url('https://fonts.googleapis.com/css?family=Ubuntu'); 

You can place it in the main .css file, included by index.html. Or you can use <link> tag and include fonts in your headers (also in index.html)

<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet"> 
like image 185
Maciej Treder Avatar answered Oct 14 '22 09:10

Maciej Treder


This is best way to use google font locally with angular 2,4,5,6,7 and more, First Download Google Font and add this inside the assets folder and use it is as per your requirement.

In angular.json call this in src/assets

"assets": [     "src/assets"  ], 

In css file add this font-face in top

@font-face {   font-family: 'Montserrat';   src: url(/assets/Montserrat/Montserrat-Light.ttf) format("truetype");   font-weight:300; } 

At Last use it as per your requirement like

body{  font-family: 'Montserrat', sans-serif; } 
like image 39
codeuix Avatar answered Oct 14 '22 11:10

codeuix