Description
The Grid component provides a powerful and flexible API for creating CSS Grid layouts. It supports all major grid properties including template areas, automatic column/row sizing, gap spacing, and alignment controls. The component also includes a companion GridItem component for positioning items within the grid.Props
Grid
Grid extends all standard HTML div attributes through Base UI’suseRender hook and accepts the following props:
Defines the grid template columns
- When a number is provided, creates columns using
repeat(n, 1fr) - When a string is provided, uses standard CSS Grid template syntax
Defines the grid template rows
- When a number is provided, creates rows using
repeat(n, 1fr) - When a string is provided, uses standard CSS Grid template syntax
Defines named grid areas using CSS Grid template areas syntax
- Accepts a single string or an array of strings
- Array format automatically adds quotes around each row
Controls how auto-placed items flow into the grid
row- Fill rows firstcolumn- Fill columns firstdense- Fill holes in the gridrow dense- Fill rows first with dense packingcolumn dense- Fill columns first with dense packing
Defines the size of implicitly-created columns
Defines the size of implicitly-created rows
Controls spacing between all grid items
extra-small- Uses--rs-space-2small- Uses--rs-space-3medium- Uses--rs-space-5large- Uses--rs-space-9extra-large- Uses--rs-space-11
Controls spacing between columns only (same values as gap)
Controls spacing between rows only (same values as gap)
Aligns grid items along the inline (row) axis
Aligns grid items along the block (column) axis
justifyContent
'start' | 'end' | 'center' | 'stretch' | 'space-around' | 'space-between' | 'space-evenly'
Aligns the grid along the inline (row) axis within its container
alignContent
'start' | 'end' | 'center' | 'stretch' | 'space-around' | 'space-between' | 'space-evenly'
Aligns the grid along the block (column) axis within its container
When true, uses
display: inline-grid instead of display: gridAdditional inline styles (merged with grid styles)
Additional CSS class names to apply
Custom render function from Base UI
A ref to access the underlying div element
GridItem
GridItem is a companion component for positioning items within a Grid. It extends all standard HTML div attributes.The name of the grid area to place this item in (must match a templateAreas name)
The grid column line where the item starts
The grid column line where the item ends
The grid row line where the item starts
The grid row line where the item ends
How many columns the item should span
How many rows the item should span
Aligns this item along the inline (row) axis
Aligns this item along the block (column) axis
Usage examples
Basic grid with numeric columns
Custom column sizes
Using template areas
Column and row spanning
Auto-flow grid
Responsive grid
Centered grid items
Different row and column gaps
Styling notes
The Grid component applies inline styles directly to the element, making it highly flexible:display: grid(orinline-gridwheninlineis true)- All grid properties are applied as inline styles
- Custom styles passed via the
styleprop are merged with grid styles
gridArea,gridColumnStart,gridColumnEnd,gridRowStart,gridRowEndgridColumnandgridRowfor span valuesjustifySelfandalignSelffor individual item alignment