I'm trying to load a self hosted font in a react project. I'm running it under development mode (npm run start) and not in production. I've followed the instructions on the official site of Material UI but had no success. My fonts are stored in path/to/react-project/src/fonts folder. This is an ecxerpt of my code:
import { ThemeProvider, createTheme } from '#mui/material/styles';
import ProximaNovaReg from '../fonts/Proxima-Nova-Reg.ttf';
import ProximaNovaIt from '../fonts/Proxima-Nova-It.otf';
import ProximaNovaBold from '../fonts/Proxima-Nova-Bold.otf';
import FuturaBold from '../fonts/Futura-Bold.ttf';
const theme = createTheme({
typography: {
fontFamily: [
'Proxima-Nova',
'sans-serif',
].join(','),
},
components: {
MuiCssBaseline: {
styleOverrides: `
#font-face {
font-family: 'Proxima-Nova';
font-style: normal;
font-display: swap;
font-weight: 400;
src: url(${ProximaNovaReg}) format('truetype')
}
#font-face {
font-family: 'Proxima-Nova';
font-style: italic;
font-display: swap;
font-weight: 400;
src: url(${ProximaNovaIt}) format('otf')
}
#font-face {
font-family: 'Proxima-Nova';
font-style: normal;
font-display: swap;
font-weight: 700;
src: url(${ProximaNovaBold}) format('otf')
}
#font-face {
fontFamily: 'Futura';
fontStyle: 'normal';
fontDisplay: 'swap';
fontWeight: 700;
src: url(${FuturaBold}) format('truetype')
}
`,
},
},
});
// ...
return (
<ThemeProvider theme={theme}>
<CssBaseline />
<Box
sx={{
fontFamily: 'Proxima-Nova',
}}
>
Proxima-Nova
</Box>
</ThemeProvider>
);
package.json: "#mui/material": "^5.10.1"
What am I doing wrong?
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.
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" ...
I am actually blocked by an issue with the font import in a React project (working with styled-components).
This is my Fonts file:
import { createGlobalStyle } from 'styled-components';
import NameOfYourFontWoff from './ClickerScript.woff';
import NameOfYourFontWoff2 from './ClickerScript.woff2';
export default createGlobalStyle`
#font-face {
font-family: 'Font Name';
src: local('Font Name'), local('FontName'),
url(${NameOfYourFontWoff2}) format('woff2'),
url(${NameOfYourFontWoff}) format('woff');
font-weight: 300;
font-style: normal;
}
`;
and this is my index.js where I import my font and styles :
import GlobalStyles from './styles/globalStyles';
import GlobalFonts from './fonts/Fonts';
ReactDom.render((
<React.Fragment>
<ThemeProvider theme={theme}>
<GlobalFonts />
<GlobalStyles />
<Provider store={store}>
<Main />
</Provider>
</ThemeProvider>
</React.Fragment>
), document.getElementById('tetris'));
With this, I can see the font is given to my div, but I have no change, and my font does not appear in the Network flow. Any idea ?
I don't know these fonts, but if they are any different, add them in two times
in this example i took it like one of them was italic
#font-face {
font-family: 'ClickerScript';
src: url(${NameOfYourFontWoff}) format('woff'),
font-weight: 300;
font-style: normal;
}
#font-face {
font-family: 'ClickerScript';
src: url(${NameOfYourFontWoff2}) format('woff2'),
font-weight: normal;
font-style: italic;
}
Let me know if it works and for clarification, you don't see any of these font loaded (even failed) in the request tab ?
Hello I would like to use different font weights of Inter font (400, 500, 700). Right now it only loads Inter regular. I'm using create-react-app with typescript and styled-components.
globalStyles:
export const GlobalStyle = createGlobalStyle`
#font-face {
font-family: 'Inter';
src: url(${Inter}) format("truetype"),
url(${InterWoff}) format("woff"),
url(${InterWoff2}) format("woff2");
font-weight: normal;
font-style: normal;
};
#font-face {
font-family: 'Inter';
src: url(${InterMedium}) format('truetype')
url(${InterMediumWoff}) format('woff'),
url(${InterMediumWoff2}) format('woff2');
font-weight: 500;
font-style: normal;
};
#font-face {
font-family: 'Inter';
src: url(${InterBold}) format('truetype')
url(${InterBoldWoff}) format('woff'),
url(${InterBoldWoff2}) format('woff2');
font-weight: bold;
font-style: normal;
}
body {
margin: 0;
font-family: 'Inter', -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;
}
This is how I was able to do it
import { createGlobalStyle } from 'styled-components'
import { GalanoGrotesqueMedium, GalanoGrotesqueRegular } from 'assets/fonts'
const GlobalStyles = createGlobalStyle`
#font-face {
font-family: 'GalanoGrotesqueRegular';
src: url(${GalanoGrotesqueRegular}) format('truetype');
}
#font-face {
font-family: 'GalanoGrotesqueMedium';
src: url(${GalanoGrotesqueMedium}) format('truetype');
}
* {
font-family: 'GalanoGrotesqueRegular';
}
`
export default GlobalStyles
where I am importing .ttf fonts
This is my assets/fonts
export {default as GalanoGrotesqueMedium} from './fonts/GalanoGrotesqueMedium.ttf'
export {default as GalanoGrotesqueRegular} from './fonts/GalanoGrotesqueRegular.ttf'
Based on your code, I would try naming the different fonts and then adding them to the font-families, as such:
export const GlobalStyle = createGlobalStyle`
#font-face {
font-family: 'Inter-Regular';
src: url(${Inter}) format("truetype"),
url(${InterWoff}) format("woff"),
url(${InterWoff2}) format("woff2");
font-weight: normal;
font-style: normal;
};
#font-face {
font-family: 'Inter-500';
src: url(${InterMedium}) format('truetype')
url(${InterMediumWoff}) format('woff'),
url(${InterMediumWoff2}) format('woff2');
font-weight: 500;
font-style: normal;
};
#font-face {
font-family: 'Inter-Bold';
src: url(${InterBold}) format('truetype')
url(${InterBoldWoff}) format('woff'),
url(${InterBoldWoff2}) format('woff2');
font-weight: bold;
font-style: normal;
}
body {
margin: 0;
font-family: 'Inter-Regular', 'Inter-500', 'Inter-Bold', -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;
}
This is how I do it:
fontsCSS.ts
import { css } from "styled-components";
export const fontsCSS = css`
#font-face {
font-family: "Inter";
src: url('/fonts/Inter-Regular.ttf');
font-weight: normal;
font-style: normal;
font-display: block;
}
#font-face {
font-family: "Inter";
src: url('/fonts/Inter-Semibold.ttf');
font-weight: bold;
font-style: normal;
font-display: block;
}
`;
GlobalStyles.ts
import { createGlobalStyle } from "styled-components";
import { resetCSS } from "#styles/resetCSS";
import { baseCSS } from "#styles/baseCSS";
import { fontsCSS } from "#styles/fontsCSS";
const GlobalStyles = createGlobalStyle`
${resetCSS}
${baseCSS}
${fontsCSS}
`;
export default GlobalStyles;
App.tsx
import React from "react";
import { theme } from "#styles/theme";
import { ThemeProvider } from "styled-components";
import GlobalStyles from "#styles/GlobalStyles";
interface App {}
const App: React.FC<App> = (props) => {
console.log("Rendering App...");
return(
<ThemeProvider theme={theme}>
<GlobalStyles/>
<RestOfTheApp/>
</ThemeProvider>
);
};
export default React.memo(App);
Use exact font-weights in font-family declarations instead of normal, bold.
When using font-family, declare font-weight as well. Browser will pick the correct font file based on the combination of font-family and font-weight.