174

My apologies if this is a very simple question, but how do you use google material icons without a

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 

?

I would like my app to be able to display the icons even when the user does not have an internet connection

Luke Tan
  • 2,048
  • 2
  • 13
  • 21
  • 3
    This article explains for Angular, thought might be helpful for someone, https://thecodeframework.com/host-google-material-icon-fonts-locally-in-angular-in-5-simple-steps/ – Gagan Jul 21 '20 at 03:09

25 Answers25

164

Method 2. Self hosting Developer Guide

Release 4 changed things a bit, please investigate recommended formats and hosting solution.

Download release 3.0.2 from github (assets: zip file), unzip, and copy the font folder, containing the material design icons files, into your local project -- https://github.com/google/material-design-icons/releases

You only need to use the font folder from the archive: it contains the icons fonts in the different formats (for multiple browser support) and boilerplate css.

  • Replace the source in the url attribute of @font-face, with the relative path to the iconfont folder in your local project, (where the font files are located) eg. url("iconfont/MaterialIcons-Regular.ttf")
@font-face {
   font-family: 'Material Icons';
   font-style: normal;
   font-weight: 400;
   src: url(iconfont/MaterialIcons-Regular.eot); /* For IE6-8 */
   src: local('Material Icons'),
        local('MaterialIcons-Regular'),
        url(iconfont/MaterialIcons-Regular.woff2) format('woff2'),
        url(iconfont/MaterialIcons-Regular.woff) format('woff'),
        url(iconfont/MaterialIcons-Regular.ttf) format('truetype');
}

.material-icons {
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  font-size: 24px;  /* Preferred icon size */
  display: inline-block;
  line-height: 1;
  text-transform: none;
  letter-spacing: normal;
  word-wrap: normal;
  white-space: nowrap;
  direction: ltr;

  /* Support for all WebKit browsers. */
  -webkit-font-smoothing: antialiased;
  /* Support for Safari and Chrome. */
  text-rendering: optimizeLegibility;

  /* Support for Firefox. */
  -moz-osx-font-smoothing: grayscale;

  /* Support for IE. */
  font-feature-settings: 'liga';
}

<i class="material-icons">face</i>

NPM / Bower Packages

Google officially has a Bower and NPM dependency option -- follow Material Icons Guide 1

Using bower : bower install material-design-icons --save

Using NPM : npm install material-design-icons --save

Material Icons : Alternatively look into Material design icon font and CSS framework for self hosting the icons, from @marella's https://marella.me/material-icons/


Note

It seems google has the project on low maintenance mode. The last release was, at time of writing, 3 years ago!

There are several issues on GitHub regarding this, but I'd like to refer to @cyberalien comment on the issue Is this project actively maintained? #951 where it refers several community projects that forked and continue maintaining material icons.


bfmags
  • 2,885
  • 2
  • 17
  • 28
  • How would you do this for a phone GAP app where you would not have a domain? – Luke Tan May 17 '16 at 10:08
  • 1
    target your local folder in the source url of @font-face : url( "iconfont/MaterialIcons-Regular.woff2" -- updated answer – bfmags May 17 '16 at 10:36
  • Hey, Can you please make an updated answer of this. I have downloaded my mdl icons with npm. – TheBAST Dec 10 '17 at 05:26
  • 3
    Solved my problem … I just wanted to note out that we only need the `iconfont` folder from the whole archive. – securecurve Aug 16 '18 at 10:01
  • for this type of "move cdn resource to be served locally" it usually would pay off to have a solid over 3000 requests average response time data BEFORE and AFTER the change - you would be surprised how-often it is not worth it, regardless of your effort to achieve it ... – Yordan Georgiev Nov 02 '18 at 13:37
  • `material-design-icons` is not really usable anymore, it is missing a lot of icons and the installing process sometimes freezes. – late1 Aug 04 '22 at 15:16
  • In 2020 there was an update to a new version (4.x). I downloaded the older source code on https://github.com/google/material-design-icons/releases/tag/3.0.2 which matches the css given above. – remy Aug 08 '22 at 15:13
  • One issue: the stylesheet references four different font formats (.eot, woff, woff2 and ttf) but the Github repo only has ttf. Opening the CSS from Google Fonts in a browser directly will give you a link to the woff2, but that’s all a bit hackish and still no source for eot and woff. (I did check Google’s official documentation, and it is not helpful either.) Any ideas? – user149408 Apr 28 '23 at 18:32
  • Release tagged 3.0.2 still holds the different formats https://github.com/google/material-design-icons/tree/3.0.2/iconfont – bfmags Apr 29 '23 at 21:14
66

I'm building for Angular 4/5 and often working offline and so the following worked for me. First install the NPM:

npm install material-design-icons --save

Then add the following to styles.css:

@import '~material-design-icons/iconfont/material-icons.css';
bownie
  • 1,608
  • 15
  • 21
  • 17
    when call "npm install" wait wait wait lol... but works after long time – Sergio Cabral May 27 '18 at 19:47
  • 2
    @SergioCabral Luckily you made the comment about waiting, cause there were about 5 times I thought this install isn't going anywhere... :) – Alfa Bravo Oct 03 '18 at 12:59
  • 1
    Thank You Loved It This Is Best Solution Thank You <3 – Ali Jamal Nov 08 '18 at 17:21
  • Working for me also... :) – Aupr Jun 18 '19 at 09:41
  • 4
    any idea how to add outlined icons? – wutzebaer Jan 14 '20 at 08:06
  • I followed the npm directions above and all works great, except I am missing two icons: toggle_off and toggle_on. I suspect the npm repo might not be up to date? – Reuben Sivan May 28 '20 at 23:00
  • 3
    It turns out the google repository is not well maintained. The solution I found is to use npm install material-design-icons-iconfont, a fork of the google repository with many fixes and performance improvements. – Reuben Sivan May 29 '20 at 00:41
  • When I use npm install it takes a long time, and then adds no new files to my AspNetCore project. Did anyone have a similar problem? – Christoph B Sep 27 '21 at 13:08
  • 3
    @wutzebaer You can use the material-icons package. FMI https://github.com/marella/material-icons#readme – ASM Aug 05 '22 at 13:01
  • the `npm install @material-design-icons/font@latest` package is way lighter. import with `@import '@material-design-icons/font';` [docs](https://github.com/marella/material-design-icons/tree/main/font#usage) – Tovar Jun 19 '23 at 17:15
32

The upper approaches does not work for me. I download the files from github, but the browser did not load the fonts.

What I did was to open the material icon source link:

https://fonts.googleapis.com/icon?family=Material+Icons

and I saw this markup:

    /* fallback */
@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: local('Material Icons'), local('MaterialIcons-Regular'), url(https://fonts.gstatic.com/s/materialicons/v22/2fcrYFNaTjcS6g4U3t-Y5ZjZjT5FdEJ140U2DJYC3mY.woff2) format('woff2');
}

.material-icons {
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  font-size: 24px;
  line-height: 1;
  letter-spacing: normal;
  text-transform: none;
  display: inline-block;
  white-space: nowrap;
  word-wrap: normal;
  direction: ltr;
  -moz-font-feature-settings: 'liga';
  -moz-osx-font-smoothing: grayscale;
}

I open the woff font url link https://fonts.gstatic.com/s/materialicons/v22/2fcrYFNaTjcS6g4U3t-Y5ZjZjT5FdEJ140U2DJYC3mY.woff2

and download the woff2 file.

Then I replace the woff2 file path with the new one in material-icons.css

@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: url(iconfont/MaterialIcons-Regular.eot); /* For IE6-8 */
  src: local('Material Icons'),
       local('MaterialIcons-Regular'),
       /* Old file: url(iconfont/MaterialIcons-Regular.woff2) format('woff2'), */
       /* load new file */ 
       url(2fcrYFNaTjcS6g4U3t-Y5ZjZjT5FdEJ140U2DJYC3mY.woff2) format('woff2'),
       url(iconfont/MaterialIcons-Regular.ttf) format('truetype');
}

That makes thing works for me.

Kaloyan Stamatov
  • 3,894
  • 1
  • 20
  • 30
  • Thanks! I had the same issue and this fixed it for me. – David Aug 25 '17 at 10:20
  • I solve my issue with this approach, but later I found another git repo, from where I get the proper release. – Kaloyan Stamatov Aug 25 '17 at 10:25
  • 1
    @ Kaloyan Stamatov would be nice to share that git repo.. since lot of people have issue with outdated official repo :) – Grashopper May 06 '19 at 12:30
  • @Grashopper, I do not have it anymore. Sorry – Kaloyan Stamatov May 07 '19 at 06:49
  • 1
    more up to date in 2019, this solution worked for me as well. – compt Jul 16 '19 at 19:24
  • this is the only way to get the latest icons that I could find... – BoDeX Jul 14 '20 at 07:19
  • in 8/2020 latest v55 link: https://fonts.gstatic.com/s/materialicons/v55/2fcrYFNaTjcS6g4U3t-Y5ZjZjT5FdEJ140U2DJYC3mY.woff2 – Timelot Aug 21 '20 at 10:38
  • i had to install npm package `npm i material-design-icons` then edited: `apps\appName\src\styles.css` and added the content of the file: `\node_modules\material-design-icons\iconfontmaterial-icons.css` but i had to change the fonts path to be of this format: `url('~material-design-icons/iconfont/MaterialIcons-Regular.woff2')` (added the qutes ' and prefix: `~material-design-icons/iconfont/` before each file name ) – Shaybc Nov 21 '21 at 23:22
  • @Shaybc check the date on the npm package. It's old! It's best to get the fonts directly from Google fonts as Kaloyan says above. Or you can get them from GitHub. – moefinley Mar 14 '22 at 15:38
26

As of 2020, my approach is to use the material-icons-font package. It simplifies the usage of Google's material-design-icons package and the community based material-design-icons-iconfont.

  1. Install the package. npm install material-icons-font --save

  2. Add the path of the package's CSS file to the style property of your project's angular.json file.

    ... "styles": [ "./node_modules/material-icons-font/material-icons-font.css" ], ...

  3. If using SCSS, copy content below to the top of your styles.scss file.

    @import '~material-icons-font/sass/variables'; @import '~material-icons-font/sass/mixins'; $MaterialIcons_FontPath: "~material-icons-font/fonts"; @import '~material-icons-font/sass/main'; @import '~material-icons-font/sass/Regular';

  4. Use the icons in the HTML file of your project.

    // Using icon tag
    <i class="material-icons">face</i>
    <i class="material-icons md-48">face</i>
    <i class="material-icons md-light md-inactive">face</i>
    
    // Using Angular Material's <mat-icon> tag
    <mat-icon>face</mat-icon>
    <mat-icon>add_circle</mat-icon>
    <mat-icon>add_circle_outline</mat-icon>
    

Icons from @angular/material tend to break when developing offline. Adding material-icons-font package in conjunction with @angular/material allows you to use the tag while developing offline.

HelloWorldPeace
  • 1,315
  • 13
  • 12
18

If you use webpack project, after

npm install material-design-icons --save

you just need to

import materialIcons from 'material-design-icons/iconfont/material-icons.css'
  • 8
    I don't recommend doing this, this repo is far too big, use one desc. below https://www.npmjs.com/package/material-design-icons-iconfont I also noticed that browser will cache https://fonts.googleapis.com/icon?family=Material+Icons and it'll work in the offline mode. Checkout https://keemor.github.io/#/ – keemor Oct 17 '17 at 13:44
  • I am newbie in npm can you elaborate `import materialIcons from 'material-design-icons/iconfont/material-icons.css'` . We adding this line to where? app.js or somewhere else. I am using framework7 with Vue and I tried. It didn't work for me. – Suat Atan PhD Nov 06 '17 at 11:53
  • @SuatAtanPhD you add this import like any other style imports. If you use webpack you need something like style-loader, css-loader and you can place this import anywhere in your js code, for example in index.js or app.js etc. – Vladislav Kosovskikh Nov 06 '17 at 19:22
  • 1
    Such approach is recommended for building an corporate web application which may work in internal network without access to Internet – Yurii Bratchuk Feb 20 '18 at 15:26
17

This may be an easy Solution


Get this repository that is a fork of the original repository Google published.

Install it with bower or npm

bower install material-design-icons-iconfont --save

npm install material-design-icons-iconfont --save

Import the css File on your HTML Page

<style>
  @import url('node_modules/material-design-icons-iconfont/dist/material-design-icons.css');
</style>

or

<link rel="stylesheet" href="node_modules/material-design-icons-iconfont/dist/material-design-icons.css">

Test: Add an icon inside body tag of your HTML File

<i class="material-icons">face</i>

If you see the face icon, you are OK.

If does not work, try add this .. as prefix to node_modules path:

<link rel="stylesheet" href="../node_modules/material-design-icons-iconfont/dist/material-design-icons.css">
Suat Atan PhD
  • 1,152
  • 13
  • 27
Md. Harun Or Rashid
  • 2,000
  • 4
  • 21
  • 37
  • Employing the _material-design-icons-iconfont_ package works even with the Angular's standard `mat-icon` component. Thus the transition is seamless. – Yuri Dec 20 '18 at 09:53
  • 1
    This is the solution that worked for me, except the stylesheet link in index.html didn't work - I had to add an import into my Styles.scss file: `@import"../node_modules/material-design-icons-iconfont/dist/material-design-icons.css";` Then my locally hosted icons leapt into life – Ade Oct 18 '19 at 07:25
  • Also, importing into your scss means that it is a build time dependency and you can NPM install --dev to not make it part of the prod node environment footprint. – steve dunning Jan 13 '21 at 19:14
11

To Avoid

npm i material-design-icons

Google isn't publishing any new version to their npm repository, so please avoid using the npm i material-design-icons since the last publish was 5 years ago.

To use

  1. Download the needed font from their repo. you can ignore the .codepoints extension. Be aware, they do not have .woff2 and .woff type anymore, but .ttf or .otf works just fine.
  2. Add them to your assets folder
  3. Into a css file, do add the following
@font-face {
  font-family: 'Material Icons';
  font-weight: 400;
  font-style: normal;
  src: local('Material Icons'), local('MaterialIcons-Regular'),
    url('/assets/fonts/MaterialIcons-Regular.ttf') format('truetype');
}

.material-icons {
  display: inline-block;
  font-family: 'Material Icons';
  font-size: 24px; /* Preferred icon size */
  font-weight: normal;
  line-height: 1;
  font-style: normal;
  text-transform: none;
  letter-spacing: normal;
  word-wrap: normal;
  white-space: nowrap;
  direction: ltr;

  /* Support for all WebKit browsers. */
  -webkit-font-smoothing: antialiased;
  /* Support for Safari and Chrome. */
  text-rendering: optimizeLegibility;

  /* Support for Firefox. */
  -moz-osx-font-smoothing: grayscale;

  /* Support for IE. */
  font-feature-settings: 'liga';
}
  1. Then add the .material-icons class
<i class="material-icons">face</i>

Angular users do not need to add .material-icons if they already use mat-icon

Outline & Regular at the same time

Both, outline & regular one?
Here is the step.

I'm using the scss @extend method for that purpose because I'm lazy.

@font-face {
  font-family: 'Material Icons Outlined';
  font-weight: 400;
  font-style: normal;
  src: local('Material Icons Outlined'), local('MaterialIconsOutlined-Regular'),
    url('/assets/fonts/MaterialIconsOutlined-Regular.otf') format('truetype');
  font-display: fallback;
}

.material-icon {
  // Same content as the previous example without the `font-family`
}

.material-icons {
  @extend .material-icon;
  font-family: 'Material Icons';
}

.material-icons-outlined {
  @extend .material-icon;
  font-family: 'Material Icons Outlined';
}
<mat-icon class="material-icons-outlined" svgIcon="dark_mode-outline"></mat-icon>
Raphaël Balet
  • 6,334
  • 6
  • 41
  • 78
  • Just a question for this: If I want to use `regular` as well as `outline` ==> how to define it in the `font-face`? Just a new entry with the same `font-familiy`? And then how to use the `outline` vs. the `regular` in a ``? Some kind of `font-set`? – LeO Aug 04 '22 at 14:34
  • Hi, thx for the very quick and helpful reply. When I follow your link about the repro I find the Material font and a hint for the variablefont. Right now we're using Angular 14 and many of the outlined icons wont work. Therefore I happy to have your solution. Nevertheless the question arise if we should use the variablefont or not. Is there somewhere a useful link about what's the difference, how to use, etc? – LeO Aug 04 '22 at 15:34
  • 1
    @LeO Glad I could help. I'm not aware of any better solution yet. But feel free to add a comment here if you find anything useful & I'll be glad to update my answer. I have to say, my bet is that google will release something with their new icon set & animation. This is one of the reason why this workaround works for now and why I wouldn't invest to much time into it. Cheers – Raphaël Balet Aug 04 '22 at 18:48
  • Just for the sake of completeness: One needs to set (in `.material-icon`) the `width` and `height` to `24px` and the `margin` to `0`. Otherwise it might happen that the Icon shrinks too much in a `flex` and some parts are truncated as consequence. Perhaps you can update as well your answer :-) – LeO Sep 07 '22 at 15:38
10

Use material-design-icons-iconfont

Full disclosure, I'm the author of this package

Google's material-design-icons project is on low maintenance and out of date for a while. There's a gap between the version in https://material.io/icons/ and the version in material-design-icons.

I've created material-design-icons-iconfont to address these major issues:

  • material-design-icons jams npm install - all irrelevant svg/xml/... files has been removed
  • Font files are always up-to-date straight from Google Fonts CDN
  • Support for scss

Install via npm

npm install material-design-icons-iconfont --save

It depends on how you pack your web application (webpack/gulp/bower/...), you'll need to import the .css/.scss file (and might change the relative fonts path)


Import Using SCSS

Import in one of your sass files

$material-design-icons-font-path: '~material-design-icons-iconfont/dist/fonts/';
@import '~material-design-icons-iconfont/src/material-design-icons';

Later on, reference your desired icon <i class="material-icons"> + icon-id + </i>

<i class="material-icons">contact_support</i>

Demo page

It comes with a light demo page to assist searching and copy-pasting fonts

image

Jossef Harush Kadouri
  • 32,361
  • 10
  • 130
  • 129
  • 1
    I'm having a problem with PWA. Icons are working while you are in browser but once you add shortcut on home screen and run the app icons are not showing. – Wlada Jun 24 '19 at 14:47
8

My recipe has three steps:

  1. to install material-design-icons package

    npm install material-design-icons
    
  2. to import material-icons.css file into .less or .scss file/ project

    @import "~/node_modules/material-design-icons/iconfont/material-icons.css";
    
  3. to include recommended code into the reactjs .js file/ project

    <i className='material-icons' style={{fontSize: '36px'}}>close</i>
    
Roman
  • 19,236
  • 15
  • 93
  • 97
6

I have tried to compile everything that needs to be done for self-hosting icons in my answer. You need to follow these 4 simple steps.

  1. Open the iconfont folder of the materialize repository

    link- https://github.com/google/material-design-icons/tree/master/iconfont

  2. Download these three icons files ->

    MaterialIcons-Regular.woff2 - format('woff2')

    MaterialIcons-Regular.woff - format('woff')

    MaterialIcons-Regular.ttf - format('truetype');

    Note- After Download you can rename it to whatever you like.

  3. Now, go to your CSS and add this code

@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: url(MaterialIcons-Regular.eot); /* For IE6-8 */
  src: local('Material Icons'),
       local('MaterialIcons-Regular'),
       url(MaterialIcons-Regular.woff2) format('woff2'),
       url(MaterialIcons-Regular.woff) format('woff'),
       url(MaterialIcons-Regular.ttf) format('truetype');
}

.material-icons {
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  font-size: 24px;  /* Preferred icon size */
  display: inline-block;
  line-height: 1;
  text-transform: none;
  letter-spacing: normal;
  word-wrap: normal;
  white-space: nowrap;
  direction: ltr;

  /* Support for all WebKit browsers. */
  -webkit-font-smoothing: antialiased;
  /* Support for Safari and Chrome. */
  text-rendering: optimizeLegibility;

  /* Support for Firefox. */
  -moz-osx-font-smoothing: grayscale;

  /* Support for IE. */
  font-feature-settings: 'liga';
}

Note : The address provided in src:url(...) should be with respect to the 'CSS File' and not the index.html file. For example it can be src : url(../myicons/MaterialIcons-Regular.woff2)


  1. You are ready to use now and here is how it can be done in HTML

<i class="material-icons">face</i>

Click here to see all the icons that can be used.

abhinav1602
  • 1,190
  • 17
  • 18
4

2019 Update here:

To self host your material icons, the Regular ones, Rounded, Sharp, all the variants. Go get them from this repo: https://github.com/petergng/material-icon-font

For some reason I dont know why these fonts are not easily accessible from Google repositories.

But here you go.

After downloading the package, go to bin folder and you'll see the four variants. Of course, it is up to you to use whichever.

To use them, create a css file and

  1. Generate a font face for each variant you need:
@font-face {
    font-family: 'Material Icons';
    font-style: normal;
    font-weight: 400;
    src: url(./icons/regular/Material-Icons-Regular.eot); /* For IE6-8 */
    src: local('Material-Icons-Regular'),
    url(./icons/regular/Material-Icons-Regular.woff2) format('woff2'),
    url(./icons/regular/Material-Icons-Regular.woff) format('woff'),
    url(./icons/regular/Material-Icons-Regular.ttf) format('truetype');
}

@font-face {
    font-family: 'Material Icons Round';
    font-style: normal;
    font-weight: 400;
    src: url(./icons/round/Material-Icons-Round.eot); /* For IE6-8 */
    src: local('Material-Icons-Round'),
    url(./icons/round/Material-Icons-Round.woff2) format('woff2'),
    url(./icons/round/Material-Icons-Round.woff) format('woff'),
    url(./icons/round/Material-Icons-Round.svg) format('svg'),
    url(./icons/round/Material-Icons-Round.ttf) format('truetype');
}

@font-face {
    font-family: 'Material Icons Sharp';
    font-style: normal;
    font-weight: 400;
    src: url(./icons/sharp/Material-Icons-Sharp.eot); /* For IE6-8 */
    src: local('Material-Icons-Sharp'),
    url(./icons/sharp/Material-Icons-Sharp.woff2) format('woff2'),
    url(./icons/sharp/Material-Icons-Sharp.woff) format('woff'),
    url(./icons/sharp/Material-Icons-Sharp.svg) format('svg'),
    url(./icons/sharp/Material-Icons-Sharp.ttf) format('truetype');
}

The url will link to where the icons are located in your project.

  1. Now lets register the icon classes:
.material-icons-outlined, .material-icons {
    font-weight: normal;
    font-style: normal;
    font-size: 24px;  /* Preferred icon size */
    display: inline-block;
    line-height: 1;
    text-transform: none;
    letter-spacing: normal;
    word-wrap: normal;
    white-space: nowrap;
    direction: ltr;
}

This will make both .material-icons-outlined, and .material-icons classes use the same defaults. If you want to to use .material-icons-sharp, just append it to the two class names.

  1. Finally, let us plug-in the font face you pulled in from step 1.
.material-icons {
    font-family: 'Material Icons';
}
.material-icons-outlined {
    font-family: 'Material Icons Outline';
}

Again, to use more variant, like Sharp, just add its block like the two above.

Once done...go to your html and use your newly minted icons.

<i class="material-icons-outlined">hourglass_empty</i>
<i class="material-icons">phone</i>
Wale
  • 1,321
  • 16
  • 11
3

After you have done npm install material-design-icons, add this in your main CSS file:

@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: url(~/material-design-icons/iconfont/MaterialIcons-Regular.eot); /* For IE6-8 */
  src: local('Material Icons'),
       local('MaterialIcons-Regular'),
       url(~material-design-icons/iconfont/MaterialIcons-Regular.woff2) format('woff2'),
       url(~material-design-icons/iconfont/MaterialIcons-Regular.woff) format('woff'),
       url(~material-design-icons/iconfont/MaterialIcons-Regular.ttf) format('truetype');
}

.material-icons {
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  font-size: 24px;  /* Preferred icon size */
  display: inline-block;
  line-height: 1;
  text-transform: none;
  letter-spacing: normal;
  word-wrap: normal;
  white-space: nowrap;
  direction: ltr;

  /* Support for all WebKit browsers. */
  -webkit-font-smoothing: antialiased;
  /* Support for Safari and Chrome. */
  text-rendering: optimizeLegibility;

  /* Support for Firefox. */
  -moz-osx-font-smoothing: grayscale;

  /* Support for IE. */
  font-feature-settings: 'liga';
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
3

With angular cli

npm install angular-material-icons --save

or

npm install material-design-icons-iconfont --save

material-design-icons-iconfont is the latest updated version of the icons. angular-material-icons is not updated for a long time

Wait wait wait install to be done and then add it to angular.json -> projects -> architect -> styles

 "styles": [
          "node_modules/material-design-icons/iconfont/material-icons.css",
          "src/styles.scss"
        ],

or if you installed material-desing-icons-iconfont then

"styles": [
              "node_modules/material-design-icons-iconfont/dist/material-design-icons.css",
              "src/styles.scss"
            ],
Wlada
  • 1,052
  • 14
  • 25
2

On http://materialize.com/icons.html the style header information you include in the page,you can go to the actual Hyperlink and make localized copies to use icons offline.

Here's how.NB: You will download two Files in all icon.css and somefile.woff.

  1. Go to the following URL as required in the header

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> .

Save page as whatever_filename.css. Default is icon.css

  1. Look for a line like this

src: local('Material Icons'), local('MaterialIcons-Regular'), url(https://fonts.gstatic.com/s/materialicons/v22/2fcrYFNaTjcS6g4U3t-Y5ZjZjT5FdEJ140U2DJYC3mY.woff2)

  1. Visit the URL that has .woff ending it

https://fonts.gstatic.com/s/materialicons/v22/2fcrYFNaTjcS6g4U3t-Y5ZjZjT5FdEJ140U2DJYC3mY.woff2 . Your browser will automatically download it. Save it in your CSS folder.

  1. You should now have the two files icon.css and 2fcrYFNa....mY.wof22, save them both in your css. Now make edits in your css header location to the icon.css in your directories. Just make sure the .woff2 file is always in the same folder as the icon.css. Feel free to edit the long file names.

2

The question title asks how to host material icons offline but the body clarifies that the objective is to use the material icons offline (emphasis mine).

Using your own copy of the material icons files is a valid approach/implementation. Another, for those that can use a service worker is to let the service worker take care of it. That way you don't have the hassle of obtaining a copy and keeping it up to date.

For example, generate a service worker using Workbox, using the simplest approach of running workbox-cli and accepting the defaults, then append the following text to the generated service worker:

workboxSW.router.registerRoute('https://fonts.googleapis.com/(.*)',
workboxSW.strategies.cacheFirst({
  cacheName: 'googleapis',
  cacheExpiration: {
    maxEntries: 20
  },
  cacheableResponse: {statuses: [0, 200]}
})
);

You can then check it was cached in Chrome using F12 > Application > Storage > IndexedDB and look for an entry with googleapis in the name.

Joseph Simpson
  • 4,054
  • 1
  • 24
  • 28
  • I got this to work also with some small changes. I changed "workboxSW" to "workbox". I changed "router" to "routing" and added this code into my sw_config.js file that gets generated when using the workbox cli wizard. – Jason Allshorn Jan 15 '19 at 18:35
2

Kaloyan Stamatov method is the best. First go to https://fonts.googleapis.com/icon?family=Material+Icons. and copy the css file. the content look like this

/* fallback */
@font-face {
    font-family: 'Material Icons';
    font-style: normal;
    font-weight: 400;
    src: url(https://fonts.gstatic.com/s/materialicons/v37/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2) format('woff2');
}
    
.material-icons {
    font-family: 'Material Icons';
    font-weight: normal;
    font-style: normal;
    font-size: 24px;
    line-height: 1;
    letter-spacing: normal;
    text-transform: none;
    display: inline-block;
    white-space: nowrap;
    word-wrap: normal;
    direction: ltr;
    -moz-font-feature-settings: 'liga';
    -moz-osx-font-smoothing: grayscale;
}

Paste the source of the font to the browser to download the woff2 file https://fonts.gstatic.com/s/materialicons/v37/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2 Then replace the file in the original source. You can rename it if you want No need to download 60MB file from github. Dead simple My code looks like this

@font-face {
    font-family: 'Material Icons';
    font-style: normal;
    font-weight: 400;
    src: url(materialIcon.woff2) format('woff2');
}

.material-icons {
    font-family: 'Material Icons';
    font-weight: normal;
    font-style: normal;
    font-size: 24px;
    line-height: 1;
    letter-spacing: normal;
    text-transform: none;
    display: inline-block;
    white-space: nowrap;
    word-wrap: normal;
    direction: ltr;
    -moz-font-feature-settings: 'liga';
    -moz-osx-font-smoothing: grayscale;
}

while materialIcon.woff2 is the downloaded and replaced woff2 file.

chris_wire
  • 21
  • 2
  • 1
    Doesn't work on all browsers, see this SO answer for more information: https://stackoverflow.com/questions/11002820/why-should-we-include-ttf-eot-woff-svg-in-a-font-face – xaviert Oct 19 '18 at 08:31
1

I solved it using this package (@mdi/font) and importing it on main.js:

import '@mdi/font/css/materialdesignicons.css'
Asanka Sampath
  • 545
  • 4
  • 12
1

While I was also facing this same challenge on my angular material project, I came up with a working offline solution. save all the details.

After downloading the resource, just follow these steps on the README.md file

STEP 1

Copy the distribution into your project

STEP 2

import the stylesheets in your angular.json imports array

Example

`angular.json [
        {
            style[
                "css/material.css", "css/icons.css"
            ]
        } `

STEP 3

You should see your icons appear offline after your next auto reload.

Optionally, you can comment or take out this line https://fonts.gstatic.com https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap https://fonts.googleapis.com/icon?family=Material+Icons from your index.html file.

HAPPYCODING :)

I created an offline repository for ngMatIcons you can download it from this link. :)

1

Adding this in case it helps someone... This might not work for your use case, but it might work for someone. I have an offline computer where I use an app that uses materialize. I was also trying to solve this problem, but that github repo is huge, and I was having a difficult time following all the instructions. Additionally, the <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> tag was slowing the opening of my app on the offline computer, as it kept trying to download that file, but couldn't.

Here is what I did (working example at the bottom of this comment)

  1. Download the file at https://fonts.googleapis.com/icon?family=Material+Icons and store it as a local css file in your website directory. This file will require one modification, explained in step 3.
  2. Add a <link> tag to index.html pointing to the file you saved in step 1. (For example, I saved https://fonts.googleapis.com/icon?family=Material+Icons to assets/css/materialize_offline_files/materialize-icons.css in my website directory, then added this to index.html: <link href="assets/css/materialize_offline_files/materialize-icons.css" rel="stylesheet"> rel="stylesheet">.) Note: Make sure to add this link tag before your materialize.css imports.
  3. Modification to file from step 1. If you look at the file at https://fonts.googleapis.com/icon?family=Material+Icons, you'll find this line of css code for the fall back font: src: url(https://fonts.gstatic.com/s/materialicons/v139/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2) format('woff2');. Download the file at that src url, https://fonts.gstatic.com/s/materialicons/v139/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2, save it to the same directory as materialize_icons.css (the file from step 1), and change that line of code to: src: url("flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2") format('woff2');

Working example (4 files):

index.html

<!DOCTYPE html>
<html>
<head>
    <link href="assets/css/materialize_offline_files/materialize-icons.css" rel="stylesheet">
    <link href="assets/css/materialize.min.css" rel="stylesheet">
</head>
<body>
    <button class="btn waves-effect waves-light">Hello!
            <i class="material-icons right">send</i>
    </button>
</body>
</html>

assets/css/materialize_offline_files/materialize-icons.css

/*
 * File downloaded from https://fonts.googleapis.com/icon?family=Material+Icons
 * with slight modification (src url of fallback font)
 */

/* fallback */
@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  /**src: url(https://fonts.gstatic.com/s/materialicons/v139/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2) format('woff2');*/
  src: url("flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2") format('woff2');
  /**
   * flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2 is the file
   * downloaded from 
   * https://fonts.gstatic.com/s/materialicons/v139/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2
   * NOTE:: css src url attributes are relative to the css file,
   * so store flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2
   * IN THE SAME DIR as this css file, for that modification to work.
   */
}

.material-icons {
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  font-size: 24px;
  line-height: 1;
  letter-spacing: normal;
  text-transform: none;
  display: inline-block;
  white-space: nowrap;
  word-wrap: normal;
  direction: ltr;
  -moz-font-feature-settings: 'liga';
  -moz-osx-font-smoothing: grayscale;
}

Finally (remaining 2 files), make sure you have the files:

I thought this would just eliminate the problem the call to fonts.googleapis.com slowing down my app, but actually the few icons I use are actually showing up on the offline computer now. I'm not sure if it will work for all the icons, but it is working for me.

bikz
  • 415
  • 4
  • 11
0

Install npm package

npm install material-design-icons --save

Put css file path to styles.css file

@import "../node_modules/material-design-icons-iconfont/dist/material-design-icons.css";
Ahmet Arslan
  • 5,380
  • 2
  • 33
  • 35
0

By the way there is video available on youtube with step by step instructions.

https://youtu.be/7j6eOkhISzk

  1. These are the steps. Download materialize icon package from https://github.com/google/material-design-icons/releases

  2. Copy the icon-font folder and rename it to icons.

  3. Open the materialize.css file and update the following paths:

a. from url(MaterialIcons-Regular.eot) to url(../fonts/MaterialIcons-Regular.eot) b. from url(MaterialIcons-Regular.woff2) format('woff2') to url(../fonts/MaterialIcons-Regular.woff2) format('woff2') c. from url(MaterialIcons-Regular.woff) format('woff') to url(../fonts/MaterialIcons-Regular.woff) format('woff') d. from url(MaterialIcons-Regular.ttf) format('truetype') to url(../fonts/MaterialIcons-Regular.ttf) format('truetype')

  1. Copy the materialize-icon.css to your css folder and reference it in your html file.

Everything will work like magic !

SicMundus
  • 11
  • 1
0

I chose a lazy solution with apache to evade dissuasions:

<link href="https://MYHOST/gfonts/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
<link href="https://MYHOST/gfonts/icon?family=Material+Icons|Material+Icons+Outlined" rel="stylesheet">

Add small apache plugin

a2enmod substitute

And in my apache config

AddOutputFilterByType INFLATE;SUBSTITUTE;DEFLATE text/html text/plain text/xml text/css
Substitute "s|https://fonts.gstatic.com/|/gsfonts/|i"
SSLProxyEngine on
ProxyPass "/gfonts/"  "https://fonts.googleapis.com/"
ProxyPassReverse "/gfonts/"  "https://fonts.googleapis.com/"
ProxyPass "/gsfonts/"  "https://fonts.gstatic.com/"
ProxyPassReverse "/gsfonts/"  "https://fonts.gstatic.com/"
wutzebaer
  • 14,365
  • 19
  • 99
  • 170
0

The answers here are all old. In 2023 you want material-icons npm package NOT material-design-icons. It's much better maintained and automatically updated.

npm install material-icons --save

It's one line of code to get it working:

import 'material-icons/iconfont/material-icons.css';

OR

@import 'material-icons/iconfont/material-icons.css';

There are other alternatives to get it working but see the linked docs for more.

halfer
  • 19,824
  • 17
  • 99
  • 186
danday74
  • 52,471
  • 49
  • 232
  • 283
-1
npm install material-design-icons

and

@import '~material-design-icons/iconfont/material-icons.css';

worked also for me with Angular Material 8

N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
side105
  • 1
  • 1
-1

Added this to the web config and the error went away

<system.webServer>
    <staticContent>
      <remove fileExtension=".woff" /> 
      <mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
      <remove fileExtension=".woff2" /> 
      <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
    </staticContent>
tfa
  • 1,643
  • 16
  • 18