Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive Typography in MUI?

Designs commonly have smaller headline fonts for mobile designs.

Does MUI 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).

like image 374
dwjohnston Avatar asked Sep 24 '18 03:09

dwjohnston


People also ask

How do you make text responsive in MUI?

Responsive h3 To automate this setup, you can use the responsiveFontSizes() helper to make Typography font sizes in the theme responsive.

What is responsive typography?

Responsive design is the practice of writing CSS styles in a way that displays the website correctly on all device widths. Whether you're using a mobile phone, tablet, or ultra-wide monitor, the website should look good and be readable, even if it doesn't look exactly the same on all devices.

How do you style typography in material UI?

You can import <Typography /> component from @mui/material using the following code. import Typography from '@mui/material/Typography'; // or import { Typography } from '@mui/material'; Important Props and its values: align: It is used to align the text on the component.

How do you make MUI in typography bold?

How do you make MUI Typography bold? MUI Typography font weight can be set to bold by adding fontWeight: bold value to the sx prop. Here is example code: <Typography sx={{fontWeight: 'bold'}}>My Text</Typography> .


2 Answers

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 
like image 75
Tim Holmes-Mitra Avatar answered Sep 17 '22 21:09

Tim Holmes-Mitra


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

like image 39
Luke Peavey Avatar answered Sep 21 '22 21:09

Luke Peavey