Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply fontSize to CardHeader title in MUI?

I want to change the title in CardHeader to 16px. I tried changing theme in App.js but it does not seem to work

const theme = createMuiTheme({
  typography: {
    useNextVariants: true,
    overrides: {
      MuiCardHeader: {
        titleTypographyProps: {
          variant:'h2'
        }
      }
    }
  }
);

In the component:

<CardHeader
  action={
    <IconButton color="inherit">
      <MoreHorizIcon />
    </IconButton>
  }
  title="Titletext"
/>

The title font still does not change. What do I need to do to fix this?

like image 902
Kimaya Avatar asked Apr 10 '19 17:04

Kimaya


People also ask

How do I use font family in MUI?

To self-host fonts, download the font files in ttf , woff , and/or woff2 formats and import them into your code. ⚠️ This requires that you have a plugin or loader in your build process that can handle loading ttf , woff , and woff2 files. Fonts will not be embedded within your bundle.

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.


2 Answers

you cant target the header class or id and change fontSize or pass as props

titleTypographyProps={{variant:'h1' }}

that object acepts:'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'subtitle1', 'subtitle2', 'body1', 'body2', 'caption', 'button', 'overline', 'srOnly', 'inherit', "display4", 'display3', 'display2', 'display1', 'headline', 'title', 'subheading'

in your code it would be

<CardHeader
        action={
        <IconButton color="inherit">
            <MoreHorizIcon />
        </IconButton>
        }
        titleTypographyProps={{variant:'h1' }}
        title="Titletext"
      />
like image 156
Black Hole Avatar answered Sep 18 '22 17:09

Black Hole


I know this is quite old post now, but for future references anyone who stumbles upon this question might take a look at: https://github.com/mui-org/material-ui/issues/7521

Basically, we can use the classes property, which takes key/value pairs, and can add style to parts of the <ContentHeader /> component based on that.

Example:

const useStyles = makeStyles({
  root: {
    minWidth: 300,
    maxWidth: 500,
    margin: "10px 15px 10px 0",
  },
  headerTitle: {
    maxWidth: 300
  }
});

const CustomizedCard = () => {
  const materializeUIClasses = useStyles();

  return (
    <Card className={materialUIClasses.root}>
      <CardHeader 
        title={title}

        // Here we can target whatever part we need: title, subtitle, action
        classes={{
          title: materialUIClasses.headerTitle
        }} 
      />
   </Card>
}
like image 25
Petar Avatar answered Sep 18 '22 17:09

Petar