How can I style Material UI table header?

Viewed 14386

How can I style Materials' UI table header?

Maybe something like add classes with useStyle.

<TableHead >
            <TableRow >
                <TableCell hover>Dessert (100g serving)</TableCell>
                <TableCell align="right">Calories</TableCell>
                <TableCell align="right">Fat&nbsp;(g)</TableCell>
                <TableCell align="right">Carbs&nbsp;(g)</TableCell>
                <TableCell align="right">Protein&nbsp;(g)</TableCell>
            </TableRow>
</TableHead>

I want to add style to the table header

6 Answers

you can use withStyles and create an custom element with your own style like this. you can check the working scenario

Edit strange-cherry-832ug

const TableHead = withStyles(theme => ({
  root: {
    backgroundColor: 'orange'
  }
}))(MuiTableHead);

const TableHeaderCell = withStyles(theme => ({
  root: {
    color: 'white'
  }
}))(TableCell);
<TableHead>
  <TableRow>
    <TableHeaderCell>Dessert (100g serving)</TableHeaderCell>
    <TableHeaderCell align="right">Calories</TableHeaderCell>
    <TableHeaderCell align="right">Fat&nbsp;(g)</TableHeaderCell>
    <TableHeaderCell align="right">Carbs&nbsp;(g)</TableHeaderCell>
    <TableHeaderCell align="right">Protein&nbsp;(g)</TableHeaderCell>
  </TableRow>
</TableHead>
CustomHeaderStyle

I personally used "makeStyles" for styling but you can use "withStyles" as well.

Here is the table:

<TableHead >
    <TableRow className={classes.root}>
        <TableCell>Dessert (100g serving)</TableCell>
        <TableCell align="right">Calories</TableCell>
        <TableCell align="right">Fat&nbsp;(g)</TableCell>
        <TableCell align="right">Carbs&nbsp;(g)</TableCell>
        <TableCell align="right">Protein&nbsp;(g)</TableCell>
    </TableRow>
</TableHead>

Here is the styles:

const useStyles = makeStyles({

    root: {
        "& .MuiTableCell-head": {
            color: "white",
            backgroundColor: "blue"
        },
    }
});

Here is the output:

Material UI table header color change

Here's how to style all of your TableHeaders globally by adjusting your theme:

import { createMuiTheme } from "@material-ui/core/styles";

export const theme = createMuiTheme({
  overrides: {
    MuiTableCell: {
      head: {
        color: "grey",
      }
    }
  },
  palette: {
    primary: {
      ... 
    },
    secondary: {
     ...
    }
  },
});

Note: this is in the MUI v4 format, the v5 format is slightly different. Check the MUI v4 -> v5 migration guide for details.

You can create CSS as an object and give it with className:

export function Content() {
    const classes = useStyles();

    return (
        <div className={classes.root}>

            <TableHead className={classes.tableHead}>
                <TableRow >
                    <TableCell hover>Dessert (100g serving)</TableCell>
                    <TableCell align="right">Calories</TableCell>
                    <TableCell align="right">Fat&nbsp;(g)</TableCell>
                    <TableCell align="right">Carbs&nbsp;(g)</TableCell>
                    <TableCell align="right">Protein&nbsp;(g)</TableCell>
                </TableRow>
            </TableHead>
    )}

Here is its custom style:

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

export const useStyles = makeStyles(() => ({
    table: {
        backgroundColor: 'red'
    }
}))

I do not advise you to style in CSS because Material UI styles may not match with CSS.

Add style inside <TableHead> like below:

NOTE:

  • backgroundColor: this is the color of header background
  • color: this is the color of the font inside the header
<TableHead style={{backgroundColor:'red', color: 'white',}}>

After adding it to your code, it would look like this:

<TableHead style={{backgroundColor:'red', color: 'white',}}>
            <TableRow className={classes.root}>
                <TableCell>Dessert (100g serving)</TableCell>
                <TableCell align="right">Calories</TableCell>
                <TableCell align="right">Fat&nbsp;(g)</TableCell>
                <TableCell align="right">Carbs&nbsp;(g)</TableCell>
                <TableCell align="right">Protein&nbsp;(g)</TableCell>
            </TableRow>
        </TableHead>
Related