Material Icons not working via React-materialize - reactjs

I have a site using Middleman, Webpack and Materialize, we have a React component in one of the pages that works ok apart from the fact that Material Icons are not rendered on Internet Explorer and Firefox; all icons in the rest of the site work fine across all browsers.
Any help would be appreciated.
Packages used
materialize-css 0.100.2 => I tried 0.97, 0.98 with the same result
webpack 3.10.0
react 16.2.0 => I tried with 15.xx versions and got the same result
react-materialize 1.1.1
In my layout I'm importing the icons according to the Docs
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="preload" as="style" onload="this.rel='stylesheet'">
what is being displayed at the moment is the icon name like alarm_off or  as I've tried both input methods

After reading lots of blogs and issues on GitHub, I managed to solved the problem thanks to this Google issue, so I added this in my style.scss
#font-face {
font-family: "Material Icons";
font-style: normal;
font-weight: 400;
src: local("Material Icons"),
local("MaterialIcons-Regular"),
url(https://rawgit.com/google/material-design-icons/master/iconfont/MaterialIcons-Regular.woff2) format("woff2"),
url(https://rawgit.com/google/material-design-icons/master/iconfont/MaterialIcons-Regular.woff) format("woff"),
url(https://rawgit.com/google/material-design-icons/master/iconfont/MaterialIcons-Regular.ttf) format("truetype");
}
.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;
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";
}
Also, I updated this in my nginx
location ~* \.(?:eot|otf|ttc|ttf|woff|woff2)$ {
expires 1M;
add_header Access-Control-Allow-Origin "*";
add_header Cache-Control "public";
Hope this help somebody else with similar issues.

Related

create-react-app based projects added font but not working

I'm using create-react-app and prefer not to eject.
i imported in index.html file . and give it default font. but its not working.
<style>
#font-face {
font-family: Gumela;
src: url("../src/fonts/*");
}
* {
font-family: "Gumela";
font-weight: normal;
font-size: 20px;
}
body {
margin: 0;
}
</style>
Try importing the fonts in your app.css or index.css file.
And try:
#font-face {
font-family: Gumela;
src: url("../src/fonts/*");
font-display: swap; // This will swap font once its loaded, for SEO purpose.
}
* {
font-family: Gumela;
font-weight: normal;
font-size: 20px;
OR
font: Gumela normal 20px;
}
I imported the font here: https://www.onlinewebfonts.com/download/00561b492d88a5287a5def1e83a34882 and it worked for me.
just add this in head before your style tag.
<link href="//db.onlinewebfonts.com/c/00561b492d88a5287a5def1e83a34882?family=Gumela" rel="stylesheet" type="text/css"/>
You can insert font-face on your App.css file
#font-face {
font-family: Gumela;
src: url('./fonts/Gumela\ Regular.otf');
}

Unable to override bootstrap styles

I have tried following the instructions listed on Bootstrap's website, as well as various StackOverflow questions, and am still unable to override a single style element of Bootstrap. I'd appreciate help, I'm not particularly knowledgeable of web dev.
Here is my setup.
I used create-react-app to generate a file structure. I have an index.js where I immediately import './index.scss';
Here is my index.scss
#import "../node_modules/bootstrap/scss/functions";
#import "../node_modules/bootstrap/scss/variables";
#import "../node_modules/bootstrap/scss/mixins";
body {
margin: 0;
font-family: Catamaran, -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale; }
code {
font-family: Catamaran source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace; }
/* playfair-display-regular - latin */
#font-face {
font-family: 'Playfair Display';
font-style: normal;
font-weight: 400;
src: url("./fonts/playfair-display-v25-latin-regular.eot");
/* IE9 Compat Modes */
src: local(""), url("./fonts/playfair-display-v25-latin-regular.eot?#iefix") format("embedded-opentype"), url("./fonts/playfair-display-v25-latin-regular.woff2") format("woff2"), url("./fonts/playfair-display-v25-latin-regular.woff") format("woff"), url("./fonts/playfair-display-v25-latin-regular.ttf") format("truetype"), url("./fonts/playfair-display-v25-latin-regular.svg#PlayfairDisplay") format("svg");
/* Legacy iOS */ }
/* catamaran-regular - latin */
#font-face {
font-family: 'Catamaran';
font-style: normal;
font-weight: 400;
src: url("./fonts/catamaran-v8-latin-regular.eot");
/* IE9 Compat Modes */
src: local(""), url("./fonts/catamaran-v8-latin-regular.eot?#iefix") format("embedded-opentype"), url("./fonts/catamaran-v8-latin-regular.woff2") format("woff2"), url("./fonts/catamaran-v8-latin-regular.woff") format("woff"), url("./fonts/catamaran-v8-latin-regular.ttf") format("truetype"), url("./fonts/catamaran-v8-latin-regular.svg#Catamaran") format("svg");
/* Legacy iOS */ }
$container-max-widths: (
sm: 540px,
md: 720px,
lg: 960px,
xl: 1020px,
xxl: 1080px
);
$blue: #306779;
$primary: #306779;
#import "../node_modules/bootstrap/scss/bootstrap.scss";
The fonts are loading correctly, but the bootstrap variables are not. I have also attempted to manually transpile a scss file into a css file and import that into the <head> using <link rel="stylesheet" href="../scss/custom.css" />, however that also does not work and I still see the normal bootstrap color scheme.

Google fonts not applied to deployed react app on Github Pages

I'm using styled-components for my app, and I imported a google font url in createGlobalStyle. :
import { createGlobalStyle } from "styled-components";
const GlobalStyles = createGlobalStyle`
* {
#import url(//fonts.googleapis.com/css2?family=Space+Mono:wght#400;700&display=swap);
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Space Mono', monospace;
font-weight: 400;
}
After deploying the app, the font is not applied. I tried replacing the #import url from https://fonts.. to http://fonts.. and then currently //fonts... These are some solutions I found that have worked for some, but not for me. What seems to be the problem here?
here is the link of the deployed app: http://patorikkun.github.io/tip-calculator-app
Try to move the #import to top level, outside * {}
From:
createGlobalStyle`
* {
#import url("https://fonts.googleapis.com/css2?family=Space+Mono:wght#400;700&display=swap");
margin: 0;
...
}
...
}`;
To:
createGlobalStyle`
#import url("https://fonts.googleapis.com/css2?family=Space+Mono:wght#400;700&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Space Mono', monospace;
font-weight: 400;
}
...
}`;
From the MDN explanation:
Imported rules must precede all other types of rules, except #charset rules;

Font is loaded but not applicated

I use the Roboto font in my React project and I have a strange problem, when I access my application locally the font is not applied to my texts, however if I open the element inspector and I uncheck and recheck a css property (no matter which one) the font is applied everywhere!
I use scss files with node-sass and my font is local and defined using #font-face
Here is the .scss file for the font
#font-face {
font-family: Roboto;
src: url("./Roboto/Roboto-Regular.ttf") format("truetype");
font-weight: 400;
font-style: normal;
font-display: optional;
}
#font-face {
font-family: Roboto;
src: url("./Roboto/Roboto-Italic.ttf") format("truetype");
font-weight: 400;
font-style: italic;
font-display: optional;
}
#font-face {
font-family: Roboto;
src: url("./Roboto/Roboto-Medium.ttf") format("truetype");
font-weight: 500;
font-style: normal;
font-display: optional;
}
#font-face {
font-family: Roboto;
src: url("./Roboto/Roboto-MediumItalic.ttf") format("truetype");
font-weight: 500;
font-style: italic;
font-display: optional;
}
#font-face {
font-family: Roboto;
src: url("./Roboto/Roboto-Bold.ttf") format("truetype");
font-weight: 700;
font-style: normal;
font-display: optional;
}
#font-face {
font-family: Roboto;
src: url("./Roboto/Roboto-BoldItalic.ttf") format("truetype");
font-weight: 700;
font-style: italic;
font-display: optional;
}
I import all my .scss file into a main.scss file and I import this file to my App.js. Everything related to css is working fine except the font :(
Has anyone experienced a similar problem? I also use a font for icons and it works perfectly
Thanks in advance !
The solution is to not use "font-display: optional" ...

How to insert a Local Font in my Project?

I used IconMoon to create custom icon fonts.
In the .html that is generated by it, it works perfectly. But when I try to use it in my project with React and Styled-Components, instead of being displayed the icon displays its content in HTML.
My fonts are in the path: src/assets/fonts/icons
Fonts are being downloaded by the browser and no errors are displayed:
Soon I am trying to create a component for my fonts, similar to FontAwesome and this happens:
MY COMPONENT:
import React from "react";
import styled from "styled-components";
const Tst = styled.i`
font-family: "spotify" !important;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
::before {
content: "\e900";
color: red;
}
`;
const SaveFavorite = () => {
return <Tst />;
};
export default SaveFavorite;
MY GLOBAL STYLES:
import { createGlobalStyle } from "styled-components";
const GlobalStyle = createGlobalStyle`
#font-face {
font-family: "spotify";
font-weight: normal;
font-style: normal;
src: url("../assets/fonts/spotify.eot?") format("embedded-opentype"),
url("../assets/fonts/spotify.ttf?") format("truetype"),
url("../assets/fonts/spotify.woff?") format("woff"),
url("../assets/fonts/spotify.svg?") format("svg");
}
* {
box-sizing: border-box;
padding: 0;
margin: 0;
outline: 0;
}
body {
font-family: 'CircularSpUIv3T', sans-serif;
color: white;
text-rendering: optimizeLegibility !important;
-webkit-font-smoothing: antialiased !important;
-moz-osx-font-smoothing: grayscale !important;
user-select: none;
}
img {
width: 100%;
max-width: 100%;
height: auto;
}
`;
export default GlobalStyle;
If you look at the css generated by Styled Components, you'll find that the font paths aren't resolved at all, they are exactly the way you typed them. The browser won't be able to find relative paths in the src directory without some help from Babel, and the way you have it now these paths would just be passed as-is.
You have two options, although I'm not sure if both will work.
Option 1: move your fonts to the public directory
Files in the public directory can be found by the browser just like any old HTML page. In your global style, you would then have to use a path that is relative to public/index.html.
Your file structure would be something like:
project
|-- public
|-- index.html
|-- fonts
|-- spotify.eot
|-- spotify.ttf
|-- spotify.woff
|-- spotify.svg
|-- src
|-- global.style.js
|-- (...)
And your global.style.js:
const GlobalStyle = createGlobalStyle`
#font-face {
font-family: "spotify";
src: url("fonts/spotify.eot?") format("embedded-opentype"),
url("fonts/spotify.ttf?") format("truetype"),
url("fonts/spotify.woff?") format("woff"),
url("fonts/spotify.svg?") format("svg");
}
Option 2: keep your fonts in src, but make Babel resolve the paths for you
To make Babel resolve the paths, we need to import the files in our global.style.js, and then use the resolved path instead of the static one:
import SpotifyEOT from '../assets/fonts/spotify.eot';
// etc
const GlobalStyle = createGlobalStyle`
#font-face {
font-family: "spotify";
src: url(${SpotifyEOT}?) format("embedded-opentype"),
// [etc]
}
I don't know what the question mark is actually for but I assume you can just place it after the resolved path like I did.

Resources