I have my mui theme defined like this:
export default createMuiTheme({
typography: {
fontFamily: '"Nunito Sans", "Helvetica", "Arial", sans-serif',
fontWeightLight: 300,
fontWeightMedium: 600,
fontWeightRegular: 400
}
}
});
I have the fonts being loaded in using App.css from local files. I can see from the network requests that these files are being found.
/* latin */
#font-face {
font-family: 'Nunito Sans';
font-style: normal;
font-weight: 300;
src: local('Nunito Sans Light'), local('NunitoSans-Light'),
url(../assets/font/pe03MImSLYBIv1o4X1M8cc8WAc5tU1E.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
/* latin */
#font-face {
font-family: 'Nunito Sans';
font-style: normal;
font-weight: 400;
src: local('Nunito Sans Regular'), local('NunitoSans-Regular'), url(../assets/font/pe0qMImSLYBIv1o4X1M8cce9I9s.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
/* latin */
#font-face {
font-family: 'Nunito Sans';
font-style: normal;
font-weight: 600;
src: local('Nunito Sans SemiBold'), local('NunitoSans-SemiBold'),
url(../assets/font/pe03MImSLYBIv1o4X1M8cc9iB85tU1E.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
The issue is that the UI is falling back to "Helvetica". I can see no reason why "Nunito Sans" is not being used by MUI. Annoyingly this setup was working fine before and is now failing. Anyone any ideas why this might not work? Thanks!
Here’s a working example: https://codesandbox.io/s/mzlmxpw4r8?fontsize=14
I think there are two possible problems you could be having.
Configuring the Material UI theme
// Import the wrapper component, and the the creator function
import { MuiThemeProvider, createMuiTheme } from "#material-ui/core/styles";
// Create a new theme using Nunito Sans
const theme = createMuiTheme({
typography: {
fontFamily: "Nunito Sans, Roboto, sans-serif"
}
});
const App = function(props) {
// Pass the theme as a prop to the theme provider
return (
<MuiThemeProvider theme={theme}>
<Demo />
</MuiThemeProvider>
);
};
For the sake of this demo, I thought it would be easier to use the hosted version of Nunito Sans on Google Fonts, so my index.html file also has the following:
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,500"
/>
Changing how you load the fonts
You mentioned you’re seeing the requests for the files coming through, so it’s probably not this, but I thought I would note it anyway.
From the example CSS you provide, it looks to me like you’ve copied the CSS declaration from Google Fonts itself. When I visit now for Nunito Sans, I get the exact same result: https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,50
However, this is different depending on the browser requesting the CSS. For example, if you are on an browser that supports WOFF but not WOFF2, you’ll be served the WOFF files.
Google Fonts has its own reasons and logic to handle this automatically, so it’s worth considering if it would be a good option for you. Otherwise, if you do want to host the fonts locally, downloading them directly from the Google Fonts API will be less reliable than getting them from another source with all the formats you might want (WOFF and WOFF2 is almost always sufficient at this point, and you can support an extra ~9% of browsers in use by adding the WOFF formats, without much extra effort).
For example, after download the WOFF as well:
#font-face {
font-family: 'Nunito Sans';
font-style: normal;
font-weight: 400;
src:
url('../assets/font/NunitoSans-400.woff2') format('woff2'),
url('../assets/font/NunitoSans-400.woff') format('woff');
}
Or, because Material UI works with the typefaces- packages on npm, you could install the formats and CSS you need that way:
npm install typeface-nunito-sans
Then at the top of your entry JavaScript file:
import "typeface-nunito-sans";
// Or require('typeface-nunito-sans') if you aren’t using import
Still, my first suggestion would be to start with the live Google Fonts version of Nunito Sans, and change your approach from there as necessary:
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,500" />
Hope that helps!
Related
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');
}
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.
Followed the documentation and added a _document.js file with the provided code:
import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head>
<link href="https://fonts.googleapis.com/css2?family=Almarai:wght#300;400;700&display=swap" rel="stylesheet" />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
When I use my chrome extension font checker and verify in the inspector, it states that it using the defaults fonts. Any idea how I can get this to work? Might there be some interference with the fact that I'm using tailwindcss?
in globals.css
/* Google Font */
#import url('https://fonts.googleapis.com/css2?family=Kurale&display=swap');
/* From Public Directory */
#font-face {
font-family: 'Kurale';
src: url('/fonts/Kurale.ttf');
font-style: medium;
font-weight: normal;
font-display: swap;
}
Check this, use <style data-href= instead of
For the fonts with NExtJS and Vercel I recommend you to use fontsource
Import all fonts in the globals.css or in the stylesheet imported in _app.tsx
#import url("https://fonts.googleapis.com/css2?family=Montserrat:wght#100;200;300;400&display=swap");
#import url("https://fonts.googleapis.com/css2?family=Kdam+Thmor+Pro&display=swap");
#import url("https://fonts.googleapis.com/css2?family=Lobster&display=swap");
#import url("https://fonts.googleapis.com/css2?family=Poiret+One&display=swap");
All the fonts had to be imported separately. For me, importing them
all together didn't work in the production.
Instead of <link> try using #import in your globals.css (or any stylesheet which is imported to the _app.js):
/* globals.css */
#import url("https://fonts.googleapis.com/css2?family=Almarai:wght#300;400;700&display=swap");
None of the usual things worked for me.
I was trying to import the following in the global.css file:
#import url("https://fonts.googleapis.com/css2?family=DM+Sans:wght#100;200;300;400;500;700&display=swap");
and the font wasn't loading. I had to inject the actual font instead:
/* IMPORTANT: For some reason directly importing the font from fonts.googleapis.com/css2 is not working. Instead I had to inject its contents */
/* #import url("https://fonts.googleapis.com/css2?family=DM+Sans:wght#100;200;300;400;500;700&display=swap"); */
/* latin-ext */
#font-face {
font-family: 'DM Sans';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/dmsans/v11/rP2Hp2ywxg089UriCZ2IHTWEBlwu8Q.woff2) format('woff2');
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
#font-face {
font-family: 'DM Sans';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/dmsans/v11/rP2Hp2ywxg089UriCZOIHTWEBlw.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
/* latin-ext */
#font-face {
font-family: 'DM Sans';
font-style: normal;
font-weight: 500;
font-display: swap;
src: url(https://fonts.gstatic.com/s/dmsans/v11/rP2Cp2ywxg089UriAWCrCBamC3YU-CnE6Q.woff2) format('woff2');
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
#font-face {
font-family: 'DM Sans';
font-style: normal;
font-weight: 500;
font-display: swap;
src: url(https://fonts.gstatic.com/s/dmsans/v11/rP2Cp2ywxg089UriAWCrCBimC3YU-Ck.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
/* latin-ext */
#font-face {
font-family: 'DM Sans';
font-style: normal;
font-weight: 700;
font-display: swap;
src: url(https://fonts.gstatic.com/s/dmsans/v11/rP2Cp2ywxg089UriASitCBamC3YU-CnE6Q.woff2) format('woff2');
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
#font-face {
font-family: 'DM Sans';
font-style: normal;
font-weight: 700;
font-display: swap;
src: url(https://fonts.gstatic.com/s/dmsans/v11/rP2Cp2ywxg089UriASitCBimC3YU-Ck.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
I am trying to use a locally hosted font in a React project that utilizes Emotion, and its Global component. This method works great for web fonts, like Google Fonts, but when I downloaded that same font and tried to apply it as a local .ttf file using #font-face, I couldn't achieve the same result.
Here's the important file, App.js:
import React from "react";
import { Global, css } from "#emotion/core";
import styled from "#emotion/styled";
const GlobalStyles = css`
#import url("https://fonts.googleapis.com/css?family=Dancing+Script&display=swap");
#font-face {
font-family: "Local Font";
src: url("fonts/DancingScript-Regular.ttf");
}
* {
text-align: center;
}
`;
const FromGoogle = styled.h1`
font-family: "Dancing Script";
`;
const FromLocal = styled.h1`
font-family: "Local Font";
`;
function App() {
return (
<div className="App">
<Global styles={GlobalStyles} />
<FromGoogle>This text's font family is from Google.</FromGoogle>
<FromLocal>
This text's font family should be the same, except it comes from a local
font file, and it's not working.
</FromLocal>
</div>
);
}
export default App;
For some reason, the text in FromGoogle uses the Google font fine, while the text from FromLocal doesn't. My first thought was that it's an issue with the path, but if it is, I couldn't tell.
Here's the full project on GitHub. I used Create React App, and removed all the irrelevant boilerplate.
In my Next.js app I am using emotion with these versions:
"#emotion/react": "^11.1.1",
"#emotion/styled": "^11.0.0",
My global styles are:
export const GlobalStyles = () => (
<Global
styles={css`
#font-face {
font-family: 'Faustina';
font-style: normal;
font-weight: 700;
src: local(''),
url('/fonts/Faustina/Faustina.woff') format('woff'),
url('/fonts/Faustina/Faustina.ttf') format('truetype');
}
* {
box-sizing: border-box;
font-family: Faustina;
}
html,
body {
width: 100%;
height: 100%;
min-height: 100%;
padding: 0;
margin: 0;
}
`}
/>
);
My fonts are in
project_root/public/fonts/Faustina
// explicitly
project_root/public/fonts/Faustina/Faustina-Bold.ttf
project_root/public/fonts/Faustina/Faustina-Bold.woff
In order to see font changes, I needed to restart dev server, e.g. yarn dev. Before restarting I had same issue where fonts weren't displayed (even downloaded in dev tools Network tab).
am using these versions in my gatsby project;
"#emotion/react": "^11.8.1",
"gatsby": "^4.8.0"
index.html file is not available so you can't add <style> html tag and embed #font-face inside it.
Host your downloaded fonts inside your src/myfonts directory then import any of your fonts using the static import statement.
import font1 from './myfonts/font1.ttf';
then call url(font1) css function with the font1
const globalStyle = css`
#font-face {
font-family: 'font1';
src: url(${font1}) format('truetype');
};
`;
it worked in my case;
In my case, the solution was as in this answer.
I needed to define the fonts in a css file that is imported into the index.html like so:
<link rel="stylesheet" type="text/css" href="/styles/fonts.css" />
I was then able to reference the font family names in my emotion theme and have them load correctly.
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.