Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I style Material UI table header?

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

like image 808
Eylon Shmilovich Avatar asked Mar 21 '26 10:03

Eylon Shmilovich


2 Answers

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

like image 190
Kasun Shanaka Avatar answered Mar 23 '26 00:03

Kasun Shanaka


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
like image 43
Raj Kumar Avatar answered Mar 22 '26 22:03

Raj Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!