How to host material icons offline?

后端 未结 19 2411
无人及你
无人及你 2020-12-02 06:55

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



        
相关标签:
19条回答
  • 2020-12-02 07:37

    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.

    0 讨论(0)
  • 2020-12-02 07:42

    Method 2. Self hosting Developer Guide

    Download the latest release 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.


    0 讨论(0)
  • 2020-12-02 07:42

    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>
    
    0 讨论(0)
  • 2020-12-02 07:43

    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.

    0 讨论(0)
  • 2020-12-02 07:43

    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>
    
    0 讨论(0)
  • 2020-12-02 07:44

    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';
    
    0 讨论(0)
提交回复
热议问题