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:
string | number
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
string | number
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
string | string[]
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
'row' | 'column' | 'dense' | 'row dense' | 'column dense'
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
string
Defines the size of implicitly-created columns
string
Defines the size of implicitly-created rows
'extra-small' | 'small' | 'medium' | 'large' | 'extra-large'
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
'extra-small' | 'small' | 'medium' | 'large' | 'extra-large'
Controls spacing between columns only (same values as gap)
'extra-small' | 'small' | 'medium' | 'large' | 'extra-large'
Controls spacing between rows only (same values as gap)
'start' | 'end' | 'center' | 'stretch'
Aligns grid items along the inline (row) axis
'start' | 'end' | 'center' | 'stretch'
Aligns grid items along the block (column) axis
'start' | 'end' | 'center' | 'stretch' | 'space-around' | 'space-between' | 'space-evenly'
Aligns the grid along the inline (row) axis within its container
'start' | 'end' | 'center' | 'stretch' | 'space-around' | 'space-between' | 'space-evenly'
Aligns the grid along the block (column) axis within its container
boolean
default:"false"
When true, uses
display: inline-grid instead of display: gridReact.CSSProperties
Additional inline styles (merged with grid styles)
string
Additional CSS class names to apply
(props: React.ComponentPropsWithRef<'div'>) => React.ReactElement
Custom render function from Base UI
React.Ref<HTMLDivElement>
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.string
The name of the grid area to place this item in (must match a templateAreas name)
number | string
The grid column line where the item starts
number | string
The grid column line where the item ends
number | string
The grid row line where the item starts
number | string
The grid row line where the item ends
number | string
How many columns the item should span
number | string
How many rows the item should span
'start' | 'end' | 'center' | 'stretch'
Aligns this item along the inline (row) axis
'start' | 'end' | 'center' | 'stretch'
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