4

I want to host Material Icons offline for my offline web development project (I will not have internet on the computer where is deployed). From my Google search, I found this SO answer. BUT it is Not working for me. My question is how to make it work. How to host material design icons offline for my offline project?

I have attached a .zip file of my SSCCE project, which reproduces the problem, here.

Basically I downloaded the MaterialIcons-Regular.eot, MaterialIcons-Regular.ttf, MaterialIcons-Regular.woff and MaterialIcons-Regular.woff2 from here and put them in my project's directory.

Here is my index file:

<!DOCTYPE html>

<html>

<head>
    <title>MaterializeTest</title>

    <link rel="stylesheet" href="material-fonts.css" />
    <link type="text/css" rel="stylesheet" href="materialize.min.css" />

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width; initial-scale=1"/>

    <script src="jquery.min.js"></script>
    <script src="materialize.min.js"></script>
</head>

<body>
    <a href="#!"><i class="material-icons">chevron_left</i></a>
</body>

</html>

And here is the CSS file.

@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';
}
Shy
  • 542
  • 8
  • 20
  • 1
    Have you read this [instruction](http://google.github.io/material-design-icons/)? – Vladislav Kievski Oct 03 '17 at 08:32
  • @VladislavKievski Yes, I have followed the 'Setup Method 2. Self hosting' section. You can download my example little project from [here](http://www.mediafire.com/file/0dsd97tf3g8al85/MaterialFontsOffline.zip). I have also copied the code snippets above. – Shy Oct 03 '17 at 08:41
  • try to prepend file like this: `url("./MaterialIcons-Regular.eot")` and all other locations.. and see if it loads ok,, also try to add quotes – Kresimir Pendic Oct 03 '17 at 09:47
  • @KresimirPendic But `./` is prepended to `exe`s. Isn't it? – Shy Oct 03 '17 at 09:51
  • 1
    try to add mime type fix,, I added answer lower – Kresimir Pendic Oct 03 '17 at 09:53
  • @KresimirPendic OK – Shy Oct 03 '17 at 10:05
  • For someone using Angular, this post might be helpful https://thecodeframework.com/host-google-material-icon-fonts-locally-in-angular-in-5-simple-steps/ – Gagan Jul 21 '20 at 03:12

3 Answers3

5

First way to add icons will be following:

  1. Copy content of this file to you custom css file (for instance, material-fonts.css) https://fonts.googleapis.com/icon?family=Material+Icons

/* fallback */
@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/materialicons/v29/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;
  -webkit-font-feature-settings: 'liga';
  -webkit-font-smoothing: antialiased;
}
  1. Copy the link from src url and put it in browser. It will download file. You need to put it file in the same directory where you save first file(material-fonts.css).
  2. Update in material-fonts.css file src: url() as src: url(2fcrYFNaTjcS6g4U3t-Y5ZjZjT5FdEJ140U2DJYC3mY.woff2) format('woff2');

Second example: Looks like you download wrong files. You need to download this directory and put it into your folder, previous files have to removed.

Your materialFontTest.php should look like:

<!DOCTYPE html>

<html>

<head>
<title>MaterializeTest</title>

<link rel="stylesheet" href="material-icons.css" />

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>

<script src="jquery.min.js"></script>
<script src="materialize.min.js"></script>
</head>

<body>
<a href="#!"><i class="material-icons">chevron_left</i></a>
</body>

</html>
Vladislav Kievski
  • 1,637
  • 11
  • 12
4

Original but light weight Material Icons

This light-weight repository (https://www.npmjs.com/package/material-design-icons-iconfont) is a fork of the original repository of ~60MB but this is very much light weight because unnecessary files were removed from that original repository.

How to install?

step 1:

// install via bower/npm
bower install material-design-icons-iconfont

npm install material-design-icons-iconfont

step 2:

// import or link
@import "~material-design-icons-iconfont/dist/material-design-icons.css";

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

How to use?

<i class='material-icons'>done</i> // remember to add class `material-icons`

more on https://material.io/tools/icons/?style=baseline

WasiF
  • 26,101
  • 16
  • 120
  • 128
2

I think you downloaded wrong files from somewhere. This is the path that you have to download correct fonts from: https://github.com/google/material-design-icons/tree/master/iconfont or use the one in my GH repo that worked for you already.

Also you need to call only one css in your html (php) file, like this:

<link rel="stylesheet" href="material-fonts.css" />

hth, k

Kresimir Pendic
  • 3,597
  • 1
  • 21
  • 28
  • By global file, do you mean `/etc/apache2/sites-enabled/000-default.conf` file? – Shy Oct 03 '17 at 10:37
  • @Sie that depends on platform you are in,, I can see that for ubuntu it's `/etc/apache2/apache2.conf` but I'm not sure, I'm on nginx – Kresimir Pendic Oct 03 '17 at 10:53
  • I am using Ubuntu 16.04 and Apache2, and I added those lines at the end of `/etc/apache2/apache2.conf` and restarted apache. But I still have the same problem. Material Icons are not rendering. – Shy Oct 03 '17 at 11:13
  • 1
    can you share your url (if it's not private or localhost one) so I can see and try to investigate your case? – Kresimir Pendic Oct 03 '17 at 14:54
  • Hey it's localhost, but let me try to deploy it in cloudnine servers. Then I'll give you the URL. Hey but I will do it in one and a half hour, as I am late for class. – Shy Oct 04 '17 at 04:24
  • Hi. Firstly, sorry for late. Secondly, [here I uploaded this project on Github](https://github.com/JessieSie/MaterialFontsOffline). Will you be able to take a look or try to fix my problem? – Shy Oct 05 '17 at 08:01
  • 1
    hi @Sie you can test my repo pull that I sent you,, this is public html file that works you can see it here: https://rawgit.com/mkteam/MaterialFontsOffline/f0e2eac5bf1b5c4d8e5e92f3a73a5c9970593dfb/index.html – Kresimir Pendic Oct 05 '17 at 12:46
  • Hi, this works. Thank you so very much indeed. After some testing, I have concluded that it is the actual font files you have replaced mine with, have made the difference. So can you tell me where did you get those font files? – Shy Oct 05 '17 at 23:22
  • Secondly can you edit the answer accordingly, so that I can mark this answer as the accepted answer to this question. Thank you – Shy Oct 05 '17 at 23:22