Material-ui: Change/use custom font

Created on 7 Apr 2016  路  7Comments  路  Source: mui-org/material-ui

Why are we forced to use fonts bundled with material-ui?

Is there a way to remove or configure this?

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

  1. create /fonts directory in /public
  2. 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');
}
  1. add <link rel="stylesheet" href="%PUBLIC_URL%/fonts/fonts.css"> to /public/index.html's head

  2. Hurray!

4/b. If, for any reason, it still doesn't work, change the fonts' extensions to .css (also at src: url('./OperatorMono.css') )

All 7 comments

Actually, after 6 hours of searching online, digging in lib code and randomly guessing, the conclusion I arrived at: IT IS POSSIBLE! :D

  1. create /fonts directory in /public
  2. 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');
}
  1. add <link rel="stylesheet" href="%PUBLIC_URL%/fonts/fonts.css"> to /public/index.html's head

  2. Hurray!

4/b. If, for any reason, it still doesn't work, change the fonts' extensions to .css (also at src: url('./OperatorMono.css') )

  1. link google fonts in your index.html
    <link href="https://fonts.googleapis.com/css?family=Montserrat:400,500,700&display=swap" rel="stylesheet">

  2. Add in your body style
    /* Styles */ body { margin: 0 auto; font-family: 'Montserrat'; }

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

  1. create /fonts directory in /public
  2. 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');
}
  1. add <link rel="stylesheet" href="%PUBLIC_URL%/fonts/fonts.css"> to /public/index.html's head
  2. 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"

  1. Import Google Fonts in your index.html
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;700&display=swap" rel="stylesheet"/>

  2. 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_

Issue:

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


Solution:

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

Was this page helpful?
0 / 5 - 0 ratings