Skip to content

Data grid - Styling

The grid CSS can be easily overwritten.

Using the sx prop

For one-off styles, the sx prop can be used. It allows to apply simple to complex customizations directly onto the DataGrid element. The keys accepted can be any CSS property as well as the custom properties provided by MUI. For more details, visit the sx prop page.

<DataGrid sx={{ m: 2 }} /> // Sets the margin to 2 times the spacing unit = 16px
No rows
Desk
Commodity
Trader Name
Trader Email

Rows per page:

0–0 of 0

Press Enter to start editing

Styling column headers

The GridColDef type has properties to apply class names and custom CSS on the header.

  • headerClassName: to apply class names into the column header. It can also be a function, which is called with a GridColumnHeaderParams object.
  • headerAlign: to align the content of the header. It must be 'left' | 'right' | 'center'.
const columns: GridColumns = [
  {
    field: 'first',
    headerClassName: 'super-app-theme--header',
    headerAlign: 'center',
  },
  {
    field: 'last',
    headerClassName: 'super-app-theme--header',
    headerAlign: 'center',
  },
];
first
last
Jane
Carter
Jack
Smith
Gill
Martin

Rows per page:

1–3 of 3

Press Enter to start editing

Styling rows

The getRowClassName prop can be used to apply a custom CSS class on each row. It's called with a GridRowParams object and must return a string.

interface GridRowParams<R extends GridRowModel = GridRowModel> {
  /**
   * The grid row id.
   */
  id: GridRowId;
  /**
   * The row model of the row that the current cell belongs to.
   */
  row: R;
  /**
   * All grid columns.
   */
  columns: GridColumns;
  /**
   * Get the cell value of a row and field.
   * @param {GridRowId} id The row id.
   * @param {string} field The field.
   * @returns {any} The cell value.
   */
  getValue: (id: GridRowId, field: string) => any;
}
No rows
Desk
Commodity
Trader Name
Trader Email
Quantity
Filled Quantity
Is Filled
Status

Rows per page:

0–0 of 0

Press Enter to start editing

Styling cells

There are multiple ways to apply a custom CSS class on a cell.

  1. Using thecellClassName property of GridColDef:

This property allows to set a CSS class that is applied on every cell of the column it was defined. It can also be a function, which is called with a GridCellParams object.

const columns: GridColumns = [
  {
    field: 'name',
    cellClassName: 'super-app-theme--cell',
  },
  {
    field: 'score',
    type: 'number',
    cellClassName: (params: GridCellParams<number>) =>
      clsx('super-app', {
        negative: params.value < 0,
        positive: params.value > 0,
      }),
  },
];
name
score
Jane
100
Jack
-100
Gill
-50

Rows per page:

1–3 of 3

  1. Using the getCellClassName prop:

This prop is called for every cell in every column. Different from the first option, this prop is defined at the grid level, not column level. It is also called with a GridCellParams object.

city
oct
nov
dec
Amsterdam
7.1 °C
4 °C
10.2 °C
Barcelona
14.9 °C
12.3 °C
18.2 °C
Paris
8.1 °C
5.4 °C
12.3 °C
São Paulo
20.2 °C
21.1 °C
19.2 °C

Rows per page:

1–4 of 4

Press Enter to start editing

Cell alignment

Use the align property in GridColDef to change the alignment of content of the cells. Choose between one of the following values: 'left' | 'right' | 'center'.

Striped rows

You can use the indexRelativeToCurrentPage param passed to getRowClassName to apply alternating styles to the rows.

The following demo illustrates how this can be achieved.

Avatar
Name
Website
Rating
Email
Phone
Username
City

Rows per page:

0–0 of 0

Press Enter to start editing

Custom theme

The following demo leverages the CSS customization API to match the Ant Design specification.

No rows
Desk
Commodity
Trader Name
Trader Email
Quantity
Filled Quantity
Is Filled