How to prevent material icon text from showing up when Google's JS fails to convert them?

假装没事ソ 提交于 2021-01-20 16:21:26

问题


How do you prevent material icon text from showing up when Google's JS fails to convert them to icons?

Icons are defined in markup as such:

<span class="material-icons">icon_name</span>

Example: https://archive.fo/CKqKG/scr.png (see the top row of buttons).

Material Icons Documentation: https://material.io/icons/

This is also an issue in Google search where Google will actually read and save the div's text instead of ignoring it.

Example: https://i.imgur.com/TixS06y.png

I understand that one solution is to simply switch to .PNGs (supplied by Google). I'd like to do whatever results in less (network) load on the user's system.

Thanks!


回答1:


you can use font-display: block;, just add this CSS to your HTML head:

<style>
   @font-face {
      font-family: 'Material Icons';
      font-display: block;
    }
</style>

for more information font-display




回答2:


I've been struggling with a similar situation: my problem was not that the icons never loaded, just that they could take a while to load on slower connections and until they loaded ugly, unformatted text like sentiment_very_satisfied would be shown on the page (often many times larger than the surrounding text as well making it very obvious).

The other solutions here didn't work for me (including font-display:block which I thought might be promising), so I came up with my own using CSS and jQuery. I'm sure you could easily adapt it to use vanilla JS.

CSS:

.material-icons{
    opacity:0;
}

jQuery:

$(window).load(function() {
    $('.material-icons').css('opacity','1');
});

The trick here is that, unlike the more commonly used $(document).ready() listener, $(window).load() waits for all elements of a page to be downloaded before being triggered. In this case, that means it won't change the opacity of the icons until the icon font has been downloaded.

The downside is that the icons won't show up until everything on the page has been downloaded, but that was a trade-off I was willing to make to avoid having huge spans of text visible on my page before the icon font loaded.

(I also added a transition to the CSS .material-icons{transition:opacity 0.5s;} so they showed up nice and smooth.)




回答3:


If you are using Typekit's webfont loader, you can apply conditional classes to hide the icons while the web font is loading or if it failed to load, e.g.:

.wf-loading, .wf-materialicons-n4-inactive {
  .material-icons {
    display: none;
  }
}

You can of course apply other styling techniques according to your preferences for best results, e.g. font-size: 0;, it will depend on your site and use case.

To load the material icons with the webfont loader, use configuration like so:

window.WebFontConfig = {
  google: {
    families: [
      'Material Icons',
    ],
  },
};



回答4:


The right solution to this will be to add a max width of the same font-size and set overflow to hidden.

.material-icons {
    max-width: 16px;
    overflow: hidden;
}



回答5:


I am facing this same issue. I believe, though, that using a pseudo selector like i.material-icons:before can help. See this for more info.

---- EDIT : Working Example

i.material-icons:before{display:none;}




回答6:


In case if your are using angularjs, fix might be

<i class="material-icons-outlined" ng-bind-html=" 'icon_text' "></i>

or if you are using jquery, then

HTML

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

jQuery.onLoad

$('[material-icons]').each(function(){
  var icon_text = $(this).attr('material-icons');
  $(this).html(icon_text)
});

Icon appears after the page has loaded.




回答7:


Easy solution:

Take advantage of class/css priority to 'hide' the icon while material icons fonts and css have not been yet loaded.

For that, create a class that has the style display: none an put it before the .material-icons ones. When the fonts have been loaded, .material-icons class display property will override the display: none ones, causing the icon to be shown. And that's it.

Example:

.display-none {
    display: none;
}

// Important: .display-none class has to go before .material-icons ones!!!
<i class="display-none material-icons">settings</i>



回答8:


Not a fix but on browsers that support preconnect you can try and load the fonts as soon as possible. Should help reduce the amount of time between text and icon being shown on slow connections.

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



回答9:


Create a div style = "position: absolute; top: -1000px" on the homepage and enter all items with class material-icon or awesome font as follows:

<div   style="position:absolute;top:-1000px" >      
<i class="icon material-icons-outlined"  >add_circle</i>
<i class="icon material-icons "  >list_alt</i>
<span class="fas fa-circle fa-stack-2x " ></span>
<span class="fas fa-home fa-stack-1x fa-inverse"  ></span>
</div>



回答10:


instead of using material icons full text you can use their corresponding hex codepoints. of course this is not hiding but if the font is not loaded it just shows the unknown char symbol.

example:

<i class="material-icons">&#xE87D;</i>

you can find the list of codepoints at:

https://github.com/google/material-design-icons/issues/813#issuecomment-401601344

or

https://raw.githubusercontent.com/flutter/flutter/master/packages/flutter/lib/src/material/icons.dart



来源:https://stackoverflow.com/questions/41710834/how-to-prevent-material-icon-text-from-showing-up-when-googles-js-fails-to-conv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!