56

I'm pretty new to Material UI, and I'm trying to set a Typography with a text color like this:

const theme = createMuiTheme({
  palette: {
    text:{
      primary: "#FFFFFF"
    }
  }
});

const WhiteText = (props: { text:string, varient: Variant | 'inherit'}) => {
  return <ThemeProvider theme={theme}>
      <Typography variant={props.varient} align="center" color="textPrimary">{props.text}</Typography>
  </ThemeProvider>
}
...
<WhiteText varient="h3" text="This text should be white"/>

but the text will not change color :/

Am I applying the theme wrong?

Olivier Tassinari
  • 8,238
  • 4
  • 23
  • 23
Tim
  • 1,013
  • 1
  • 10
  • 16

10 Answers10

68

Though your approach works fine in this sandbox, it is not the approach I would recommend. Instead of nested themes, for customizations like this I would recommend using withStyles as shown below (for v4 of Material UI -- v5 example further down).

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

const WhiteTextTypography = withStyles({
  root: {
    color: "#FFFFFF"
  }
})(Typography);

export default function App() {
  return (
    <div className="App" style={{ backgroundColor: "black" }}>
      <WhiteTextTypography variant="h3">
        This text should be white
      </WhiteTextTypography>
    </div>
  );
}

Edit white text


In v5, Material UI has enhanced the color prop significantly (for all components that have a color prop) to support any color in the theme's palette, so for white you can use common.white:

import React from "react";
import Typography from "@mui/material/Typography";

export default function App() {
  return (
    <div className="App" style={{ backgroundColor: "black" }}>
      <Typography variant="h3" color="common.white">
        This text should be white
      </Typography>
    </div>
  );
}

Edit white text

Related answer: Can you add an additional color in Material UI?

Olivier Tassinari
  • 8,238
  • 4
  • 23
  • 23
Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
50

If you want to set a default color for all Typography elements, but not for other Material UI elements you can try this:

const theme = createMuiTheme({
  typography: {
    allVariants: {
      color: "pink"
    },
  },
});
PrzemKon
  • 751
  • 5
  • 4
20

The color prop of Typography is theme aware which is a nice feature of sx prop. This means besides setting the color as usual like this:

<Typography variant="h1" color="#00ff00">

You can reference the themed colors, either from the default palette or a custom palette defined like below:

const theme = createTheme({
  palette: {
    tomato: '#FF6347',
    pink: {
      deep: '#FF1493',
      hot: '#FF69B4',
      medium: '#C71585',
      pale: '#DB7093',
      light: '#FFB6C1',
    },
  },
});

After registering the custom theme in ThemeProvider, you can use it in Typography by specifying the string path of the Palette object to the color value:

<Typography color="tomato">text</Typography>
<Typography color="pink.deep">text</Typography>
<Typography color="pink.hot">text</Typography>
<Typography color="pink.medium">text</Typography>
<Typography color="pink.pale">text</Typography>
<Typography color="pink.light">text</Typography>

Here is a couple of examples of Typograhy using the default colors from the palette:

<Typography color="primary.main">text</Typography>
<Typography color="primary.dark">text</Typography>
<Typography color="primary.light">text</Typography>
<Typography color="secondary">text</Typography>
<Typography color="text.secondary">text</Typography>
<Typography color="text.disabled">text</Typography>

Codesandbox Demo

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
13

If you don't want to use any themes. You can set it via style Attribute also.

<Typography style={{color:"#00adb5"}}  variant="h3" >My Text</Typography>

For MUI 5, use sx attribute,

<Typography sx={{ color: "#00adb5" }} variant="h3">
   My Text
</Typography>
Smack Alpha
  • 1,828
  • 1
  • 17
  • 37
8

Set Typography Text Color in Material UI

<Typography gutterBottom variant="h9" component="h2" color="secondary">
    {card.unitNumberList}
</Typography>
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Soma sundara
  • 157
  • 1
  • 2
5

I would try this - Include a typgraphy property in your theme, something like below with an 'h3' variant.

const theme = createMuiTheme({
  palette: {
    text: {
      primary: "#FFFFFF"
    }
  },

  typography: {
    useNextVariants: true,
    fontFamily: "Montserrat",
    h3: {
      fontSize: 33,
      fontFamily: "Montserrat",
      fontWeight: 300,
      color: "#2882F8",
      letterSpacing: "0.0075em",
      verticalAlign: "middle",
      alignItems: "center",
      textAlign: "center"
    }
  }
});

Then the color of your Typography should come directly from the variant="h3" that you just declared inside theme. You dont need to seperately pass the 'color' props to Typgraphy

For a working implementations of this, you can check this Repo of mine, where I am keeping all my Typography variants in a single central global file called globalTheme.js and in the App.js wrapping all the other components within MuiThemeProvider as below

<MuiThemeProvider theme={globalTheme}>

So all Typography component anywhere in the project will have access to the variants that I have declared in that globalTheme.js file.

Rohan_Paul
  • 1,504
  • 3
  • 29
  • 36
  • No dice - I'm using material 4.9 so `MuiThemeProvider` -> `ThemeProvider` and there's no `useNextVariants`, but from what I can tell the theme isn't being applied at all. When I use `typography: {h3: {color: "#FFFFFF"}}` I also get no change in text color – Tim Mar 09 '20 at 20:44
  • 1
    I would definitely not recommend doing this. Material UI clearly separates palette (colors) from typography (font definitions). I don't think you'd want to mix them by defining color in the typography definition. – Javid Jamae Sep 25 '20 at 20:47
2

First of all, you need to define alternative colors for the text elements.

text: {
  primary: 'rgba(60, 72, 88, 1)',
  secondary: 'rgba(132, 146, 166, 1)',
  disabled: 'rgba(60, 72, 88, 0.38)',
  hint: 'rgba(60, 72, 88, 0.38)',
}

Then you can do the following:

<Typography variant="h1">Lorem ipsum</Typography> // Default text color
<Typography variant="subtitle1" color="textSecondary">dolor sit amet</Typography> // Secondary text color.
<Typography variant="body1" color="secondary">consectetur adipiscing elit</Typography> // Global secondary color.
Burak Özdemir
  • 510
  • 8
  • 18
1

You can try using make styles from the material-ui core to create a custom look for your text which can include the text colour as shown in the example below

import {makeStyles} from '@material-ui/core/styles'

const useStyles=makeStyles((theme)=>({
text:{
    color:"#ffffff"
}
}));

const classes=useStyles();
<Typography align="center" className={classes.text}>This text should be white</Typography>

0

For those who are still on v4, uses typescript and want to have a typesafe definition can extend mui's Typography like this:

import React, { FC } from "react";
import { createStyles, makeStyles, Theme } from "@material-ui/core";
import MuiTypography, { TypographyProps } from "@material-ui/core/Typography";
import clsx from "clsx";

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    warning: {
      color: theme.palette.warning.main,
    },
    success: {
      color: theme.palette.success.main,
    },
  })
);

type AdditionalColorKeys = keyof ReturnType<typeof useStyles>;

export type TypographyWithSpacingProps = Omit<TypographyProps, "color"> & {
  color?: AdditionalColorKeys | TypographyProps["color"];
};

export const Typography: FC<TypographyWithSpacingProps> = ({ color, className, ...props }) => {
  const classes = useStyles();
  let colorClass = "";

  if (color && color in classes) {
    colorClass = classes[color as keyof ReturnType<typeof useStyles>];
  }

  return <MuiTypography {...props} className={clsx(className, colorClass)} />;
};
Ruslan Zaytsev
  • 424
  • 4
  • 11
0

You can also simply change the Typography's color by adding system props 'sx' like this

<Typography
      variant="h6"
      sx={{
          color: "white",
         }}
>
Jon Snow
  • 51
  • 5