Data Grid - Layout
The Data Grid offers multiple layout modes.
Flex parent container
The Data Grid can be placed inside a flex container with flex-direction: column.
Without setting the minimum and maximum height, the Data Grid takes as much space as it needs to display all rows.
Minimum and maximum height
In the demo below, the Data Grid is placed inside a flex container with a minimum height of 200px and a maximum height of 400px and adapts its height when the number of rows changes.
Percentage dimensions
When using percentages (%) for height or width, make sure that the Data Grid's parent container has intrinsic dimensions. Browsers adjust the element based on a percentage of its parent's size. If the parent has no size, the percentage will be zero.
Predefined dimensions
You can predefine dimensions for the parent of the Data Grid.
import { DataGrid } from '@mui/x-data-grid';
import { useDemoData } from '@mui/x-data-grid-generator';
export default function FixedSizeGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 5,
maxColumns: 6,
});
return (
<div style={{ width: '100%' }}>
<div style={{ height: 350, width: '100%' }}>
<DataGrid {...data} />
</div>
</div>
);
}
Height prop
Use the height prop to set a fixed height directly on the Data Grid, without a parent element with an explicit height.
It accepts a number (in pixels), a CSS length such as '50vh', a percentage, or a calculation such as 'calc(100vh - 200px)'.
Percentage values require a parent element with an explicit height.
For content-based sizing, use a flex parent container instead of values such as 'auto', 'min-content', 'max-content', or 'fit-content', which this prop does not support.
To change the height, update the prop instead of sx or style, because the prop also sets the minimum and maximum height.
import { DataGrid } from '@mui/x-data-grid';
import { useDemoData } from '@mui/x-data-grid-generator';
export default function HeightGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 5,
maxColumns: 6,
});
return <DataGrid {...data} height={350} />;
}
Overlay height
When data grid has no content, overlays (such as "Loading" or "No rows") take the height of two rows by default.
To customize the overlay height, use the --DataGrid-overlayHeight CSS variable.
import Box from '@mui/material/Box';
import { DataGrid } from '@mui/x-data-grid';
import { styled } from '@mui/material/styles';
const StyledGridOverlay = styled('div')(({ theme }) => ({
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
height: '100%',
'& .no-rows-primary': {
fill: '#3D4751',
...theme.applyStyles('light', {
fill: '#AEB8C2',
}),
},
'& .no-rows-secondary': {
fill: '#1D2126',
...theme.applyStyles('light', {
fill: '#E8EAED',
}),
},
}));
function CustomNoRowsOverlay() {
return (
<StyledGridOverlay>
<svg
fill="none"
width={96}
viewBox="0 0 452 257"
aria-hidden
focusable="false"
>
<path
className="no-rows-primary"
d="M348 69c-46.392 0-84 37.608-84 84s37.608 84 84 84 84-37.608 84-84-37.608-84-84-84Zm-104 84c0-57.438 46.562-104 104-104s104 46.562 104 104-46.562 104-104 104-104-46.562-104-104Z"
/>
<path
className="no-rows-primary"
d="M308.929 113.929c3.905-3.905 10.237-3.905 14.142 0l63.64 63.64c3.905 3.905 3.905 10.236 0 14.142-3.906 3.905-10.237 3.905-14.142 0l-63.64-63.64c-3.905-3.905-3.905-10.237 0-14.142Z"
/>
<path
className="no-rows-primary"
d="M308.929 191.711c-3.905-3.906-3.905-10.237 0-14.142l63.64-63.64c3.905-3.905 10.236-3.905 14.142 0 3.905 3.905 3.905 10.237 0 14.142l-63.64 63.64c-3.905 3.905-10.237 3.905-14.142 0Z"
/>
<path
className="no-rows-secondary"
d="M0 10C0 4.477 4.477 0 10 0h380c5.523 0 10 4.477 10 10s-4.477 10-10 10H10C4.477 20 0 15.523 0 10ZM0 59c0-5.523 4.477-10 10-10h231c5.523 0 10 4.477 10 10s-4.477 10-10 10H10C4.477 69 0 64.523 0 59ZM0 106c0-5.523 4.477-10 10-10h203c5.523 0 10 4.477 10 10s-4.477 10-10 10H10c-5.523 0-10-4.477-10-10ZM0 153c0-5.523 4.477-10 10-10h195.5c5.523 0 10 4.477 10 10s-4.477 10-10 10H10c-5.523 0-10-4.477-10-10ZM0 200c0-5.523 4.477-10 10-10h203c5.523 0 10 4.477 10 10s-4.477 10-10 10H10c-5.523 0-10-4.477-10-10ZM0 247c0-5.523 4.477-10 10-10h231c5.523 0 10 4.477 10 10s-4.477 10-10 10H10c-5.523 0-10-4.477-10-10Z"
/>
</svg>
<Box sx={{ mt: 2 }}>No rows</Box>
</StyledGridOverlay>
);
}
export default function GridOverlayHeight() {
return (
<Box sx={{ width: '100%', display: 'flex', flexDirection: 'column' }}>
<DataGrid
columns={[
{ field: 'ID' },
{ field: 'First name', rowHeader: true },
{ field: 'Last name', rowHeader: true },
]}
rows={[]}
slots={{ noRowsOverlay: CustomNoRowsOverlay }}
sx={{ '--DataGrid-overlayHeight': '300px' }}
/>
</Box>
);
}
Auto height
The autoHeight prop enables the Data Grid to adjust its size based on its content.
This means that the Data Grid's height will be determined by the number of rows, ensuring that all rows will be visible to the user simultaneously.