Andrea D_
Andrea D_

Reputation: 2141

How to give round corners to the TableHead of Material UI

I am working with React and Material UI and I am trying to give a broder-radius of 10px to the header of the table so that only the TableHead have round corners. The problem is that border-radius doesn't seem to work at all on TableHead as you can see in this screenshot: enter image description here

Code (taken from the official docs, I just added the style to the TableHead):

<TableContainer>
  <Table aria-label="customized table">
    <TableHead style={{ backgroundColor: '#E2E2E2', borderRadius: '10px' }}>
      <TableRow>
        <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>
    <TableBody>
      {rows.map((row) => (
        <TableRow key={row.name}>
          <TableCell component="th" scope="row">
            {row.name}
          </TableCell>
          <TableCell align="right">{row.calories}</TableCell>
          <TableCell align="right">{row.fat}</TableCell>
          <TableCell align="right">{row.carbs}</TableCell>
          <TableCell align="right">{row.protein}</TableCell>
        </TableRow>
      ))}
    </TableBody>
  </Table>
</TableContainer>;

How do I get this done?

Upvotes: 4

Views: 7107

Answers (2)

Spatz
Spatz

Reputation: 20158

As far as I know, there are no ways to set border-radius for the entire tablerow, but the problem can be solved by using a separate radius for the first and last cells:

const { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, makeStyles } = MaterialUI

const useStyles = makeStyles((theme) => ({
  table: {
    minWidth: 400,
  },
  thead: {
    backgroundColor: 'lightgray',
    '& th:first-child': {
      borderRadius: '1em 0 0 1em'
    },
    '& th:last-child': {
      borderRadius: '0 1em 1em 0'
    }
  }
}))

function createData(name, calories, fat, carbs, protein) {
  return { name, calories, fat, carbs, protein }
}

const rows = [
  createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
  createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
  createData('Eclair', 262, 16.0, 24, 6.0),
  createData('Cupcake', 305, 3.7, 67, 4.3),
  createData('Gingerbread', 356, 16.0, 49, 3.9),
]

const App = function () {
  const classes = useStyles()

  return (
    <TableContainer component={Paper}>
      <Table className={classes.table} aria-label="customized table">
        <TableHead classes={{ root: classes.thead }}>
          <TableRow>
            <TableCell>Dessert (100g serving)</TableCell>
            <TableCell align="right">Calories</TableCell>
            <TableCell align="right">Fat (g)</TableCell>
            <TableCell align="right">Carbs (g)</TableCell>
            <TableCell align="right">Protein (g)</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          {rows.map((row) => (
            <TableRow key={row.name}>
              <TableCell component="th" scope="row">{row.name}</TableCell>
              <TableCell align="right">{row.calories}</TableCell>
              <TableCell align="right">{row.fat}</TableCell>
              <TableCell align="right">{row.carbs}</TableCell>
              <TableCell align="right">{row.protein}</TableCell>
            </TableRow>
          ))}
        </TableBody>
      </Table>
    </TableContainer>
  )}

ReactDOM.render(<App />, document.getElementById('root'))
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@material-ui/[email protected]/umd/material-ui.development.js"></script>

<div id="root"></div>

Upvotes: 4

Drew Reese
Drew Reese

Reputation: 203407

You can continue using the makeStyles hook generator to style the table header.

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

const useTableHeaderStyles = makeStyles({
  // .MuiTableHead-root
  root: {
    backgroundColor: '#E2E2E2',
    borderRadius: '10px',
  },
});

In the component:

const tableHeaderClasses = useTableHeaderStyles();

...

<TableHead classes={tableHeaderClasses.root}>
  ...
</TableHead>

Upvotes: 1

Related Questions