How to host material icons offline?

后端 未结 19 2410
无人及你
无人及你 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:44

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

    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.

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

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

    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.

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

    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"
                ],
    
    0 讨论(0)
  • 2020-12-02 07:57

    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.

    0 讨论(0)
提交回复
热议问题