Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material UI - Typography fontSize

Tags:

Recently I upgraded my material UI version from 3.9.4 to 4.11.0, I had to replace these on the theme style override: enter image description here

to avoid these warnings: enter image description here

But I require to put that fontSize styles wit !important since that's working on a widget which is rendered on different web pages and if I don't use the !important, then the styles are overritten by the ones of the page, Is there a way to use !important label on the typography fontSize style on the latest versions?

I tried using fontSize: `16 !important`, and fontSize: [[16], ['!important'] without success.

any help would be welcome, Thanks in advice!!!

EDIT: On the override part it receives the styles even as a string but on the typography part, even using @Ryan Cogswell suggestion, it still throw me the same warning

const Theme = createMuiTheme({
  root: {
    display: 'flex',
  },
  palette: {
    primary: {
      main: '#052d4f',
    },
    secondary: {
      main: '#2376b8',
    },
  },
  typography: {
    fontFamily: 'Arial, Helvetica, sans-serif !important',
    fontSize: [16, "!important"],
  },
  overrides: {
    MuiTypography: {
      body2: {
        fontFamily: 'Arial, Helvetica, sans-serif !important',
        fontSize: "16px !important",
      },
      subtitle1: {
        fontFamily: 'Arial',
        fontSize: "16px !important",
      },
    },
    MuiTablePagination: {
      toolbar: {
        fontSize: "14px !important",
      }
    },
    MuiAutocomplete: {
      root: {
        paddingLeft: "15px",
        paddingRight: "15px",
      },
      groupLabel: {
        fontWeight: 700,
        color: "black",
        fontSize: "14px !important",
      },
      option: {
        paddingTop: "0px",
        paddingBottom: "0px",
        fontSize: "14px !important",
        height: "25px"
      }
    }
  },
  status: {
    danger: 'orange',
  },
});
like image 994
Lorenzo Zuluaga Avatar asked Oct 12 '20 21:10

Lorenzo Zuluaga


People also ask

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 I change the font size in material UI typography?

To change the font-size of MUI you can provide a fontSize property. The default value is 14px .

How do you make a typography bold in material UI?

Material-UI Typography Bold Text If you need the text to be bold, simply add fontWeight: 'bold' to the sx prop. This is the same as font-weight: 700 .

How do you use font weight in typography?

Font weight fontWeightLight <Box sx={{ fontWeight: 'regular' }}>… <Box sx={{ fontWeight: 'medium' }}>… <Box sx={{ fontWeight: 500 }}>… <Box sx={{ fontWeight: 'bold' }}>…


1 Answers

The syntax you want is fontSize: [16, "!important"]. It also works to put the 16 within an array, but you can't put "!important" in an array.

Here's a working example:

import React from "react";
import { ThemeProvider, createMuiTheme } from "@material-ui/core/styles";
import Typography from "@material-ui/core/Typography";

const theme = createMuiTheme({
  //v5.0.0
  typography: {
    body2: {
        fontSize: [16, "!important"]
    }
  },
  //older versions
  overrides: {
    MuiTypography: {
      body2: {
        fontSize: [16, "!important"]
      }
    }
  }
});
export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <div className="App">
        <Typography variant="body2">Hello CodeSandbox</Typography>
      </div>
    </ThemeProvider>
  );
}

Edit fontSize override important

JSS Documentation: https://cssinjs.org/jss-syntax?v=v10.4.0#modifier-important

like image 58
Ryan Cogswell Avatar answered Oct 27 '22 18:10

Ryan Cogswell