50

Designs commonly have smaller headline fonts for mobile designs.

Does Material UI have a mechanism for making the typography responsive?

I see that the default theme has the font sizes defined in rems - does that mean it's a matter of just reducing the base font-size? (That doesn't seem to work, what if you want to reduce the headline fonts at different rates).

Olivier Tassinari
  • 8,238
  • 4
  • 23
  • 23
dwjohnston
  • 11,163
  • 32
  • 99
  • 194

7 Answers7

46

Update

MUI v4 has responsive typography built in! Check here for details.

Old response

@Luke's answer is great. I wanted to add some detail to make this work in practice, because both breakpoints and pxToRem are accessible on the theme object... making this becomes a chicken and egg problem! My approach:

import { createMuiTheme } from "@material-ui/core"

const defaultTheme = createMuiTheme({ ... customisations that don’t rely on theme properties... })
const { breakpoints, typography: { pxToRem } } = defaultTheme

const theme = {
  ...defaultTheme,
  overrides: {
    MuiTypography: {
      h1: {
        fontSize: "5rem",
        [breakpoints.down("xs")]: {
          fontSize: "3rem"
        }
      }
    }
  }
}

export default theme
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
40

None of the other answers uses the variant preset.

The best way to MUI v5, and use variant:

<Typography sx={{ typography: { sm: 'body1', xs: 'body2' } }} >
    Hello world!
</Typography>
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
gaohomway
  • 2,132
  • 1
  • 20
  • 37
  • This is the best way Thanks –  Jan 21 '22 at 12:32
  • This works, thanks... Before this I was applying the breakpoints directly to `variant` parameter, it was changing the sizes but weirdly it also changed their font family back to the default roboto for some reason – DooMGuy096 Mar 01 '22 at 14:23
  • 1
    Note: as it just applies the style, it doesn't actually change the variant prop, and therefore doesn't change the classname – MonstraG Dec 12 '22 at 08:31
  • If you were to go from h1 to h3, does it have any impact on accessibility? Does it only affect the styling vs changing the Typography component? – Chris GW Green Jan 25 '23 at 12:32
18

Update

The latest version of Material UI (v4) fully supports response typography. See the official documentation for details.

Original Answer

As of version 1.x, Material UI does not have a specific mechanism for handling responsive typography.

You can scale the size of all MUI Typography by changing the font-size of the <html> element, as you mentioned. (docs)

const styles = theme => ({
  "@global": {
    html: {
      [theme.breakpoints.up("sm")]: {
        fontSize: 18
      }
    }
  }
}

Edit Material UI - scale font size

Theme overrides

As far as i know, the only other option is to use theme overrides to define custom styles for each of the Typography variants.

This requires replicating some of the logic in createTypography.js (ie setting line heights to maintain vertical rhythm)

const theme = createMuiTheme({
  overrides: {
    MuiTypography: {
      headline: {
        fontSize: pxToRem(24),
        [breakpoints.up("md")]: {
          fontSize: pxToRem(32)
        }
      }
    }
  }

Edit Material UI - Responsive font size

Luke Peavey
  • 3,777
  • 22
  • 24
  • 4
    Good answer, thanks. Any idea why responsive typography isn't a thing in Material-UI? It seems like a no brainer. – dwjohnston Sep 26 '18 at 04:35
  • 1
    Where's the `breakpoints` variable coming from in your 2nd snippet? I know it should come from a theme, but that's exactly the thing your creating and is not created yet at the time you're using `breakpoints`. – Christiaan Westerbeek Nov 28 '18 at 20:27
  • This is the legit answer. Thanks for sharing Luke! – danielrvt Apr 11 '19 at 10:22
18

As described in the docs you can use the responsiveFontSizes() helper to make Typography font sizes in the theme responsive.

import { createMuiTheme, responsiveFontSizes } from '@material-ui/core/styles';

let theme = createMuiTheme();
theme = responsiveFontSizes(theme);
thisismydesign
  • 21,553
  • 9
  • 123
  • 126
11

MUI v5 adds sx prop to all MUI components which supports responsive values where you can pass an object to define the values at different breakpoints:

<Typography
  sx={{
    fontSize: {
      lg: 30,
      md: 20,
      sm: 15,
      xs: 10
    }
  }}
>
  This text is resized on different screen breakpoints
</Typography>

Similar to Box, Typography also supports system properties so you can skip the sx prop and pass the fontSize property directly. The code below is the same as above, just a bit shorter to write:

<Typography
  fontSize={{
    lg: 30,
    md: 20,
    sm: 15,
    xs: 10
  }}
>
  This text is resized on different screen breakpoints
</Typography>

Codesandbox Demo

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
  • This answer is great for those small tweaks you always need to do with responsive design. Perhaps an image should not be displayed for example in the mobile view. Thanks for sharing. – RollingInTheDeep Oct 27 '21 at 08:22
6

My approach with v5

import { createTheme } from "@mui/material";

let theme = createTheme({
  // ...
});

theme = createTheme(theme, {
  typography: {
    h1: {
      fontSize: 53,
      [theme.breakpoints.only("sm")]: {
        fontSize: 71,
      },
    },
  },
});
export default theme;

Calypso
  • 192
  • 2
  • 9
4

This seem to be working for me v5

in App.js

...

import {
  createTheme,
  responsiveFontSizes,
  ThemeProvider,
} from '@mui/material/styles';

let theme = createTheme();
theme = responsiveFontSizes(theme);

...

function App() {
  return (
    <ThemeProvider theme={theme}>
      <Router>
       .....
      </Router>
    </ThemeProvider>
  );
}

"To automate this setup, you can use the responsiveFontSizes() helper to make Typography font sizes in the theme responsive." https://mui.com/customization/typography/#responsive-font-sizes

atazmin
  • 4,757
  • 1
  • 32
  • 23