Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 570
David Avatar asked Jan 18 '17 03:01

David


People also ask

Are Google material icons free for commercial use?

Licensing. We have made these icons available for you to incorporate them into your products under the Apache License Version 2.0. Feel free to remix and re-share these icons and documentation in your products. We'd love attribution in your app's about screen, but it's not required.

How do I embed a Google material icon?

To embed Google Icons in your HTML web pages you have to add the link of material-icons font library in the <head> section of your HTML file and then add the class material-icons in the <i> or <span> tag of the <body> section along with the name of the icon.

How do I change the size of the material icon?

Try setting "height" , "width", "line-height" attributes for that particular icon. This might help. Save this answer.


2 Answers

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

like image 126
Fareed Alnamrouti Avatar answered Oct 03 '22 02:10

Fareed Alnamrouti


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.)

like image 24
todd Avatar answered Oct 03 '22 01:10

todd