Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override .MuiTypography-body1 class in Material UI

I want to override the defaults and apply a font size and family (without using inline) to a list element in a drawer. I don't want to create a new theme. I use the "useStyles" function to do that for other properties, but for some reason it doesn't work for them. If I disable them in the browser dev tools my changes take effect - any idea of how to do that in the code?

There is another similar thread but the explanation unfortunately doesn't help me:overwrite the font size of this class .MuiTypography-body1

const useStyles = makeStyles(theme => ({
  root: {
    display: "flex"
  },
  listItems: {
    margin: "7vh 0 7vh 0"
  },
  listText: {
    padding: "1vh 0 1vh 0",
    textAlign: "center",
    fontSize: "50px",
    fontFamily: "typeface-pacifico"
  }
}));
like image 615
Mystic_Quest Avatar asked Nov 13 '19 01:11

Mystic_Quest


People also ask

How do you override material UI font family?

To change the font family of all React Material UI components, we can call the createTheme function provided by Material UI to set the font styles for the theme. Then we apply the theme with ThemeProvider to apply the styles to the whole app. We call createTheme with an object that has the typography.

How do I change my font family in MUI?

You can change the font family with the theme. typography. fontFamily property.

How do I change the font size in material UI?

To change font size of text field in React Material UI, we can set the InputProps and the InputLabelProps prop to set the font size of the input box and the input label respectively. to set InputProps and InputLabelProps to { style: { fontSize: 40 } } so that the input box and the label both have font size 40px.


Video Answer


1 Answers

You have to create a MuiTheme in order to override body1.

Below is the code you will need.

// theme.js
import { createMuiTheme } from '@material-ui/core/styles'

const theme = createMuiTheme({
  typography: {
    body1: {
      fontFamily: "'Open Sans', sans-serif",
      fontWeight: 400,
      fontSize: 16,
      color: "red"
    }
  }
})

export default theme

Then have a ThemeProvider component wrap your content like below,

// example.js
import React from 'react'
import { ThemeProvider } from '@material-ui/styles'
import { makeStyles } from '@material-ui/core/styles';
import theme from 'theme.js'
import Button from '@material-ui/core/Button';

const useStyles = makeStyles(theme => ({
  // Some extra styling if you'd like
  button: {
    margin: theme.spacing(1),
  },
}));


export default function Example() {
  const classes = useStyles();
  return (
    <ThemeProvider theme={theme}>
      <Button className={classes.button}>I'm a button</Button>
    </ThemeProvider>
  )
}
like image 194
Hasan Sefa Ozalp Avatar answered Nov 15 '22 09:11

Hasan Sefa Ozalp