When to use it#
- Use Grid to place things in columns: a page with a main part and a side part, or a set of service cards, news items or quick links.
- Start with one column and add columns from a breakpoint (a width of the grid itself):
kv-grid--cols-1 kv-grid--lg-cols-12. The grid measures its own width, so the same markup works in a sidebar, in a card and at 400% zoom. - For as many columns as fit, none narrower than you choose, use
kv-grid--fit-sm,-mdor-lg. - A grid of cards or links is a list. Render
Grid.Rootas a<ul>with one<li>per item, so a screen reader says how many there are. - Not for one column of things with space between them: use Stack. Not for data with rows and columns that people compare: use a Table.
Example#
Waste and recycling
Collection days and recycling centres.
Schools and childcare
Apply for a place and see term dates.
Housing and building
Permits, adaptations and grants.
import { Card, Grid, Heading, Link } from '@kvirn-ui/react'
import { useGridTexts } from './texts.ts'
export function DefaultGrid() {
const { texts } = useGridTexts()
const services = [texts.services.waste, texts.services.school, texts.services.housing]
return (
<Grid.Root className="kv-grid--fit-md" as="ul">
{services.map((service) => (
<Card.Root key={service.title} as="li">
<Card.Body className="kv-prose">
<Heading as="h3">
<Link href="#">{service.title}</Link>
</Heading>
<p>{service.text}</p>
</Card.Body>
</Card.Root>
))}
</Grid.Root>
)
}Use cases#
A 12-column page layout
One column on a phone. When the grid is at least 64rem wide, the main part takes eight columns and the side part four. The cells are in reading order, so a keyboard and a screen reader get the order you see.
Apply for a preschool place
You can apply from the year your child turns one. Apply at least four months before you need the place.
Contact
Preschool administration, 08:00 to 16:00 on weekdays.
import { Card, Grid, Heading } from '@kvirn-ui/react'
import { useGridTexts } from './texts.ts'
export function PageLayout() {
const { texts } = useGridTexts()
return (
<Grid.Root className="kv-grid--cols-1 kv-grid--lg-cols-12">
<Grid.Item className="kv-grid-item--lg-span-8">
<Card.Root className="kv-prose">
<Heading as="h3">{texts.layout.main.title}</Heading>
<p>{texts.layout.main.text}</p>
</Card.Root>
</Grid.Item>
<Grid.Item className="kv-grid-item--lg-span-4">
<Card.Root className="kv-prose">
<Heading as="h3">{texts.layout.contact.title}</Heading>
<p>{texts.layout.contact.text}</p>
</Card.Root>
</Grid.Item>
</Grid.Root>
)
}Props in this example:
Warning: Don't forget
Line placement (start-n, end-n) can put a later cell before an earlier one. Keep it in DOM order, because focus and reading order follow the DOM (1.3.2 Meaningful Sequence, 2.4.3 Focus Order).
A service page with navigation for its section
Pages in the same section link to each other from a side column of three of twelve columns, with the content over the other nine. Render the side Item as a nav and name it, and mark the current page with current.
Apply for a place at a preschool
You can apply from the year your child turns one. Apply at least four months before you need the place.
import { Grid, Heading, Link, Stack } from '@kvirn-ui/react'
import { useGridTexts } from './texts.ts'
export function ServicePage() {
const { texts } = useGridTexts()
const { service } = texts
return (
<Grid.Root className="kv-grid--lg-cols-12 kv-grid--lg-gap-10">
<Grid.Item as="nav" aria-label={service.navigationLabel} className="kv-grid-item--lg-span-3">
<Stack as="ul" className="kv-stack--gap-2">
{service.links.map((label) => (
<li key={label}>
<Link href="#" current={label === service.current ? 'page' : undefined}>
{label}
</Link>
</li>
))}
</Stack>
</Grid.Item>
<Grid.Item className="kv-grid-item--lg-span-9 kv-prose">
<Heading as="h3">{service.heading}</Heading>
<p>{service.text}</p>
</Grid.Item>
</Grid.Root>
)
}Props in this example:
Warning: Don't forget
A nav is a landmark (a region people can jump to) and needs a name, in the page’s language. An Item is never <main> by default: a page has one, so choose it with as where it fits (1.3.1 Info and Relationships).
Contact details beside the text
Information that belongs to the page but isn’t part of the text goes in a side column. Write the content first and the side after it, so the content is at the start of the line and is read and visited first. Render the side as an aside and name it.
Housing adaptation grant
You can get a grant if you need to adapt your home because of a disability. Apply before the work starts.
import { Card, Grid, Heading } from '@kvirn-ui/react'
import { useId } from 'react'
import { useGridTexts } from './texts.ts'
export function SideInformation() {
const { texts } = useGridTexts()
const headingId = useId()
return (
<Grid.Root className="kv-grid--lg-cols-12 kv-grid--lg-gap-10">
<Grid.Item className="kv-grid-item--lg-span-9 kv-prose">
<Heading as="h3">{texts.contact.article}</Heading>
<p>{texts.contact.articleText}</p>
</Grid.Item>
<Grid.Item as="aside" aria-labelledby={headingId} className="kv-grid-item--lg-span-3">
<Card.Root className="kv-prose">
<Heading as="h4" id={headingId}>
{texts.contact.heading}
</Heading>
<p>{texts.contact.text}</p>
</Card.Root>
</Grid.Item>
</Grid.Root>
)
}Props in this example:
ason Grid.Item
Information: Did you know?
The cells follow the DOM order. In a right-to-left language the grid mirrors, so the first cell sits at the right and the side column moves to the left.
Short links in narrow columns
Links with a few words fit in a narrow column, so more of them sit side by side. Choose the narrowest fit class and a smaller gap.
import { Grid, Link } from '@kvirn-ui/react'
import { useGridTexts } from './texts.ts'
export function QuickLinks() {
const { texts } = useGridTexts()
const links = Object.values(texts.quickLinks)
return (
<Grid.Root className="kv-grid--fit-sm kv-grid--gap-4" as="ul">
{links.map((label) => (
<li key={label}>
<Link href="#">{label}</Link>
</li>
))}
</Grid.Root>
)
}Props in this example:
Information: Did you know?
A grid that is narrower than the fit size gets one column, so nothing scrolls sideways at 320px.
Accessibility#
What it does for you#
- APG pattern: none. Grid is a layout, not a widget, and it is not the ARIA
gridrole. - Deviations: none
- Native elements used:
<div>by default.Grid.Roottakesulorol, andGrid.Itemtakesli,article,section,nav,asideormain, withas. The element's own semantics apply.
Grid lays children out on explicit column tracks chosen with classes (kv-grid--cols-12, kv-grid--md-cols-2, kv-grid--fit-md, kv-grid-item--lg-span-8). It adds no ARIA, no text, no tabindex and no behaviour, and it has no data-* state. As a ul or ol Root it adds role="list", because WebKit and VoiceOver drop the list role under list-style: none. Everything a user perceives comes from the consumer's children, which keep their own semantics and focus order.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| Grid.Root | internal <div class="kv-grid-container"> | none | The query container around the grid element. No role, no ARIA, no attribute beyond its class. Grid renders a wrapper and the grid element, so a container query can size it |
| Grid.Root | <div> → generic, or <ul>/<ol> → list | role="list" on ul/ol only | class="kv-grid". Every prop, the ref and className go here. as="ul" with <li> Items is a list, and its count is announced |
| Grid.Item | <div> → generic | none. The consumer adds it with the element | class="kv-grid-item". Optional: any direct child is a cell. Outside a Root it warns once in development |
| Grid.Root/Item | as | the chosen element's own | A value outside the list is a type error. className joins, refs pass |
| both | never | no tabindex, inert, aria-hidden, data-* | No handler, no heading, no live region, no text |
useGrid() gives the same frozen props objects (only className): containerProps, rootProps and itemProps.
It announces nothing.
What you need to do#
- A list of links or cards is a list.
as="ul"or"ol"with<li>children (Grid.Item as="li"), so the count is announced (1.3.1 Info and Relationships (A)). Grid setsrole="list"itself; don't write it. - No reordering. There is no
order-*and no dense packing (1.3.2 Meaningful Sequence (A), 2.4.3 Focus Order (A)).start-*,end-*androw-start-*place an item on a line: placing a later item before an earlier one makes the visual order differ from the DOM, reading and focus order. Keep line placement in DOM order. (An accessibility trade-off.) - Start at one track. A bare
cols-n(n > 1),flow-colwithoutrows-n, orauto-cols-min|maxkeeps its tracks at 320px, so the cells squeeze and content overflows or overlaps (1.4.10 Reflow (AA)). Start withkv-grid--cols-1orkv-grid--fit-*and add columns with a breakpoint form (kv-grid--md-cols-n).fit-*usesmin(…, 100%)and never overflows. - Name the landmarks. A
nav,asideorsectionItem needsaria-labeloraria-labelledby, in the page's language. A page has onemain: Grid never renders one by default. - Language. Text in another language goes in a nested
KvirnProvider, withuseLocale().localePropson your own element (3.1.2 Language of Parts (AA)).
Warning: Don't forget
A grid has one column until you add more. Add columns from a breakpoint, such as kv-grid--lg-cols-3, or use a fit class. A bare kv-grid--cols-3 keeps three columns on a 320px screen, and content overflows (1.4.10 Reflow).
Visual and modes#
Headless: Grid ships no CSS. With @kvirn-ui/theme/theme.css:
- Focus indicator, target size, contrast: not applicable. Grid has no colour, no edge and no control of its own.
- forced-colors behaviour: not applicable, nothing is drawn.
- reduced-motion behaviour: no motion.
- Reflow (1.4.10 Reflow (AA)): the reflow sweep checks every story at 320 CSS pixels for sideways scrolling. Text spacing (1.4.12 Text Spacing (AA)): Grid sets no fixed height,
overflowor clip, and the text-spacing sweep applies the wider spacing to every story and fails on a box that clips text.
Warning: Don't forget
If you style a cell, don’t give it a fixed height or hide its overflow. When people widen the text spacing, the text would be cut off (1.4.12 Text Spacing).
Focus management#
- Strategy: native. Grid never moves focus and renders no
tabindex. Its children keep their own order, which is the DOM order. - Initial focus: not moved.
- Trap: no.
- Restore to: not applicable.
- Never obscured by: Grid renders no overlay and sets no
overflow,position: stickyor fixed height, so a child's focus ring is never clipped (2.4.11 Focus Not Obscured (Minimum) (AA); 2.4.13 Focus Appearance (AAA), not counted toward AA).
Allowed elements#
A tag outside the list changes the page's outline or meaning (1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A)). as is a string, so it works from a Server Component.
| Part | `as` | Why |
|---|---|---|
| Grid.Root | div (default), ul, ol | ul and ol with li Items are lists whose count is announced. No landmark element: use Container or Section |
| Grid.Item | div (default), li, article, section, nav, aside, main | li inside a ul/ol Root. A named section, nav (navigation) or aside (related content) is a landmark, and one without a name is noise. main (one per page) is the consumer's choice, never the default. article only for self-contained content |
WCAG success criteria#
- 1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A): a div has no role, and a
ulorolhasrole="list". - 1.3.2 Meaningful Sequence (A), 2.4.3 Focus Order (A): DOM order equals reading and focus order (the keyboard rows).
- 1.4.10 Reflow (AA): the reflow sweep checks that nothing scrolls sideways at 320 CSS pixels. 1.4.12 Text Spacing (AA): no fixed height, overflow or clip, and the text-spacing sweep fails on a box that clips text.
Keyboard#
Grid has no keys of its own. What you put in it keeps its own keys.
Announcements#
What a screen reader reads#
| State or action | Read aloud | Politeness | Test |
|---|---|---|---|
A div Root with Items | only the children's phrases in DOM order; none for the wrapper or the grid | none | grid.test.tsx › read aloud › a div Root reads only its children in DOM order |
as="ul" Root with li Items | list, listitem, <text>, end of list for each item | none | grid.test.tsx › read aloud › a ul Root with li Items reads as a list with its items |
An Item as="main" | main | none | grid.test.tsx › read aloud › a main Item reads as main |
A named nav Item | navigation, <name> | none | grid.test.tsx › read aloud › a named nav Item reads as a navigation |
An Item as="section" with a name | region, <name> | none | grid.test.tsx › read aloud › a named section Item reads as a region |
API reference#
import { Grid, useGrid } from '@kvirn-ui/react'Grid.Root
Renders <div class="kv-grid">, inside an internal <div class="kv-grid-container">, so the breakpoints answer to the grid’s own width. It takes every attribute of a <div> and passes ref to it. It has no state, so no data-*. Every prop and the ref go to the grid element, not to the wrapper. It has no role, except role="list" on a ul or ol. Put it in a block-level parent: in a shrink-to-fit parent (inline-flex, float) it collapses. Also exported as GridRoot. The default theme is described on Theming.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | – | Your own classes. They join the part’s class and never replace it, so the theme keeps styling the layout. Add kv-grid--cols-1 to -12 for a number of columns, kv-grid--fit-sm, -md or -lg for as many as fit, kv-grid--gap-n for the space, and a breakpoint for a wider grid: kv-grid--md-cols-2. |
as | 'div' | 'ul' | 'ol' | 'div' | Changes the element: ul or ol with li children for a list whose count is announced. A list gets role="list", because Safari drops the list role when the bullets are removed. In JavaScript, an element outside the list is replaced by a div and warns once in development. |
ref | Ref<HTMLElement> | – | Reaches the grid element, not the wrapper around it. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-grid | always | The part class. The default theme styles it. |
kv-grid-container | always | On the internal wrapper around the grid element. The breakpoints measure its width. |
kv-grid--cols-n | you add it | n equal columns, 1 to 12. The default is one column. A bare cols-n (n above 1), flow-col without rows-n or auto-cols-min|max keeps its tracks at 320px, so content overflows or overlaps (1.4.10): start at cols-1 or fit-* and add columns with a breakpoint form, kv-grid--lg-cols-3. |
kv-grid--fit-sm | -md | -lg | you add it | As many columns as fit, none narrower than 14rem, 18rem or 24rem. |
kv-grid--gap-n | you add it | The space between cells. n is 0 to 6, 8, 10, 12 or 16. The default gap is 1.5rem; gap-0 removes it. |
kv-grid--gap-x-n | kv-grid--gap-y-n | you add it | The space between columns only, or between rows only. |
kv-grid--rows-n | flow-row | flow-col | auto-rows-… | auto-cols-… | you add it | Explicit rows (1 to 12), the direction new cells fill in, and the size of rows or columns the grid adds itself. They change how cells are placed, not the order of the DOM. |
kv-grid--items-… | justify-items-… | place-items-… | content-… | justify-… | you add it | Where cells sit inside their area (start, center, end, stretch), and where the whole set of tracks sits inside the grid. |
kv-grid--sm-… | --md-… | --lg-… | --xl-… | --2xl-… | you add it | Any of the above from a breakpoint of the grid’s own width: sm 40rem, md 48rem, lg 64rem, xl 80rem, 2xl 96rem. For example kv-grid--lg-cols-12. |
Grid.Item
Renders <div class="kv-grid-item"> with no role. It takes every attribute of a <div> and passes ref to it. It has no state, so no data-*. Optional: any direct child of Root is a cell. Also exported as GridItem. An Item outside a Root warns in development.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | – | Your own classes. They join the part’s class and never replace it, so the theme keeps styling the layout. Add kv-grid-item--span-n for the columns it covers, start-n and end-n for the column lines, row-span-n for rows, and a breakpoint: kv-grid-item--lg-span-8. |
as | 'div' | 'li' | 'article' | 'section' | 'nav' | 'aside' | 'main' | 'div' | Changes the element: li inside a ul or ol Root, article, section, nav or aside with a name, or main (one per page, never the default). Grid adds no role of its own: the element’s meaning applies. |
ref | Ref<HTMLElement> | – | Reaches the element, whichever it is. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-grid-item | always | The part class. The default theme styles it. |
kv-grid-item--span-n | span-full | start-n | end-n | row-span-n | row-start-n | row-end-n | you add it | The columns and rows a cell covers, or the line it starts or ends at. Keep line placement in DOM order: reading and focus order follow the DOM. |
kv-grid-item--self-… | justify-self-… | place-self-… | you add it | Where one cell sits inside its own area, in place of what the Root chose. |
kv-grid-item--subgrid | rows-subgrid | you add it | The cell becomes a grid that shares the columns (or rows) of its parent, so its own cells line up with the cells around it. |
kv-grid-item--lg-span-8 | you add it | Any of the above from a breakpoint of the grid’s own width, as for the Root. |
useGrid
Gives the classes for your own elements. It takes no options, and the classes add no role, ARIA or tabindex. On your own ul or ol, add role="list" yourself, which Grid.Root does for you.
Result
| Prop | Type | Default | Description |
|---|---|---|---|
containerProps | GridPartProps | – | Spread on an element that wraps only the grid element: it is the container the breakpoints answer to. Only className. |
rootProps | GridPartProps | – | Spread on the grid’s element: only className. |
itemProps | GridPartProps | – | Spread on a cell’s element: only className. |
Known issues#
- Grid in a shrink-to-fit parent. In an inline-flex parent, a float or a flex item with no grow, the grid gets no width, so its cells overflow. Put Grid in a block-level parent, or give the flex item a size. It follows from Grid's two-element Root, and there is no other workaround.
Testing#
The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.
| AT + browser + OS | Agent check | Human check | Result | Notes |
|---|---|---|---|---|
| Automated (re-runnable tests) | ||||
| axe, WCAG 2.2 A and AA, every story, 4 themes | pass | n/a | pass | grid.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | grid.test.tsx (axe and role, name and state): adds role="list" to a ul and an ol Root, and no role to a div |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | 2 rows, each names a test in grid.test.tsx |
| Focus order and management | pass | n/a | pass | grid.test.tsx asserts focus |
| Display modes (forced colours, 320px reflow, text spacing, reduced motion) | pass | n/a | pass | the four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI |
| Core (required for beta) | ||||
| NVDA + Chrome + Windows | pending | pending | pending | |
| VoiceOver + Safari + iOS | pending | pending | pending | |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | pending | pending | pending | |
| TalkBack + Chrome + Android | pending | pending | pending | |
| VoiceOver + Safari + macOS | pending | pending | pending |
What to check#
- Move through a page in reading order with NVDA or VoiceOver: the order matches what you see, at wide and narrow widths.
- As a
ulwithliItems, it is read as "list, N items". - A named
navorasideItem is found in the landmarks list by its name. - At 400% zoom nothing needs sideways scrolling.