Why are we forced to use fonts bundled with material-ui?
Is there a way to remove or configure this?
Actually, after 6 hours of searching online, digging in lib code and randomly guessing, the conclusion I arrived at: IT IS POSSIBLE! :D
/public/fonts/fonts.css
, that defines the @font-faces:@font-face {
font-family: 'inglobal';
font-weight: normal;
font-style: normal;
src: url('./inglobal.ttf');
}
add <link rel="stylesheet" href="%PUBLIC_URL%/fonts/fonts.css">
to /public/index.html's head
Hurray!
4/b. If, for any reason, it still doesn't work, change the fonts' extensions to .css (also at src: url('./OperatorMono.css')
)
link google fonts in your index.html
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,500,700&display=swap" rel="stylesheet">
Add in your body style
/* Styles */
body {
margin: 0 auto;
font-family: 'Montserrat';
}
few changes in app.js
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
const theme = createMuiTheme({
typography: {
fontFamily: [
'Montserrat'
].join(','),
},
})
/public/index.html
worked for me :)
Actually, after 6 hours of searching online, digging in lib code and randomly guessing, the conclusion I arrived at: IT IS POSSIBLE! :D
- create /fonts directory in /public
- create
/public/fonts/fonts.css
, that defines the @font-faces:@font-face { font-family: 'inglobal'; font-weight: normal; font-style: normal; src: url('./inglobal.ttf'); }
- add
<link rel="stylesheet" href="%PUBLIC_URL%/fonts/fonts.css">
to /public/index.html's head- Hurray!
4/b. If, for any reason, it still doesn't work, change the fonts' extensions to .css (also at
src: url('./OperatorMono.css')
)
It's work !! Thank you.
Follow these 2 steps to change default font:
Here I'm changing default font to "Open Sans"
Import Google Fonts in your index.html
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;700&display=swap" rel="stylesheet"/>
Do these changes in index.js
import { ThemeProvider } from '@material-ui/styles';
import { CssBaseline } from '@material-ui/core';
import { createMuiTheme } from '@material-ui/core';
const theme = createMuiTheme({ typography: { fontFamily: [ 'Open Sans' ].join(','), }, })
ReactDOM.render(
<ThemeProvider theme={theme}>
<CssBaseline />
<App />
</ThemeProvider>,
document.getElementById('root')
);
_Sometimes sequence matters during the configuration. As a good practice always define typography at the top level during the configuration on createMuiTheme_
After the configuration theme is not getting font-family
// Create a theme instance.
const theme = createMuiTheme({
palette: {
primary: {
main: "#2991d6",
},
secondary: {
main: "#19857b",
},
error: {
main: red.A400,
},
background: {
default: "#fff",
},
}, typography: {
fontFamily: "VarelaRound,sans-serif",
},
overrides: {
MuiCssBaseline: {
"@global": {
"@font-face": [varelaRound],
},
},
},
});
Note:
In this case Material UI using Roboto as the default font
// Create a theme instance.
const theme = createMuiTheme({
typography: {
fontFamily: "VarelaRound,sans-serif",
},
palette: {
primary: {
main: "#2991d6",
},
secondary: {
main: "#19857b",
},
error: {
main: red.A400,
},
background: {
default: "#fff",
},
},
overrides: {
MuiCssBaseline: {
"@global": {
"@font-face": [varelaRound],
},
},
},
});
I just change the sequence and everything working fine.
Most helpful comment
Actually, after 6 hours of searching online, digging in lib code and randomly guessing, the conclusion I arrived at: IT IS POSSIBLE! :D
/public/fonts/fonts.css
, that defines the @font-faces:add
<link rel="stylesheet" href="%PUBLIC_URL%/fonts/fonts.css">
to /public/index.html's headHurray!
4/b. If, for any reason, it still doesn't work, change the fonts' extensions to .css (also at
src: url('./OperatorMono.css')
)